Enum Class CalendarUtil.TimeUnit

All Implemented Interfaces:
Serializable, Comparable<CalendarUtil.TimeUnit>, Constable
Enclosing class:
CalendarUtil

public static enum CalendarUtil.TimeUnit extends Enum<CalendarUtil.TimeUnit>
Unit of a CalendarUtil.TimePeriod: day, week, month, quarter or year.

Methods that take a unit as a string accept the constant name in any letter case, for example "Month" or "MONTH".

  • Enum Constant Details

    • DAY

      public static final CalendarUtil.TimeUnit DAY
      One calendar day.
    • WEEK

      public static final CalendarUtil.TimeUnit WEEK
      One calendar week. The first day of the week and the minimal number of days in the first week of the year follow the calendar settings of the partition.
    • MONTH

      public static final CalendarUtil.TimeUnit MONTH
      One calendar month.
    • QUARTER

      public static final CalendarUtil.TimeUnit QUARTER
      Three calendar months, starting in January, April, July or October.
    • YEAR

      public static final CalendarUtil.TimeUnit YEAR
      One year. A period named by its year, such as 2026, is the calendar year; a period created from a date starts on that date (see startDate(Date)).
  • Method Details

    • values

      public static CalendarUtil.TimeUnit[] values()
      Returns an array containing the constants of this enum class, in the order they are declared.
      Returns:
      an array containing the constants of this enum class, in the order they are declared
    • valueOf

      public static CalendarUtil.TimeUnit valueOf(String name)
      Returns the enum constant of this class with the specified name. The string must match exactly an identifier used to declare an enum constant in this class. (Extraneous whitespace characters are not permitted.)
      Parameters:
      name - the name of the enum constant to be returned.
      Returns:
      the enum constant with the specified name
      Throws:
      IllegalArgumentException - if this enum class has no constant with the specified name
      NullPointerException - if the argument is null
    • getName

      public String getName()
      Returns the display name of the unit, for example "Month".
      Returns:
      Display name of the unit.
    • startDate

      public Date startDate(Date date)
      Returns the start of the period of this unit that contains the given date. For example, for MONTH and the date 2026-02-15, the result is 2026-02-01 at the start of the day. For WEEK, the week starts on the first day of the week set for the partition.

      For YEAR, the date is returned unchanged, including the time of day. It is not moved to January 1.

      Parameters:
      date - Date inside the period.
      Returns:
      Start date of the period, or null if date is null.
    • endDate

      public Date endDate(Date date)
      Returns the last day of the period of this unit that contains the given date, at the start of that day. For example, for MONTH and the date 2026-02-15, the result is 2026-02-28.

      For YEAR, the result is the day before the same date one year later, with the time of day of the given date. For example, for 2026-06-15 the result is 2027-06-14 (see startDate(Date)).

      Parameters:
      date - Date inside the period.
      Returns:
      End date of the period, or null if date is null.
    • offsetDate

      public Date offsetDate(Date date, int offset)
      Moves a date by a number of units of this type. A negative offset moves the date back. A QUARTER moves the date by three months.
      Parameters:
      date - Date to move.
      offset - Number of units to move the date by.
      Returns:
      Moved date. Returns date unchanged if it is null or if offset is 0.
    • toString

      public String toString()
      Returns the display name of the unit, the same as getName().
      Overrides:
      toString in class Enum<CalendarUtil.TimeUnit>
      Returns:
      Display name of the unit, for example "Month".
    • get

      public static CalendarUtil.TimeUnit get(String unit, CalendarUtil.TimeUnit defaultValue)
      Returns the unit with the given name, ignoring letter case.
      Parameters:
      unit - Name of the unit, for example "Month" or "MONTH".
      defaultValue - Unit to return when unit is null or not a valid unit name.
      Returns:
      Matching unit, or defaultValue if there is no match.
    • get

      public static CalendarUtil.TimeUnit get(String unit)
      Returns the unit with the given name, ignoring letter case.
      Parameters:
      unit - Name of the unit, for example "Month" or "MONTH".
      Returns:
      Matching unit, or null if unit is null or not a valid unit name.