Class CalendarUtil.TimePeriod

Object
TimePeriod
Enclosing class:
CalendarUtil

public static class CalendarUtil.TimePeriod extends Object
A range of one or more consecutive periods of the same 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 Details

    • TimePeriod

      protected TimePeriod()
      Internal: used by the server to read saved time periods. Not for use in logic.
    • TimePeriod

      public TimePeriod(Calendar cal, CalendarUtil.TimeUnit unit)
      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 (see CalendarUtil.TimeUnit.startDate(Date)).

      Parameters:
      cal - Calendar whose date is inside the period.
      unit - Unit of the period.
    • TimePeriod

      public TimePeriod(String timePeriodAsString) throws ParseException
      Creates a time period from a period name, such as 2026-Q1. See CalendarUtil for the supported formats.
      Parameters:
      timePeriodAsString - Period name.
      Throws:
      ParseException - If timePeriodAsString is not a valid period name.
    • TimePeriod

      public TimePeriod(Date startDate, CalendarUtil.TimeUnit unit)
      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 (see CalendarUtil.TimeUnit.startDate(Date)).

      Parameters:
      startDate - Date inside the period.
      unit - Unit of the period.
    • TimePeriod

      public TimePeriod(Date startDate, Date endDate, CalendarUtil.TimeUnit unit)
      Creates a range from the period that contains startDate to the period that contains endDate, both of the given unit.

      For CalendarUtil.TimeUnit.YEAR, the range starts on startDate, not on January 1, and ends one day before the same date as endDate in the next year (see CalendarUtil.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 of startPeriod to the end of endPeriod, 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

      public Date getStartDate()
      Returns the first day of the time period, at the start of that day (except CalendarUtil.TimeUnit.YEAR periods created from a date, see CalendarUtil.TimeUnit.startDate(Date)).
      Returns:
      Start date of the time period.
    • getTimeUnit

      public CalendarUtil.TimeUnit getTimeUnit()
      Returns the unit of the time period.
      Returns:
      Unit of the time period.
    • getEndDate

      public Date getEndDate()
      Returns the last day of the time period, at the start of that day (except CalendarUtil.TimeUnit.YEAR periods created from a date, see CalendarUtil.TimeUnit.startDate(Date)).
      Returns:
      End date of the time period.
    • getStartPeriod

      public CalendarUtil.TimePeriod 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

      public CalendarUtil.TimePeriod 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

      public CalendarUtil.TimePeriod add(Integer numberPeriods)
      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 null if numberPeriods is null.
    • toString

      public String toString()
      Returns a short text form of the time period: the first letter of the unit, an S for a range, and the start and end dates. For example, Q[2026-01-01, 2026-03-31].
      Overrides:
      toString in class Object
      Returns:
      Text form of the time period.