Class TimeCategory

java.lang.Object
org.apache.groovy.datetime.TimeCategory

public class TimeCategory extends Object
A java.time-based DSL for convenient date/time manipulation, the modern counterpart to the legacy groovy.time.TimeCategory. Numbers gain properties that produce Duration (time-based amounts) or Period (date-based amounts), which compose with the java.time types via their native arithmetic:
 import java.time.LocalDate
 import java.time.LocalDateTime
 use (org.apache.groovy.datetime.TimeCategory) {
     assert LocalDate.of(2000, 1, 1) + 2.months == LocalDate.of(2000, 3, 1)
     assert 2.days.ago instanceof LocalDate
     assert 1.hour.from.now instanceof LocalDateTime
 }
 

Time-based units (nanoseconds, milliseconds, seconds, minutes, hours) yield a Duration; date-based units (days, weeks, months, years) yield a Period. The two are not combined into a single amount (as java.time intends); instead they compose by chaining, e.g. someDateTime + 2.months + 3.hours.

Unlike the legacy DSL, .ago/.from.now never floor the time-of-day: a Duration resolves against LocalDateTime.now() (time preserved) and a Period against LocalDate.now() (date only, so no time component exists).

  • Constructor Details

    • TimeCategory

      public TimeCategory()
  • Method Details

    • getNanoseconds

      public static Duration getNanoseconds(Integer self)
      A Duration of self nanoseconds.
      Parameters:
      self - the number of nanoseconds
      Returns:
      that duration
    • getNanosecond

      public static Duration getNanosecond(Integer self)
      Same nanosecond duration as getNanoseconds(Integer).
      Parameters:
      self - the number of nanoseconds
      Returns:
      that duration
    • getMilliseconds

      public static Duration getMilliseconds(Integer self)
      A Duration of self milliseconds.
      Parameters:
      self - the number of milliseconds
      Returns:
      that duration
    • getMillisecond

      public static Duration getMillisecond(Integer self)
      Same millisecond duration as getMilliseconds(Integer).
      Parameters:
      self - the number of milliseconds
      Returns:
      that duration
    • getSeconds

      public static Duration getSeconds(Integer self)
      A Duration of self seconds.
      Parameters:
      self - the number of seconds
      Returns:
      that duration
    • getSecond

      public static Duration getSecond(Integer self)
      Same second duration as getSeconds(Integer).
      Parameters:
      self - the number of seconds
      Returns:
      that duration
    • getMinutes

      public static Duration getMinutes(Integer self)
      A Duration of self minutes.
      Parameters:
      self - the number of minutes
      Returns:
      that duration
    • getMinute

      public static Duration getMinute(Integer self)
      Same minute duration as getMinutes(Integer).
      Parameters:
      self - the number of minutes
      Returns:
      that duration
    • getHours

      public static Duration getHours(Integer self)
      A Duration of self hours.
      Parameters:
      self - the number of hours
      Returns:
      that duration
    • getHour

      public static Duration getHour(Integer self)
      Same hour duration as getHours(Integer).
      Parameters:
      self - the number of hours
      Returns:
      that duration
    • getDays

      public static Period getDays(Integer self)
      A Period of self days.
      Parameters:
      self - the number of days
      Returns:
      that period
    • getDay

      public static Period getDay(Integer self)
      Same day period as getDays(Integer).
      Parameters:
      self - the number of days
      Returns:
      that period
    • getWeeks

      public static Period getWeeks(Integer self)
      A Period of self weeks.
      Parameters:
      self - the number of weeks
      Returns:
      that period
    • getWeek

      public static Period getWeek(Integer self)
      Same week period as getWeeks(Integer).
      Parameters:
      self - the number of weeks
      Returns:
      that period
    • getMonths

      public static Period getMonths(Integer self)
      A Period of self months.
      Parameters:
      self - the number of months
      Returns:
      that period
    • getMonth

      public static Period getMonth(Integer self)
      Same month period as getMonths(Integer).
      Parameters:
      self - the number of months
      Returns:
      that period
    • getYears

      public static Period getYears(Integer self)
      A Period of self years.
      Parameters:
      self - the number of years
      Returns:
      that period
    • getYear

      public static Period getYear(Integer self)
      Same year period as getYears(Integer).
      Parameters:
      self - the number of years
      Returns:
      that period
    • getAgo

      public static LocalDateTime getAgo(Duration self)
      The LocalDateTime this time-based duration before now (time-of-day preserved).
    • getAgo

      public static LocalDate getAgo(Period self)
      The LocalDate this date-based period before today.
    • getFrom

      public static TimeCategory.From getFrom(Duration self)
      Helper for duration.from.now; the resulting temporal is a LocalDateTime.
    • getFrom

      public static TimeCategory.From getFrom(Period self)
      Helper for period.from.now; the resulting temporal is a LocalDate.