Class CalendarUtil

Object
CalendarUtil

public class CalendarUtil extends Object
Helper for dates and time periods in the Analytics calendar: years, quarters, months, weeks and days.

It turns a period name such as 2026-Q1 into a CalendarUtil.TimePeriod, lists the periods between two periods or dates, offsets a period by a number of time units, and parses dates. A time period can then filter a Datamart query on a date field.

In logic code, you get an instance from api.getDatamartContext().calendar(). That object also implements DatamartContext.Calendar, which describes the calendar methods intended for logic code. Logic code does not create instances directly.

Period names use these formats:

  • Year: 2026
  • Quarter: 2026-Q1 (quarters 1 to 4)
  • Month: 2026-M01 or 202601
  • Week: 2026-W05
  • Day: a date such as 2026-01-15
Year, quarter, month and week names must use a year from 1970 onward. An earlier year, such as 1950, is read as a date and gives a one-day period.

Week periods follow the calendar settings of the partition: the advanced configuration options calendarFirstDayOfWeek (Sunday = 1 to Saturday = 7, default Sunday) and calendarMinimalDaysInFirstWeek (1 to 7, default 1).

The end date of a time period 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)).


 def calendar = api.getDatamartContext().calendar()
 def quarter = calendar.getTimePeriod("2026-Q1")
 def filter = calendar.timePeriodAsFilter(quarter, "InvoiceDate")
 
