Class CalendarUtil
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-M01or202601 - Week:
2026-W05 - Day: a date such as
2026-01-15
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classA range of one or more consecutive periods of the sameCalendarUtil.TimeUnit, with a start date and an end date.static enumUnit of aCalendarUtil.TimePeriod: day, week, month, quarter or year. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringAlternative date patternyyyy/MM/ddthatparseDate(String)also accepts.static final StringAlternative date patterndd-MM-yyyythatparseDate(String)also accepts.static final StringAlternative date-time patternyyyy-MM-dd HH:mm:ssthatparseDateTime(String)tries first.static final CalendarUtilInternal: shared instance with the default date and date-time patterns, used by the server.static final StringDefault pattern for dates,yyyy-MM-dd.static final StringDefault pattern for date-times,yyyy-MM-dd'T'HH:mm:ss.static final StringPatternEEE MMM dd HH:mm:ss zzz yyyy(the text form ofDate, for exampleThu Jan 15 00:00:00 UTC 2026) thatparseDate(String)also accepts. -
Constructor Summary
ConstructorsConstructorDescriptionInternal: creates a calendar helper with the default date and date-time patterns.CalendarUtil(String datePattern, String dateTimePattern) Internal: creates a calendar helper with custom patterns for formatting and parsing. -
Method Summary
Modifier and TypeMethodDescriptionMoves 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.static Stringdate()Returns the current date of the server in the formatyyyy-MM-dd, for example"2026-02-15".Returns the current date and time in UTC in the formatyyyyMMdd_HHmmss, for example"20260215_143000".fileInfixTimestamp(Date date) Returns the given date and time in UTC in the formatyyyyMMdd_HHmmss, for example"20260215_143000".formatDate(LocalDate localDate) Formats a date with the date pattern of this instance,DEFAULT_DATE_PATTERNby default.formatDate(Date date) Formats a date with the date pattern of this instance,DEFAULT_DATE_PATTERNby default.formatDateTime(LocalDateTime localDateTime) Formats a date-time with the date-time pattern of this instance,DEFAULT_DATETIME_PATTERNby default.formatDateTime(Date date) Formats a date-time with the date-time pattern of this instance,DEFAULT_DATETIME_PATTERNby default.formatDateTime(DateTime dateTime) Formats a Joda-TimeDateTimewith the date-time pattern of this instance,DEFAULT_DATETIME_PATTERNby default.Returns the name of the day, which is the date formatted withformatDate(Date), for example"2026-02-15".longgetDaysDiff(String period2, String period1) Returns the number of days from the start ofperiod1to the end ofperiod2, both days included.getEndDate(String period) Returns the end date of the time period with the given name.Returns the name of the month that contains the given date, for example"2026-M02"for 2026-02-15.getQuarter(Date date) Returns the name of the quarter that contains the given date, for example"2026-Q1"for 2026-02-15.getStartDate(String period) Returns the start date of the time period with the given name.getTimePeriod(String periodAsString) Returns the time period for a period name, such as2026-Q1.getTimePeriodByDate(String dateAsString, String unitAsString) Returns the single period of the given unit that contains the given date.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 andCalendarUtil.TimeUnit.MONTH.Returns the name of a time period, for example"2026-Q1".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.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.getTimePeriods(Date startDate, Date endDate, CalendarUtil.TimeUnit unit) Returns the periods of the given unit, in date order, starting with the period that containsstartDate.Returns the name of the week that contains the given date, for example"2026-W07".Returns the name of the year that contains the given date, for example"2026".newTimePeriod(String startDateAsString, String unitAsString) Returns the single period of the given unit that contains the given date.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.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 ofparseDateTime(String).DateTimeparseDateTime(String datetimeAsString) Converts text to a date-time, tryingyyyy-MM-dd HH:mm:ss, thenyyyy-MM-dd'T'HH:mm:ss, and then a flexible parser.parseTimePeriod(String period) Returns the time period for a period name, such as2026-Q1.static Stringtime()Returns the current date and time of the server in the formatyyyy-MM-dd'T'HH:mm:ss, for example"2026-02-15T14:30:00".today()Returns the current date, formatted withformatDate(Date), for example"2026-02-15".
-
Field Details
-
DEFAULT_DATETIME_PATTERN
Default pattern for date-times,yyyy-MM-dd'T'HH:mm:ss. Used byformatDateTime(Date)andparseDateTime(String)unless the instance was created with another pattern.- See Also:
-
DEFAULT_DATE_PATTERN
Default pattern for dates,yyyy-MM-dd. Used byformatDate(Date)andparseDate(String)unless the instance was created with another pattern.- See Also:
-
ALT_DATE_PATTERN_1
Alternative date patternyyyy/MM/ddthatparseDate(String)also accepts.- See Also:
-
ALT_DATE_PATTERN_2
Alternative date patterndd-MM-yyyythatparseDate(String)also accepts.- See Also:
-
ALT_DATETIME_PATTERN
Alternative date-time patternyyyy-MM-dd HH:mm:ssthatparseDateTime(String)tries first.- See Also:
-
SIMPLEDATEFORMAT_PATTERN
PatternEEE MMM dd HH:mm:ss zzz yyyy(the text form ofDate, for exampleThu Jan 15 00:00:00 UTC 2026) thatparseDate(String)also accepts.- See Also:
-
DEFAULT
Internal: shared instance with the default date and date-time patterns, used by the server. Not for use in logic. In logic code, useapi.getDatamartContext().calendar().
-
-
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, useapi.getDatamartContext().calendar(). -
CalendarUtil
Internal: creates a calendar helper with custom patterns for formatting and parsing. Not for use in logic. In logic code, useapi.getDatamartContext().calendar().- Parameters:
datePattern- Pattern for dates, ornullforDEFAULT_DATE_PATTERN.dateTimePattern- Pattern for date-times, ornullforDEFAULT_DATETIME_PATTERN.
-
-
Method Details
-
getYear
Returns the name of the year that contains the given date, for example"2026".- Parameters:
date- Date inside the year.- Returns:
- Year name, or
nullifdateisnull. - See Also:
-
getQuarter
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
nullifdateisnull. - See Also:
-
getMonth
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
nullifdateisnull. - See Also:
-
getWeek
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 (seeCalendarUtil). 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
nullifdateisnull. - See Also:
-
getDay
Returns the name of the day, which is the date formatted withformatDate(Date), for example"2026-02-15".- Parameters:
date- Date to format.- Returns:
- Day name, or
nullifdateisnull. - See Also:
-
today
Returns the current date, formatted withformatDate(Date), for example"2026-02-15".- Returns:
- Current date as text.
-
date
Returns the current date of the server in the formatyyyy-MM-dd, for example"2026-02-15".- Returns:
- Current date as text.
-
time
Returns the current date and time of the server in the formatyyyy-MM-dd'T'HH:mm:ss, for example"2026-02-15T14:30:00".- Returns:
- Current date and time as text.
-
fileInfixTimestamp
Returns the current date and time in UTC in the formatyyyyMMdd_HHmmss, for example"20260215_143000". Use it to make file names unique.- Returns:
- Timestamp for a file name.
-
fileInfixTimestamp
Returns the given date and time in UTC in the formatyyyyMMdd_HHmmss, for example"20260215_143000". Use it to make file names unique.- Parameters:
date- Date and time to format. Must not benull.- Returns:
- Timestamp for a file name.
-
getTimePeriod
Returns the time period for a period name, such as2026-Q1. SeeCalendarUtilfor the supported formats.An invalid quarter, month or week number also returns
null, for example2026-Q5, or week 53 in a year that has only 52 weeks. This depends on the advanced configuration optioncalendarThrowOnInvalidTimePeriod, which istrueby default. When it is set tofalse, the behavior of versions before 16.0 applies: most invalid numbers still returnnull, but week 53 of some years that have only 52 weeks returns a one-day period instead.- Parameters:
periodAsString- Period name, such as2026,2026-Q1,2026-M01or2026-W05.- Returns:
- Matching time period, or
nullifperiodAsStringisnullor 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
nullor not valid, the unit isCalendarUtil.TimeUnit.DAY. If a date cannot be parsed, the matching date of the range isnull. ForCalendarUtil.TimeUnit.YEAR, the range does not start on January 1 (seeCalendarUtil.TimeUnit.startDate(Date)).- Parameters:
startDateAsString- Date inside the first period, for example2026-01-15. SeeparseDate(String).endDateAsString- Date inside the last period, ornull. If it isnull, the range has no end date.unitAsString- Name of aCalendarUtil.TimeUnit, for example"Month".- Returns:
- Time period, or
nullifstartDateAsStringisnull. - See Also:
-
newTimePeriod
Returns the single period of the given unit that contains the given date. For example,newTimePeriod("2026-01-15", "Month")returns the period2026-M01.It works like
getTimePeriodByDate(String, String), with these differences: if the unit name isnullor not valid, the unit isCalendarUtil.TimeUnit.DAY, and if the date isnull, the result isnull. ForCalendarUtil.TimeUnit.YEAR, the period does not start on January 1 (seeCalendarUtil.TimeUnit.startDate(Date)).- Parameters:
startDateAsString- Date inside the period, for example2026-01-15. SeeparseDate(String).unitAsString- Name of aCalendarUtil.TimeUnit, for example"Month".- Returns:
- Time period that contains the date, or
nullifstartDateAsStringisnull. - See Also:
-
getTimePeriodByDate
Returns the single period of the given unit that contains the given date. For example,getTimePeriodByDate("2026-01-15", "Month")returns the period2026-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, usegetTimePeriod(String)with the year, for examplegetTimePeriod("2026").- Parameters:
dateAsString- Date inside the period, for example2026-01-15. SeeparseDate(String).unitAsString- Name of aCalendarUtil.TimeUnit, for example"Month". Must be a valid unit name.- Returns:
- Time period that contains the date.
- See Also:
-
getTimePeriodName
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
nullifperiodisnull. - See Also:
-
getTimePeriodName
Returns the name of the period of the given unit that contains the given date, for example"2026-M02"for 2026-02-15 andCalendarUtil.TimeUnit.MONTH. SeegetYear(Date),getQuarter(Date),getMonth(Date),getWeek(Date)andgetDay(Date).- Parameters:
date- Date inside the period.unit- Unit of the period.- Returns:
- Period name, or
nullifdateorunitisnull.
-
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 aCalendarUtil.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 asgetTimePeriodNames(String, String, String), but returnsCalendarUtil.TimePeriodobjects.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 aCalendarUtil.TimeUnit, for example"Month".- Returns:
- Time periods, or
nullif 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 containsstartDate.The method moves
startDateforward by one unit at a time, as long as the moved date is not afterendDate, and returns the period of each date. IfstartDateis not the first day of its period, the period that containsendDatecan be missing. For example, for 2026-01-15 to 2026-03-01 andCalendarUtil.TimeUnit.MONTH, the result is January and February only. To be safe, pass the start date of a period asstartDate. ForCalendarUtil.TimeUnit.YEAR, the periods do not start on January 1 (seeCalendarUtil.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
nullor ifendDateis beforestartDate. - See Also:
-
getStartDate
Returns the start date of the time period with the given name.- Parameters:
period- Period name, such as2026-Q1.- Returns:
- First day of the period, or
nullifperiodisnullor cannot be parsed. - See Also:
-
getEndDate
Returns the end date of the time period with the given name.- Parameters:
period- Period name, such as2026-Q1.- Returns:
- Last day of the period, at the start of that day, or
nullifperiodisnullor cannot be parsed. - See Also:
-
add
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 as2026-M01.number- Number of units to move by. A negative number moves back.unit- Name of aCalendarUtil.TimeUnitto move by, for example"Month".- Returns:
- Name of the resulting period, or
nullifperiodorunitisnull, or ifperiodcannot be parsed. - Throws:
IllegalArgumentException- Ifunitis not a validCalendarUtil.TimeUnitname.- See Also:
-
getDaysDiff
Returns the number of days from the start ofperiod1to the end ofperiod2, 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
nullor cannot be parsed. - See Also:
-
parseTimePeriod
Returns the time period for a period name, such as2026-Q1. SeeCalendarUtilfor the supported formats. Text that is not a period name but a date or a date-time gives a one-day period. UnlikegetTimePeriod(String), this method throws an exception instead of returningnull.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 optioncalendarThrowOnInvalidTimePeriod, which istrueby default. When it is set tofalse, 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 as2026,2026-Q1,2026-M01,202601or2026-W05.- Returns:
- Matching time period. Never
null. - Throws:
ParseException- Ifperiodisnullor cannot be parsed.- See Also:
-
parseDate
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 ofparseDateTime(String). For an ambiguous date such as03/04/2026, the order of day and month follows the locale of the request.- Parameters:
dateAsString- Text to convert.- Returns:
- Matching date, or
nullif no format matches. - See Also:
-
parseDateTime
Converts text to a date-time, tryingyyyy-MM-dd HH:mm:ss, thenyyyy-MM-dd'T'HH:mm:ss, and then a flexible parser. For an ambiguous date such as03/04/2026, the order of day and month follows the locale of the request.- Parameters:
datetimeAsString- Text to convert.- Returns:
- Matching Joda-Time
DateTime, ornullif no format matches. - See Also:
-
formatDate
Formats a date with the date pattern of this instance,DEFAULT_DATE_PATTERNby default. The date is formatted in UTC. Ajava.sql.Dateis formatted as its local date, without a conversion to UTC.- Parameters:
date- Date to format. Must not benull.- Returns:
- Formatted date, for example
"2026-02-15".
-
formatDate
Formats a date with the date pattern of this instance,DEFAULT_DATE_PATTERNby default.- Parameters:
localDate- Date to format. Must not benull.- Returns:
- Formatted date, for example
"2026-02-15".
-
formatDateTime
Formats a date-time with the date-time pattern of this instance,DEFAULT_DATETIME_PATTERNby default. The date-time is formatted in UTC. Ajava.sql.Datehas no time, so only the date is formatted, with the date pattern.- Parameters:
date- Date-time to format. Must not benull.- Returns:
- Formatted date-time, for example
"2026-02-15T14:30:00".
-
formatDateTime
Formats a date-time with the date-time pattern of this instance,DEFAULT_DATETIME_PATTERNby default.- Parameters:
localDateTime- Date-time to format. Must not benull.- Returns:
- Formatted date-time, for example
"2026-02-15T14:30:00".
-
formatDateTime
Formats a Joda-TimeDateTimewith the date-time pattern of this instance,DEFAULT_DATETIME_PATTERNby default. The date-time is formatted in UTC.- Parameters:
dateTime- Date-time to format. Must not benull.- Returns:
- Formatted date-time, for example
"2026-02-15T14:30:00".
-