Package com.storedobject.core
Class AbstractPeriod<T extends Date>
java.lang.Object
com.storedobject.common.Range<T>
com.storedobject.core.AbstractPeriod<T>
- Type Parameters:
T- The type of the date, extendingDate.
- Direct Known Subclasses:
DatePeriod,TimePeriod,TimestampPeriod
AbstractPeriod represents a time period defined by a start date (from)
and an end date (to). It extends the
Range class and provides utility
methods for working with time periods. It supports operations such as
retrieving formatted date strings, calculating date-based conditions for databases,
and checking for overlaps or inclusion within the defined period.- Author:
- Syam
-
Field Summary
-
Constructor Summary
ConstructorsConstructorDescriptionAbstractPeriod(T from, T to) Constructs a new AbstractPeriod object with the specified start and end points. -
Method Summary
Modifier and TypeMethodDescriptionRetrieves a Calendar instance representing the starting point of the period.Retrieves a Calendar instance representing the ending point of the period.Generates a database condition string representing a range between two date points.Generates a database condition string for a date range based on the starting and ending points of the period.Generates a database condition string representing the time range between the start of the 'from' date and the end of the 'to' date.Generates a database condition string for the time range (start of 'from' to end of 'to') using the specified TransactionManager to convert to GMT.intgetMonth()Retrieves the month value of the starting point of the period.intRetrieves the month value of the starting point of the period.intRetrieves the month value of the ending point of the period.intCalculates the duration of the period in days by determining the difference between the start and end points of the period.intCalculates and retrieves the total duration of the period in months.intgetYear()Retrieves the year value of the starting point of the period.intRetrieves the year value from the starting point of the period.intRetrieves the year value of the ending point of the period.booleaninside(long time) Checks if the given time is within this period.booleanChecks if the given date is within this period.booleanoverlaps(AbstractPeriod<?> other) Checks if this period overlaps with another period.protected StringtoDBString(T date) Converts the given `date` to a database-compatible string format.Returns a short string representation of the period.Convert to string in the given "date format".protected longGets the long value representing the date.
-
Constructor Details
-
AbstractPeriod
-
-
Method Details
-
getCalendarFrom
Retrieves a Calendar instance representing the starting point of the period. The date is extracted using the `getFrom` method and set into a GregorianCalendar object.- Returns:
- A Calendar instance initialized to the starting date of the period.
-
getCalendarTo
Retrieves a Calendar instance representing the ending point of the period. The date is extracted using the `getTo` method and set into a GregorianCalendar object.- Returns:
- A Calendar instance initialized to the ending date of the period.
-
value
-
getPeriodInDays
public int getPeriodInDays()Calculates the duration of the period in days by determining the difference between the start and end points of the period.- Returns:
- The number of days in the current period.
-
getPeriodInMonths
public int getPeriodInMonths()Calculates and retrieves the total duration of the period in months. The period is determined based on the start and end points, where the number of months between these points is calculated.- Returns:
- The number of months within the period.
-
toDBString
-
getMonth
public int getMonth()Retrieves the month value of the starting point of the period.- Returns:
- The integer representation of the month (1-based, where January is 1 and December is 12) corresponding to the starting point of the period.
-
getYear
public int getYear()Retrieves the year value of the starting point of the period.- Returns:
- The integer representation of the year corresponding to the starting point of the period.
-
getMonthFrom
public int getMonthFrom()Retrieves the month value of the starting point of the period. The month is extracted from the starting date using thegetFrommethod.- Returns:
- The integer representation of the month (1-based, where January is 1 and December is 12) corresponding to the starting point of the period.
-
getYearFrom
public int getYearFrom()Retrieves the year value from the starting point of the period. The year is extracted using the `getFrom` method and processed through the `DateUtility.getYear` method.- Returns:
- The integer representation of the year corresponding to the starting point of the period.
-
getMonthTo
public int getMonthTo()Retrieves the month value of the ending point of the period. The month is derived from the date returned by thegetTomethod using theDateUtility.getMonthutility.- Returns:
- The integer representation of the month (1-based, where January is 1 and December is 12) corresponding to the ending point of the period.
-
getYearTo
public int getYearTo()Retrieves the year value of the ending point of the period. The year is derived using thegetTomethod and processed through theDateUtility.getYearutility.- Returns:
- The integer representation of the year corresponding to the ending point of the period.
-
getDBCondition
Generates a database condition string representing a range between two date points.- Returns:
- A string representing the database condition for the period. If the start or end points are null, the method returns null. If the start and end points are equal, the result is an equality condition. Otherwise, the result is a range condition in the format "BETWEEN 'start' AND 'end'".
-
getDBCondition
Generates a database condition string for a date range based on the starting and ending points of the period. The dates are converted to a database-compatible string format using the `TransactionManager` and utility methods.- Parameters:
tm- The TransactionManager instance used for retrieving and formatting the date in GMT.- Returns:
- A string representing the database condition for the period. The result is based on the formatted start and end dates. If the start or end points are null, the method returns null. If the start and end points are equal, the result is an equality condition. Otherwise, the result represents a range condition in the format "BETWEEN 'start' AND 'end'".
-
getDBTimeCondition
Generates a database condition string representing the time range between the start of the 'from' date and the end of the 'to' date.- Returns:
- A string representing the database condition for the time period.
-
getDBTimeCondition
Generates a database condition string for the time range (start of 'from' to end of 'to') using the specified TransactionManager to convert to GMT.- Parameters:
tm- The TransactionManager instance to use for GMT conversion.- Returns:
- A string representing the database condition for the time period in GMT.
-
toString
Convert to string in the given "date format". The "date format" passed must be a valid format for creating a formatted date/date-time withSimpleDateFormat. For example, by passing "MMM dd, yyyy" will result in a formatted output like "Jan 23, 1998 - Mar 6, 1999".- Parameters:
format- Date format.- Returns:
- Formatted output.
-
inside
public boolean inside(long time) Checks if the given time is within this period.- Parameters:
time- Time in milliseconds to check.- Returns:
- True if the time is within the period (inclusive of 'from', exclusive of 'to').
-
inside
Checks if the given date is within this period.- Parameters:
date- Date to check.- Returns:
- True if the date is within the period.
-
toShortString
Returns a short string representation of the period. If start and end dates are the same, only one date is returned.- Returns:
- Short string representation.
-
overlaps
Checks if this period overlaps with another period.- Parameters:
other- The other period to check against.- Returns:
- True if the periods overlap.
-