See Also:
  • Field Details

  • Constructor Details

    • CalendarUtil

      public CalendarUtil()
      Internal: creates a calendar helper with the default date and date-time patterns. Not for use in logic. In logic code, use api.getDatamartContext().calendar().
    • CalendarUtil

      public CalendarUtil(String datePattern, String dateTimePattern)
      Internal: creates a calendar helper with custom patterns for formatting and parsing. Not for use in logic. In logic code, use api.getDatamartContext().calendar().
      Parameters:
      datePattern - Pattern for dates, or null for DEFAULT_DATE_PATTERN.
      dateTimePattern - Pattern for date-times, or null for DEFAULT_DATETIME_PATTERN.
  • Method Details

    • getYear

      public String getYear(Date date)
      Returns the name of the year that contains the given date, for example "2026".
      Parameters:
      date - Date inside the year.
      Returns:
      Year name, or null if date is null.
      See Also:
    • getQuarter

      public String getQuarter(Date date)
      Returns the name of the quarter that contains the given date, for example "2026-Q1" for 2026-02-15.
      Parameters:
      date - Date inside the quarter.
      Returns:
      Quarter name, or null if date is null.
      See Also:
    • getMonth

      public String getMonth(Date date)
      Returns the name of the month that contains the given date, for example "2026-M02" for 2026-02-15.
      Parameters:
      date - Date inside the month.
      Returns:
      Month name, or null if date is null.
      See Also:
    • getWeek

      public String getWeek(Date date)
      Returns the name of the week that contains the given date, for example "2026-W07". The week number follows the calendar settings of the partition (see CalendarUtil). The year in the name is the year that the week belongs to, which can differ from the year of the date: a date at the end of December can be in week 1 of the next year, for example "2027-W01".
      Parameters:
      date - Date inside the week.
      Returns:
      Week name, or null if date is null.
      See Also:
    • getDay

      public String getDay(Date date)
      Returns the name of the day, which is the date formatted with formatDate(Date), for example "2026-02-15".
      Parameters:
      date - Date to format.
      Returns:
      Day name, or null if date is null.
      See Also:
    • today

      public String today()
      Returns the current date, formatted with formatDate(Date), for example "2026-02-15".
      Returns:
      Current date as text.
    • date

      public static String date()
      Returns the current date of the server in the format yyyy-MM-dd, for example "2026-02-15".
      Returns:
      Current date as text.
    • time

      public static String time()
      Returns the current date and time of the server in the format yyyy-MM-dd'T'HH:mm:ss, for example "2026-02-15T14:30:00".
      Returns:
      Current date and time as text.
    • fileInfixTimestamp

      public String fileInfixTimestamp()
      Returns the current date and time in UTC in the format yyyyMMdd_HHmmss, for example "20260215_143000". Use it to make file names unique.
      Returns:
      Timestamp for a file name.
    • fileInfixTimestamp

      public String fileInfixTimestamp(Date date)
      Returns the given date and time in UTC in the format yyyyMMdd_HHmmss, for example "20260215_143000". Use it to make file names unique.
      Parameters:
      date - Date and time to format. Must not be null.
      Returns:
      Timestamp for a file name.
    • getTimePeriod

      public CalendarUtil.TimePeriod getTimePeriod(String periodAsString)
      Returns the time period for a period name, such as 2026-Q1. See CalendarUtil for the supported formats.

      An invalid quarter, month or week number also returns null, for example 2026-Q5, or week 53 in a year that has only 52 weeks. This depends on the advanced configuration option calendarThrowOnInvalidTimePeriod, which is true by default. When it is set to false, the behavior of versions before 16.0 applies: most invalid numbers still return null, but week 53 of some years that have only 52 weeks returns a one-day period instead.

      Parameters:
      periodAsString - Period name, such as 2026, 2026-Q1, 2026-M01 or 2026-W05.
      Returns:
      Matching time period, or null if periodAsString is null or cannot be parsed.
      See Also:
    • newTimePeriod

      public CalendarUtil.TimePeriod newTimePeriod(String startDateAsString, String endDateAsString, String unitAsString)
      Creates a range of periods of the given unit, from the period that contains the start date to the period that contains the end date. For example, newTimePeriod("2026-01-15", "2026-03-10", "Month") returns the range from 2026-01-01 to 2026-03-31.

      If the unit name is null or not valid, the unit is CalendarUtil.TimeUnit.DAY. If a date cannot be parsed, the matching date of the range is null. For CalendarUtil.TimeUnit.YEAR, the range does not start on January 1 (see CalendarUtil.TimeUnit.startDate(Date)).

      Parameters:
      startDateAsString - Date inside the first period, for example 2026-01-15. See parseDate(String).
      endDateAsString - Date inside the last period, or null. If it is null, the range has no end date.
      unitAsString - Name of a CalendarUtil.TimeUnit, for example "Month".
      Returns:
      Time period, or null if startDateAsString is null.
      See Also:
    • newTimePeriod

      public CalendarUtil.TimePeriod newTimePeriod(String startDateAsString, String unitAsString)
      Returns the single period of the given unit that contains the given date. For example, newTimePeriod("2026-01-15", "Month") returns the period 2026-M01.

      It works like getTimePeriodByDate(String, String), with these differences: if the unit name is null or not valid, the unit is CalendarUtil.TimeUnit.DAY, and if the date is null, the result is null. For CalendarUtil.TimeUnit.YEAR, the period does not start on January 1 (see CalendarUtil.TimeUnit.startDate(Date)).

      Parameters:
      startDateAsString - Date inside the period, for example 2026-01-15. See parseDate(String).
      unitAsString - Name of a CalendarUtil.TimeUnit, for example "Month".
      Returns:
      Time period that contains the date, or null if startDateAsString is null.
      See Also:
    • getTimePeriodByDate

      public CalendarUtil.TimePeriod getTimePeriodByDate(String dateAsString, String unitAsString)
      Returns the single period of the given unit that contains the given date. For example, getTimePeriodByDate("2026-01-15", "Month") returns the period 2026-M01.

      For the unit "Year", the period starts on the given date, not on January 1: getTimePeriodByDate("2026-06-15", "Year") returns 2026-06-15 to 2027-06-14. To get the calendar year, use getTimePeriod(String) with the year, for example getTimePeriod("2026").

      Parameters:
      dateAsString - Date inside the period, for example 2026-01-15. See parseDate(String).
      unitAsString - Name of a CalendarUtil.TimeUnit, for example "Month". Must be a valid unit name.
      Returns:
      Time period that contains the date.
      See Also:
    • getTimePeriodName

      public String getTimePeriodName(CalendarUtil.TimePeriod period)
      Returns the name of a time period, for example "2026-Q1". For a range, it returns the name of the first period of the range.
      Parameters:
      period - Time period.
      Returns:
      Period name, or null if period is null.
      See Also:
    • getTimePeriodName

      public String getTimePeriodName(Date date, CalendarUtil.TimeUnit unit)
      Returns the name of the period of the given unit that contains the given date, for example "2026-M02" for 2026-02-15 and CalendarUtil.TimeUnit.MONTH. See getYear(Date), getQuarter(Date), getMonth(Date), getWeek(Date) and getDay(Date).
      Parameters:
      date - Date inside the period.
      unit - Unit of the period.
      Returns:
      Period name, or null if date or unit is null.
    • getTimePeriodNames

      public List<String> getTimePeriodNames(String startPeriodAsString, String endPeriodAsString, String unitAsString)
      Returns the names of the periods of the given unit, in date order, from the start period to the end period. For example, getTimePeriodNames("2026-Q1", "2026-Q2", "Month") returns ["2026-M01", "2026-M02", ..., "2026-M06"].

      The method calls getTimePeriods(Date, Date, TimeUnit) with the start date of the start period and the end date of the end period, so the same limit applies: when the unit does not line up with the given periods, the last period can be missing. For example, getTimePeriodNames("2026-Q1", "2026-Q1", "Week") misses the week that contains March 31, and "2026-W05" to "2026-W10" with "Month" misses March.

      Parameters:
      startPeriodAsString - Name of the start period.
      endPeriodAsString - Name of the end period.
      unitAsString - Name of a CalendarUtil.TimeUnit, for example "Month".
      Returns:
      Period names. Empty if a period name or the unit name cannot be parsed.
      See Also:
    • getTimePeriods

      public List<CalendarUtil.TimePeriod> getTimePeriods(String startPeriodAsString, String endPeriodAsString, String unitAsString)
      Returns the periods of the given unit, in date order, from the start period to the end period. The same as getTimePeriodNames(String, String, String), but returns CalendarUtil.TimePeriod objects.

      The method calls getTimePeriods(Date, Date, TimeUnit) with the start date of the start period and the end date of the end period, so the same limit applies: when the unit does not line up with the given periods, the last period can be missing. For example, getTimePeriods("2026-Q1", "2026-Q1", "Week") misses the week that contains March 31, and "2026-W05" to "2026-W10" with "Month" misses March.

      Parameters:
      startPeriodAsString - Name of the start period.
      endPeriodAsString - Name of the end period.
      unitAsString - Name of a CalendarUtil.TimeUnit, for example "Month".
      Returns:
      Time periods, or null if a period name or the unit name cannot be parsed.
      See Also:
    • getTimePeriods

      public List<CalendarUtil.TimePeriod> getTimePeriods(Date startDate, Date endDate, CalendarUtil.TimeUnit unit)
      Returns the periods of the given unit, in date order, starting with the period that contains startDate.

      The method moves startDate forward by one unit at a time, as long as the moved date is not after endDate, and returns the period of each date. If startDate is not the first day of its period, the period that contains endDate can be missing. For example, for 2026-01-15 to 2026-03-01 and CalendarUtil.TimeUnit.MONTH, the result is January and February only. To be safe, pass the start date of a period as startDate. For CalendarUtil.TimeUnit.YEAR, the periods do not start on January 1 (see CalendarUtil.TimeUnit.startDate(Date)).

      Parameters:
      startDate - Date inside the first period.
      endDate - Date inside the last period.
      unit - Unit of the periods.
      Returns:
      Time periods. Empty if any argument is null or if endDate is before startDate.
      See Also:
    • getStartDate

      public Date getStartDate(String period)
      Returns the start date of the time period with the given name.
      Parameters:
      period - Period name, such as 2026-Q1.
      Returns:
      First day of the period, or null if period is null or cannot be parsed.
      See Also:
    • getEndDate

      public Date getEndDate(String period)
      Returns the end date of the time period with the given name.
      Parameters:
      period - Period name, such as 2026-Q1.
      Returns:
      Last day of the period, at the start of that day, or null if period is null or cannot be parsed.
      See Also:
    • add

      public String add(String period, int number, String unit)
      Moves the start of a period by a number of units and returns the name of the period that contains the result, in the same unit as the input period. For example:
      • add("2026-M01", 12, "Month") returns "2027-M01".
      • add("2026-Q1", 1, "Quarter") returns "2026-Q2".
      • add("2026", 3, "Month") returns "2026", because April is still in 2026.
      • add("2026", -3, "Month") returns "2025".
      Parameters:
      period - Name of the period to move, such as 2026-M01.
      number - Number of units to move by. A negative number moves back.
      unit - Name of a CalendarUtil.TimeUnit to move by, for example "Month".
      Returns:
      Name of the resulting period, or null if period or unit is null, or if period cannot be parsed.
      Throws:
      IllegalArgumentException - If unit is not a valid CalendarUtil.TimeUnit name.
      See Also:
    • getDaysDiff

      public long getDaysDiff(String period2, String period1)
      Returns the number of days from the start of period1 to the end of period2, both days included. Pass the later period first. For example, getDaysDiff("2026-M05", "2026-M04") returns 61, because April has 30 days and May has 31.
      Parameters:
      period2 - Name of the end period.
      period1 - Name of the start period.
      Returns:
      Number of days, or 0 if either period is null or cannot be parsed.
      See Also:
    • parseTimePeriod

      public CalendarUtil.TimePeriod parseTimePeriod(String period) throws ParseException
      Returns the time period for a period name, such as 2026-Q1. See CalendarUtil for the supported formats. Text that is not a period name but a date or a date-time gives a one-day period. Unlike getTimePeriod(String), this method throws an exception instead of returning null.

      An invalid quarter, month or week number, for example 2026-Q5, or week 53 in a year that has only 52 weeks, throws an exception. This depends on the advanced configuration option calendarThrowOnInvalidTimePeriod, which is true by default. When it is set to false, the text is then read as a date: in most cases it still throws an exception, but week 53 of some years that have only 52 weeks returns a one-day period instead.

      Parameters:
      period - Period name, such as 2026, 2026-Q1, 2026-M01, 202601 or 2026-W05.
      Returns:
      Matching time period. Never null.
      Throws:
      ParseException - If period is null or cannot be parsed.
      See Also:
    • parseDate

      public Date parseDate(String dateAsString)
      Converts text to a date, trying several formats in order: yyyy-MM-dd, yyyy/MM/dd, dd-MM-yyyy, EEE MMM dd HH:mm:ss zzz yyyy, and then the date-time formats of parseDateTime(String). For an ambiguous date such as 03/04/2026, the order of day and month follows the locale of the request.
      Parameters:
      dateAsString - Text to convert.
      Returns:
      Matching date, or null if no format matches.
      See Also:
    • parseDateTime

      public DateTime parseDateTime(String datetimeAsString)
      Converts text to a date-time, trying yyyy-MM-dd HH:mm:ss, then yyyy-MM-dd'T'HH:mm:ss, and then a flexible parser. For an ambiguous date such as 03/04/2026, the order of day and month follows the locale of the request.
      Parameters:
      datetimeAsString - Text to convert.
      Returns:
      Matching Joda-Time DateTime, or null if no format matches.
      See Also:
    • formatDate

      public String formatDate(Date date)
      Formats a date with the date pattern of this instance, DEFAULT_DATE_PATTERN by default. The date is formatted in UTC. A java.sql.Date is formatted as its local date, without a conversion to UTC.
      Parameters:
      date - Date to format. Must not be null.
      Returns:
      Formatted date, for example "2026-02-15".
    • formatDate

      public String formatDate(LocalDate localDate)
      Formats a date with the date pattern of this instance, DEFAULT_DATE_PATTERN by default.
      Parameters:
      localDate - Date to format. Must not be null.
      Returns:
      Formatted date, for example "2026-02-15".
    • formatDateTime

      public String formatDateTime(Date date)
      Formats a date-time with the date-time pattern of this instance, DEFAULT_DATETIME_PATTERN by default. The date-time is formatted in UTC. A java.sql.Date has no time, so only the date is formatted, with the date pattern.
      Parameters:
      date - Date-time to format. Must not be null.
      Returns:
      Formatted date-time, for example "2026-02-15T14:30:00".
    • formatDateTime

      public String formatDateTime(LocalDateTime localDateTime)
      Formats a date-time with the date-time pattern of this instance, DEFAULT_DATETIME_PATTERN by default.
      Parameters:
      localDateTime - Date-time to format. Must not be null.
      Returns:
      Formatted date-time, for example "2026-02-15T14:30:00".
    • formatDateTime

      public String formatDateTime(DateTime dateTime)
      Formats a Joda-Time DateTime with the date-time pattern of this instance, DEFAULT_DATETIME_PATTERN by default. The date-time is formatted in UTC.
      Parameters:
      dateTime - Date-time to format. Must not be null.
      Returns:
      Formatted date-time, for example "2026-02-15T14:30:00".