Package org.apache.groovy.datetime
Class TimeCategory
java.lang.Object
org.apache.groovy.datetime.TimeCategory
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).
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic LocalDateTimeTheLocalDateTimethis time-based duration before now (time-of-day preserved).static LocalDateTheLocalDatethis date-based period before today.static PeriodSame day period asgetDays(Integer).static PeriodAPeriodofselfdays.static TimeCategory.FromHelper forduration.from.now; the resulting temporal is aLocalDateTime.static TimeCategory.FromHelper forperiod.from.now; the resulting temporal is aLocalDate.static DurationSame hour duration asgetHours(Integer).static DurationADurationofselfhours.static DurationgetMillisecond(Integer self) Same millisecond duration asgetMilliseconds(Integer).static DurationgetMilliseconds(Integer self) ADurationofselfmilliseconds.static DurationSame minute duration asgetMinutes(Integer).static DurationgetMinutes(Integer self) ADurationofselfminutes.static PeriodSame month period asgetMonths(Integer).static PeriodAPeriodofselfmonths.static DurationgetNanosecond(Integer self) Same nanosecond duration asgetNanoseconds(Integer).static DurationgetNanoseconds(Integer self) ADurationofselfnanoseconds.static DurationSame second duration asgetSeconds(Integer).static DurationgetSeconds(Integer self) ADurationofselfseconds.static PeriodSame week period asgetWeeks(Integer).static PeriodAPeriodofselfweeks.static PeriodSame year period asgetYears(Integer).static PeriodAPeriodofselfyears.
-
Constructor Details
-
TimeCategory
public TimeCategory()
-
-
Method Details
-
getNanoseconds
ADurationofselfnanoseconds.- Parameters:
self- the number of nanoseconds- Returns:
- that duration
-
getNanosecond
Same nanosecond duration asgetNanoseconds(Integer).- Parameters:
self- the number of nanoseconds- Returns:
- that duration
-
getMilliseconds
ADurationofselfmilliseconds.- Parameters:
self- the number of milliseconds- Returns:
- that duration
-
getMillisecond
Same millisecond duration asgetMilliseconds(Integer).- Parameters:
self- the number of milliseconds- Returns:
- that duration
-
getSeconds
ADurationofselfseconds.- Parameters:
self- the number of seconds- Returns:
- that duration
-
getSecond
Same second duration asgetSeconds(Integer).- Parameters:
self- the number of seconds- Returns:
- that duration
-
getMinutes
ADurationofselfminutes.- Parameters:
self- the number of minutes- Returns:
- that duration
-
getMinute
Same minute duration asgetMinutes(Integer).- Parameters:
self- the number of minutes- Returns:
- that duration
-
getHours
ADurationofselfhours.- Parameters:
self- the number of hours- Returns:
- that duration
-
getHour
Same hour duration asgetHours(Integer).- Parameters:
self- the number of hours- Returns:
- that duration
-
getDays
APeriodofselfdays.- Parameters:
self- the number of days- Returns:
- that period
-
getDay
Same day period asgetDays(Integer).- Parameters:
self- the number of days- Returns:
- that period
-
getWeeks
APeriodofselfweeks.- Parameters:
self- the number of weeks- Returns:
- that period
-
getWeek
Same week period asgetWeeks(Integer).- Parameters:
self- the number of weeks- Returns:
- that period
-
getMonths
APeriodofselfmonths.- Parameters:
self- the number of months- Returns:
- that period
-
getMonth
Same month period asgetMonths(Integer).- Parameters:
self- the number of months- Returns:
- that period
-
getYears
APeriodofselfyears.- Parameters:
self- the number of years- Returns:
- that period
-
getYear
Same year period asgetYears(Integer).- Parameters:
self- the number of years- Returns:
- that period
-
getAgo
TheLocalDateTimethis time-based duration before now (time-of-day preserved). -
getAgo
TheLocalDatethis date-based period before today. -
getFrom
Helper forduration.from.now; the resulting temporal is aLocalDateTime. -
getFrom
Helper forperiod.from.now; the resulting temporal is aLocalDate.
-