Class BaseDuration

java.lang.Object
org.apache.groovy.dateutil.BaseDuration
All Implemented Interfaces:
Comparable<BaseDuration>
Direct Known Subclasses:
DatumDependentDuration, Duration

public abstract class BaseDuration extends Object implements Comparable<BaseDuration>
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) use ChronoUnit's estimated durations rather than resolving against new Date() ("now"), so the result no longer depends on when it is called.
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Helper for computing dates relative to the current time.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final int
    Whole days stored in this duration.
    protected final int
    Hours stored in this duration.
    protected final int
    Milliseconds stored in this duration.
    protected final int
    Minutes stored in this duration.
    protected final int
    Whole months stored in this duration.
    protected final int
    Seconds stored in this duration.
    protected final int
    Whole years stored in this duration.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    BaseDuration(int days, int hours, int minutes, int seconds, int millis)
    Stores the day and time components.
    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.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Orders durations by toMilliseconds(), so years and months use the ChronoUnit estimates and the shorter units are exact.
    The date this duration ago.
    int
    Returns the stored day count.
    Helper for computing dates relative to the current time.
    int
    Returns the stored hour count.
    int
    Returns the stored millisecond count.
    int
    Returns the stored minute count.
    int
    Returns the stored month count.
    int
    Returns the stored second count.
    int
    Returns the stored year count.
    plus(Date date)
    Adds this duration to the supplied date.
    long
    Converts this duration to milliseconds.
    Lists each non-zero component.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • years

      protected final int years
      Whole years stored in this duration.
    • months

      protected final int months
      Whole months stored in this duration.
    • days

      protected final int days
      Whole days stored in this duration.
    • hours

      protected final int hours
      Hours stored in this duration.
    • minutes

      protected final int minutes
      Minutes stored in this duration.
    • seconds

      protected final int seconds
      Seconds stored in this duration.
    • millis

      protected final int millis
      Milliseconds 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 years
      months - whole months
      days - whole days
      hours - hours
      minutes - minutes
      seconds - seconds
      millis - 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 days
      hours - hours
      minutes - minutes
      seconds - seconds
      millis - 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

      public Date plus(Date date)
      Adds this duration to the supplied date.
    • toMilliseconds

      public long toMilliseconds()
      Converts this duration to milliseconds.

      DEQUIRKED (B): deterministic. Years and months use ChronoUnit estimates (a year is exactly 12 estimated months, so 1.year == 12.months); days and below are exact assuming 24-hour days. The result never depends on the current date.

    • getAgo

      public 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

      public BaseDuration.From getFrom()
      Helper for computing dates relative to the current time. DEQUIRKED (A): time-of-day preserved.
    • compareTo

      public int compareTo(BaseDuration other)
      Orders durations by toMilliseconds(), so years and months use the ChronoUnit estimates and the shorter units are exact.
      Specified by:
      compareTo in interface Comparable<BaseDuration>
      Parameters:
      other - the duration to compare with
      Returns:
      a negative, zero, or positive value when this duration is shorter, equal, or longer
    • toString

      public String 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".
      Overrides:
      toString in class Object
      Returns:
      the component list