Class LocalDate


  • public final class LocalDate
    extends Object
    A date with no time components, no time zone, in the ISO 8601 calendar.

    Note that ISO 8601 has a number of differences with the default gregorian calendar used in Java:

    • it uses a proleptic gregorian calendar, meaning that it's gregorian indefinitely back in the past (there is no gregorian change);
    • there is a year 0.

    This class implements these differences, so that year/month/day fields match exactly the ones in CQL string literals.

    Since:
    2.2
    • Method Detail

      • fromDaysSinceEpoch

        public static LocalDate fromDaysSinceEpoch​(int daysSinceEpoch)
        Builds a new instance from a number of days since January 1st, 1970 GMT.
        Parameters:
        daysSinceEpoch - the number of days.
        Returns:
        the new instance.
      • fromMillisSinceEpoch

        public static LocalDate fromMillisSinceEpoch​(long millisSinceEpoch)
                                              throws IllegalArgumentException
        Builds a new instance from a number of milliseconds since January 1st, 1970 GMT. Note that if the given number does not correspond to a whole number of days, it will be rounded towards 0.
        Parameters:
        millisSinceEpoch - the number of milliseconds since January 1st, 1970 GMT.
        Returns:
        the new instance.
        Throws:
        IllegalArgumentException - if the date is not in the range [-5877641-06-23; 5881580-07-11].
      • fromYearMonthDay

        public static LocalDate fromYearMonthDay​(int year,
                                                 int month,
                                                 int dayOfMonth)
        Builds a new instance from a year/month/day specification.

        This method is not lenient, i.e. '2014-12-32' will not be treated as '2015-01-01', but instead throw an IllegalArgumentException.

        Parameters:
        year - the year in ISO format (see this class's Javadoc).
        month - the month. It is 1-based (e.g. 1 for January).
        dayOfMonth - the day of the month.
        Returns:
        the new instance.
        Throws:
        IllegalArgumentException - if the corresponding date does not exist in the ISO8601 calendar.
      • getDaysSinceEpoch

        public int getDaysSinceEpoch()
        Returns the number of days since January 1st, 1970 GMT.
        Returns:
        the number of days.
      • getMillisSinceEpoch

        public long getMillisSinceEpoch()
        Returns the number of milliseconds since January 1st, 1970 GMT.
        Returns:
        the number of milliseconds.
      • getYear

        public int getYear()
        Returns the year.
        Returns:
        the year.
      • getMonth

        public int getMonth()
        Returns the month.
        Returns:
        the month. It is 1-based, e.g. 1 for January.
      • getDay

        public int getDay()
        Returns the day in the month.
        Returns:
        the day in the month.
      • add

        public LocalDate add​(int field,
                             int amount)
        Return a new LocalDate with the specified (signed) amount of time added to (or subtracted from) the given Calendar field, based on the calendar's rules.

        Note that adding any amount to a field smaller than Calendar.DAY_OF_MONTH will remain without effect, as this class does not keep time components.

        See Calendar javadocs for more information.

        Parameters:
        field - a Calendar field to modify.
        amount - the amount of date or time to be added to the field.
        Returns:
        a new LocalDate with the specified (signed) amount of time added to (or subtracted from) the given Calendar field.
        Throws:
        IllegalArgumentException - if the new date is not in the range [-5877641-06-23; 5881580-07-11].
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object