Package org.apache.groovy.dateutil
Class BaseDuration
java.lang.Object
org.apache.groovy.dateutil.BaseDuration
- All Implemented Interfaces:
Comparable<BaseDuration>
- Direct Known Subclasses:
DatumDependentDuration,Duration
Base class for date and time durations (dequirked,
java.util.Date-flavored).
This is the B1 prototype: the same value-class model as the legacy
groovy.time.BaseDuration, but with the historical quirks removed:
- ago / from.now no longer zero the time-of-day for date-based durations — the behavior is now uniform across all durations (time is preserved), so a single implementation lives here rather than divergent overrides per subclass.
toMilliseconds()is deterministic — datum-dependent amounts (years, months) useChronoUnit's estimated durations rather than resolving againstnew Date()("now"), so the result no longer depends on when it is called.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classHelper for computing dates relative to the current time. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final intWhole days stored in this duration.protected final intHours stored in this duration.protected final intMilliseconds stored in this duration.protected final intMinutes stored in this duration.protected final intWhole months stored in this duration.protected final intSeconds stored in this duration.protected final intWhole years stored in this duration. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedBaseDuration(int days, int hours, int minutes, int seconds, int millis) Stores the day and time components.protectedBaseDuration(int years, int months, int days, int hours, int minutes, int seconds, int millis) Stores every calendar and time component of the duration. -
Method Summary
Modifier and TypeMethodDescriptionintcompareTo(BaseDuration other) Orders durations bytoMilliseconds(), so years and months use theChronoUnitestimates and the shorter units are exact.getAgo()The date this duration ago.intgetDays()Returns the stored day count.getFrom()Helper for computing dates relative to the current time.intgetHours()Returns the stored hour count.intReturns the stored millisecond count.intReturns the stored minute count.intReturns the stored month count.intReturns the stored second count.intgetYears()Returns the stored year count.Adds this duration to the supplied date.longConverts this duration to milliseconds.toString()Lists each non-zero component.
-
Field Details
-
years
protected final int yearsWhole years stored in this duration. -
months
protected final int monthsWhole months stored in this duration. -
days
protected final int daysWhole days stored in this duration. -
hours
protected final int hoursHours stored in this duration. -
minutes
protected final int minutesMinutes stored in this duration. -
seconds
protected final int secondsSeconds stored in this duration. -
millis
protected final int millisMilliseconds stored in this duration.
-
-
Constructor Details
-
BaseDuration
protected BaseDuration(int years, int months, int days, int hours, int minutes, int seconds, int millis) Stores every calendar and time component of the duration.- Parameters:
years- whole yearsmonths- whole monthsdays- whole dayshours- hoursminutes- minutesseconds- secondsmillis- milliseconds
-
BaseDuration
protected BaseDuration(int days, int hours, int minutes, int seconds, int millis) Stores the day and time components. Years and months are zero.- Parameters:
days- whole dayshours- hoursminutes- minutesseconds- secondsmillis- milliseconds
-
-
Method Details
-
getYears
public int getYears()Returns the stored year count.- Returns:
- the stored year count
-
getMonths
public int getMonths()Returns the stored month count.- Returns:
- the stored month count
-
getDays
public int getDays()Returns the stored day count.- Returns:
- the stored day count
-
getHours
public int getHours()Returns the stored hour count.- Returns:
- the stored hour count
-
getMinutes
public int getMinutes()Returns the stored minute count.- Returns:
- the stored minute count
-
getSeconds
public int getSeconds()Returns the stored second count.- Returns:
- the stored second count
-
getMillis
public int getMillis()Returns the stored millisecond count.- Returns:
- the stored millisecond count
-
plus
Adds this duration to the supplied date. -
toMilliseconds
public long toMilliseconds()Converts this duration to milliseconds.DEQUIRKED (B): deterministic. Years and months use
ChronoUnitestimates (a year is exactly 12 estimated months, so1.year == 12.months); days and below are exact assuming 24-hour days. The result never depends on the current date. -
getAgo
The date this duration ago.DEQUIRKED (A): the time-of-day is preserved (no flooring to midnight), uniformly for all duration kinds.
-
getFrom
Helper for computing dates relative to the current time. DEQUIRKED (A): time-of-day preserved. -
compareTo
Orders durations bytoMilliseconds(), so years and months use theChronoUnitestimates and the shorter units are exact.- Specified by:
compareToin interfaceComparable<BaseDuration>- Parameters:
other- the duration to compare with- Returns:
- a negative, zero, or positive value when this duration is shorter, equal, or longer
-
toString
Lists each non-zero component. Seconds and milliseconds are combined into one fractional-seconds term, with the millisecond part padded to three digits. A duration whose components are all zero is"0".
-