Class CalendarUtil.TimePeriod
- Enclosing class:
CalendarUtil
CalendarUtil.TimeUnit, with a start date and an end date.
A single period, such as 2026-Q1, has the same start period and end period. A range, such as
2026-M01 to 2026-M03, starts on the first day of its first period and ends on the last day
of its last period. The end date is the last day of the period, at the start of that day (except
CalendarUtil.TimeUnit.YEAR periods created from a date, see CalendarUtil.TimeUnit.startDate(Date)).
You get time periods from CalendarUtil.getTimePeriod(String) and the related methods of the
Datamart calendar, and pass them to timePeriodAsFilter(...) to filter a Datamart query on a date
field.
toString() returns a short text form, for example Q[2026-01-01, 2026-03-31] for a single
quarter. A range has an S after the unit letter, for example MS[2026-01-01, 2026-03-31].
def calendar = api.getDatamartContext().calendar()
def quarter = calendar.getTimePeriod("2026-Q1")
def nextQuarter = quarter.add(1)
- See Also:
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedInternal: used by the server to read saved time periods.TimePeriod(String timePeriodAsString) Creates a time period from a period name, such as2026-Q1.TimePeriod(Calendar cal, CalendarUtil.TimeUnit unit) Creates a single period of the given unit that contains the date of the given calendar.TimePeriod(Date startDate, Date endDate, CalendarUtil.TimeUnit unit) Creates a range from the period that containsstartDateto the period that containsendDate, both of the given unit.TimePeriod(Date startDate, CalendarUtil.TimeUnit unit) Creates a single period of the given unit that contains the given date.TimePeriod(CalendarUtil.TimePeriod startPeriod, CalendarUtil.TimePeriod endPeriod, CalendarUtil.TimeUnit unit) Creates a range from the start ofstartPeriodto the end ofendPeriod, in the given unit. -
Method Summary
Modifier and TypeMethodDescriptionReturns a new time period moved by the given number of periods.Returns the last day of the time period, at the start of that day (exceptCalendarUtil.TimeUnit.YEARperiods created from a date, seeCalendarUtil.TimeUnit.startDate(Date)).Returns the last period of the range as a single period.Returns the first day of the time period, at the start of that day (exceptCalendarUtil.TimeUnit.YEARperiods created from a date, seeCalendarUtil.TimeUnit.startDate(Date)).Returns the first period of the range as a single period.Returns the unit of the time period.toString()Returns a short text form of the time period: the first letter of the unit, anSfor a range, and the start and end dates.
-
Constructor Details
-
TimePeriod
protected TimePeriod()Internal: used by the server to read saved time periods. Not for use in logic. -
TimePeriod
Creates a single period of the given unit that contains the date of the given calendar.For
CalendarUtil.TimeUnit.YEAR, the period starts on the date of the calendar, not on January 1, and ends one day before the same date of the next year (seeCalendarUtil.TimeUnit.startDate(Date)).- Parameters:
cal- Calendar whose date is inside the period.unit- Unit of the period.
-
TimePeriod
Creates a time period from a period name, such as2026-Q1. SeeCalendarUtilfor the supported formats.- Parameters:
timePeriodAsString- Period name.- Throws:
ParseException- IftimePeriodAsStringis not a valid period name.
-
TimePeriod
Creates a single period of the given unit that contains the given date.For
CalendarUtil.TimeUnit.YEAR, the period starts on the given date, not on January 1, and ends one day before the same date of the next year (seeCalendarUtil.TimeUnit.startDate(Date)).- Parameters:
startDate- Date inside the period.unit- Unit of the period.
-
TimePeriod
Creates a range from the period that containsstartDateto the period that containsendDate, both of the given unit.For
CalendarUtil.TimeUnit.YEAR, the range starts onstartDate, not on January 1, and ends one day before the same date asendDatein the next year (seeCalendarUtil.TimeUnit.startDate(Date)).- Parameters:
startDate- Date inside the first period of the range.endDate- Date inside the last period of the range.unit- Unit of the periods.
-
TimePeriod
public TimePeriod(CalendarUtil.TimePeriod startPeriod, CalendarUtil.TimePeriod endPeriod, CalendarUtil.TimeUnit unit) Creates a range from the start ofstartPeriodto the end ofendPeriod, in the given unit.- Parameters:
startPeriod- Period at the start of the range.endPeriod- Period at the end of the range.unit- Unit of the periods in the range.
-
-
Method Details
-
getStartDate
Returns the first day of the time period, at the start of that day (exceptCalendarUtil.TimeUnit.YEARperiods created from a date, seeCalendarUtil.TimeUnit.startDate(Date)).- Returns:
- Start date of the time period.
-
getTimeUnit
Returns the unit of the time period.- Returns:
- Unit of the time period.
-
getEndDate
Returns the last day of the time period, at the start of that day (exceptCalendarUtil.TimeUnit.YEARperiods created from a date, seeCalendarUtil.TimeUnit.startDate(Date)).- Returns:
- End date of the time period.
-
getStartPeriod
Returns the first period of the range as a single period. For a single period, returns this period.- Returns:
- First period of the range.
-
getEndPeriod
Returns the last period of the range as a single period. For a single period, returns this period.- Returns:
- Last period of the range.
-
add
Returns a new time period moved by the given number of periods. A negative number moves it back. A range keeps its length. This time period is not changed.- Parameters:
numberPeriods- Number of periods to move by.- Returns:
- Moved time period, or
nullifnumberPeriodsisnull.
-
toString
Returns a short text form of the time period: the first letter of the unit, anSfor a range, and the start and end dates. For example,Q[2026-01-01, 2026-03-31].
-