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 Periodstatic Periodstatic TimeCategory.FromHelper forduration.from.now; the resulting temporal is aLocalDateTime.static TimeCategory.FromHelper forperiod.from.now; the resulting temporal is aLocalDate.static Durationstatic Durationstatic DurationgetMillisecond(Integer self) static DurationgetMilliseconds(Integer self) static Durationstatic DurationgetMinutes(Integer self) static Periodstatic Periodstatic DurationgetNanosecond(Integer self) static DurationgetNanoseconds(Integer self) static Durationstatic DurationgetSeconds(Integer self) static Periodstatic Periodstatic Periodstatic Period
-
Constructor Details
-
TimeCategory
public TimeCategory()
-
-
Method Details
-
getNanoseconds
-
getNanosecond
-
getMilliseconds
-
getMillisecond
-
getSeconds
-
getSecond
-
getMinutes
-
getMinute
-
getHours
-
getHour
-
getDays
-
getDay
-
getWeeks
-
getWeek
-
getMonths
-
getMonth
-
getYears
-
getYear
-
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.
-