Class LocalDateTime

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      (package private) static class  LocalDateTime.Rule
      Rule implementation.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private LocalDate date
      The date part.
      private static long serialVersionUID
      A serialization identifier for this class.
      private LocalTime time
      The time part.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      LocalDate adjustDate​(LocalDate date)
      Adjusts a date to have the value of the date part of this object.
      LocalTime adjustTime​(LocalTime time)
      Adjusts a time to have the value of the time part of this object.
      OffsetDateTime atOffset​(ZoneOffset offset)
      Returns an offset date-time formed from this date-time and the specified offset.
      ZonedDateTime atZone​(TimeZone zone)
      Returns a zoned date-time formed from this date-time and the specified time-zone.
      ZonedDateTime atZone​(TimeZone zone, ZoneResolver resolver)
      Returns a zoned date-time formed from this date-time and the specified time-zone taking control of what occurs in time-line gaps and overlaps.
      int compareTo​(LocalDateTime other)
      Compares this LocalDateTime to another date-time.
      (package private) static LocalDateTime create​(long localSeconds, int nanoOfSecond)
      Obtains an instance of LocalDateTime using seconds from the local epoch of 1970-01-01T00:00:00.
      boolean equals​(java.lang.Object other)
      Checks if this LocalDateTime is equal to the specified date-time.
      <T> T get​(CalendricalRule<T> rule)
      Gets the value of the specified calendrical rule.
      ISOChronology getChronology()
      Gets the chronology that this date-time uses, which is the ISO calendar system.
      int getDayOfMonth()
      Gets the day-of-month field.
      DayOfWeek getDayOfWeek()
      Gets the day-of-week field, which is an enum DayOfWeek.
      int getDayOfYear()
      Gets the day-of-year field.
      int getHourOfDay()
      Gets the hour-of-day field.
      int getMinuteOfHour()
      Gets the minute-of-hour field.
      MonthOfYear getMonthOfYear()
      Gets the month-of-year field, which is an enum MonthOfYear.
      int getNanoOfSecond()
      Gets the nano-of-second field.
      int getSecondOfMinute()
      Gets the second-of-minute field.
      int getYear()
      Gets the year field.
      int hashCode()
      A hash code for this LocalDateTime.
      boolean isAfter​(LocalDateTime other)
      Checks if this LocalDateTime is after the specified date-time.
      boolean isBefore​(LocalDateTime other)
      Checks if this LocalDateTime is before the specified date-time.
      boolean isLeapYear()
      Checks if the year is a leap year, according to the ISO proleptic calendar system rules.
      boolean matches​(CalendricalMatcher matcher)
      Checks whether this LocalDateTime matches the specified matcher.
      boolean matchesCalendrical​(Calendrical calendrical)
      Checks if the date-time extracted from the calendrical matches this.
      LocalDateTime minus​(PeriodProvider periodProvider)
      Returns a copy of this LocalDateTime with the specified period subtracted.
      LocalDateTime minus​(Duration duration)
      Returns a copy of this LocalDateTime with the specified duration subtracted.
      LocalDateTime minusDays​(long days)
      Returns a copy of this LocalDateTime with the specified period in days subtracted.
      LocalDateTime minusHours​(long hours)
      Returns a copy of this LocalDateTime with the specified period in hours subtracted.
      LocalDateTime minusMinutes​(long minutes)
      Returns a copy of this LocalDateTime with the specified period in minutes subtracted.
      LocalDateTime minusMonths​(long months)
      Returns a copy of this LocalDateTime with the specified period in months subtracted.
      LocalDateTime minusMonths​(long months, DateResolver dateResolver)
      Returns a copy of this LocalDateTime with the specified period in months subtracted.
      LocalDateTime minusNanos​(long nanos)
      Returns a copy of this LocalDateTime with the specified period in nanoseconds subtracted.
      LocalDateTime minusSeconds​(long seconds)
      Returns a copy of this LocalDateTime with the specified period in seconds subtracted.
      LocalDateTime minusWeeks​(long weeks)
      Returns a copy of this LocalDateTime with the specified period in weeks subtracted.
      LocalDateTime minusYears​(long years)
      Returns a copy of this LocalDateTime with the specified period in years subtracted.
      LocalDateTime minusYears​(long years, DateResolver dateResolver)
      Returns a copy of this LocalDateTime with the specified period in years subtracted.
      static LocalDateTime now()
      Obtains the current date-time from the system clock in the default time-zone.
      static LocalDateTime now​(Clock clock)
      Obtains the current date-time from the specified clock.
      static LocalDateTime of​(int year, int monthOfYear, int dayOfMonth, int hourOfDay, int minuteOfHour)
      Obtains an instance of LocalDateTime from year, month, day, hour and minute, setting the second and nanosecond to zero.
      static LocalDateTime of​(int year, int monthOfYear, int dayOfMonth, int hourOfDay, int minuteOfHour, int secondOfMinute)
      Obtains an instance of LocalDateTime from year, month, day, hour, minute and second, setting the nanosecond to zero.
      static LocalDateTime of​(int year, int monthOfYear, int dayOfMonth, int hourOfDay, int minuteOfHour, int secondOfMinute, int nanoOfSecond)
      Obtains an instance of LocalDateTime from year, month, day, hour, minute, second and nanosecond.
      static LocalDateTime of​(int year, MonthOfYear monthOfYear, int dayOfMonth, int hourOfDay, int minuteOfHour)
      Obtains an instance of LocalDateTime from year, month, day, hour and minute, setting the second and nanosecond to zero.
      static LocalDateTime of​(int year, MonthOfYear monthOfYear, int dayOfMonth, int hourOfDay, int minuteOfHour, int secondOfMinute)
      Obtains an instance of LocalDateTime from year, month, day, hour, minute and second, setting the nanosecond to zero.
      static LocalDateTime of​(int year, MonthOfYear monthOfYear, int dayOfMonth, int hourOfDay, int minuteOfHour, int secondOfMinute, int nanoOfSecond)
      Obtains an instance of LocalDateTime from year, month, day, hour, minute, second and nanosecond.
      static LocalDateTime of​(DateProvider dateProvider, TimeProvider timeProvider)
      Obtains an instance of LocalDateTime from a date and time.
      static LocalDateTime of​(DateTimeProvider dateTimeProvider)
      Obtains an instance of LocalTime from a date-time provider.
      static LocalDateTime ofMidnight​(int year, int monthOfYear, int dayOfMonth)
      Obtains an instance of LocalDateTime from year, month and day with the time set to midnight at the start of day.
      static LocalDateTime ofMidnight​(int year, MonthOfYear monthOfYear, int dayOfMonth)
      Obtains an instance of LocalDateTime from year, month and day with the time set to midnight at the start of day.
      static LocalDateTime ofMidnight​(DateProvider dateProvider)
      Obtains an instance of LocalDateTime from a date with the time set to midnight at the start of day.
      static LocalDateTime parse​(java.lang.String text)
      Obtains an instance of LocalDateTime from a text string such as 2007-12-03T10:15:30.
      static LocalDateTime parse​(java.lang.String text, DateTimeFormatter formatter)
      Obtains an instance of LocalDateTime from a text string using a specific formatter.
      LocalDateTime plus​(PeriodProvider periodProvider)
      Returns a copy of this LocalDateTime with the specified period added.
      LocalDateTime plus​(Duration duration)
      Returns a copy of this LocalDateTime with the specified duration added.
      LocalDateTime plusDays​(long days)
      Returns a copy of this LocalDateTime with the specified period in days added.
      LocalDateTime plusHours​(long hours)
      Returns a copy of this LocalDateTime with the specified period in hours added.
      LocalDateTime plusMinutes​(long minutes)
      Returns a copy of this LocalDateTime with the specified period in minutes added.
      LocalDateTime plusMonths​(long months)
      Returns a copy of this LocalDateTime with the specified period in months added.
      LocalDateTime plusMonths​(long months, DateResolver dateResolver)
      Returns a copy of this LocalDateTime with the specified period in months added.
      LocalDateTime plusNanos​(long nanos)
      Returns a copy of this LocalDateTime with the specified period in nanoseconds added.
      LocalDateTime plusSeconds​(long seconds)
      Returns a copy of this LocalDateTime with the specified period in seconds added.
      LocalDateTime plusWeeks​(long weeks)
      Returns a copy of this LocalDateTime with the specified period in weeks added.
      LocalDateTime plusYears​(long years)
      Returns a copy of this LocalDateTime with the specified period in years added.
      LocalDateTime plusYears​(long years, DateResolver dateResolver)
      Returns a copy of this LocalDateTime with the specified period in years added.
      static CalendricalRule<LocalDateTime> rule()
      Gets the rule for LocalDateTime.
      LocalDate toLocalDate()
      Converts this date-time to a LocalDate.
      LocalDateTime toLocalDateTime()
      Converts this date-time to a LocalDateTime, trivially returning this.
      LocalTime toLocalTime()
      Converts this date-time to a LocalTime.
      java.lang.String toString()
      Outputs this date-time as a String, such as 2007-12-03T10:15:30.
      java.lang.String toString​(DateTimeFormatter formatter)
      Outputs this date-time as a String using the formatter.
      LocalDateTime with​(DateAdjuster adjuster)
      Returns a copy of this LocalDateTime with the date altered using the adjuster.
      private LocalDateTime with​(LocalDate newDate, LocalTime newTime)
      Returns a copy of this date-time with the new date and time, checking to see if a new object is in fact required.
      LocalDateTime with​(MonthOfYear monthOfYear)
      Returns a copy of this LocalDateTime with the month-of-year altered.
      LocalDateTime with​(MonthOfYear monthOfYear, DateResolver dateResolver)
      Returns a copy of this LocalDateTime with the month-of-year altered.
      LocalDateTime with​(TimeAdjuster adjuster)
      Returns a copy of this LocalDateTime with the time altered using the adjuster.
      LocalDateTime withDate​(int year, int monthOfYear, int dayOfMonth)
      Returns a copy of this LocalDateTime with the date values altered.
      LocalDateTime withDate​(int year, MonthOfYear monthOfYear, int dayOfMonth)
      Returns a copy of this LocalDateTime with the date values altered.
      LocalDateTime withDayOfMonth​(int dayOfMonth)
      Returns a copy of this LocalDateTime with the day-of-month altered.
      LocalDateTime withDayOfMonth​(int dayOfMonth, DateResolver dateResolver)
      Returns a copy of this LocalDateTime with the day-of-month altered.
      LocalDateTime withDayOfYear​(int dayOfYear)
      Returns a copy of this LocalDateTime with the day-of-year altered.
      LocalDateTime withHourOfDay​(int hourOfDay)
      Returns a copy of this LocalDateTime with the hour-of-day value altered.
      LocalDateTime withMinuteOfHour​(int minuteOfHour)
      Returns a copy of this LocalDateTime with the minute-of-hour value altered.
      LocalDateTime withMonthOfYear​(int monthOfYear)
      Returns a copy of this LocalDateTime with the month-of-year altered.
      LocalDateTime withMonthOfYear​(int monthOfYear, DateResolver dateResolver)
      Returns a copy of this LocalDateTime with the month-of-year altered.
      LocalDateTime withNanoOfSecond​(int nanoOfSecond)
      Returns a copy of this LocalDateTime with the nano-of-second value altered.
      LocalDateTime withSecondOfMinute​(int secondOfMinute)
      Returns a copy of this LocalDateTime with the second-of-minute value altered.
      LocalDateTime withTime​(int hourOfDay, int minuteOfHour)
      Returns a copy of this LocalDateTime with the time values altered.
      LocalDateTime withTime​(int hourOfDay, int minuteOfHour, int secondOfMinute)
      Returns a copy of this LocalDateTime with the time values altered.
      LocalDateTime withTime​(int hourOfDay, int minuteOfHour, int secondOfMinute, int nanoOfSecond)
      Returns a copy of this LocalDateTime with the time values altered.
      LocalDateTime withYear​(int year)
      Returns a copy of this LocalDateTime with the year altered.
      LocalDateTime withYear​(int year, DateResolver dateResolver)
      Returns a copy of this LocalDateTime with the year altered.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • serialVersionUID

        private static final long serialVersionUID
        A serialization identifier for this class.
        See Also:
        Constant Field Values
      • date

        private final LocalDate date
        The date part.
      • time

        private final LocalTime time
        The time part.
    • Constructor Detail

      • LocalDateTime

        private LocalDateTime​(LocalDate date,
                              LocalTime time)
        Constructor.
        Parameters:
        date - the date part of the date-time, not null
        time - the time part of the date-time, not null
    • Method Detail

      • now

        public static LocalDateTime now()
        Obtains the current date-time from the system clock in the default time-zone.

        This will query the system clock in the default time-zone to obtain the current date-time.

        Using this method will prevent the ability to use an alternate clock for testing because the clock is hard-coded.

        Returns:
        the current date-time using the system clock, never null
      • now

        public static LocalDateTime now​(Clock clock)
        Obtains the current date-time from the specified clock.

        This will query the specified clock to obtain the current date-time. Using this method allows the use of an alternate clock for testing. The alternate clock may be introduced using dependency injection.

        Parameters:
        clock - the clock to use, not null
        Returns:
        the current date-time, never null
      • create

        static LocalDateTime create​(long localSeconds,
                                    int nanoOfSecond)
        Obtains an instance of LocalDateTime using seconds from the local epoch of 1970-01-01T00:00:00.

        The nanosecond field is set to zero.

        Parameters:
        localSeconds - the number of seconds from the local epoch of 1970-01-01T00:00:00
        nanoOfSecond - the nanosecond within the second, from 0 to 999,999,999
        Returns:
        the local date-time, never null
        Throws:
        CalendarConversionException - if the instant exceeds the supported date range
      • ofMidnight

        public static LocalDateTime ofMidnight​(int year,
                                               MonthOfYear monthOfYear,
                                               int dayOfMonth)
        Obtains an instance of LocalDateTime from year, month and day with the time set to midnight at the start of day.

        The day must be valid for the year and month or an exception will be thrown.

        The time fields will be set to zero by this factory method.

        Parameters:
        year - the year to represent, from MIN_YEAR to MAX_YEAR
        monthOfYear - the month-of-year to represent, not null
        dayOfMonth - the day-of-month to represent, from 1 to 31
        Returns:
        the local date-time, never null
        Throws:
        IllegalCalendarFieldValueException - if the value of any field is out of range
        InvalidCalendarFieldException - if the day-of-month is invalid for the month-year
      • ofMidnight

        public static LocalDateTime ofMidnight​(int year,
                                               int monthOfYear,
                                               int dayOfMonth)
        Obtains an instance of LocalDateTime from year, month and day with the time set to midnight at the start of day.

        The day must be valid for the year and month or an exception will be thrown.

        The time fields will be set to zero by this factory method.

        Parameters:
        year - the year to represent, from MIN_YEAR to MAX_YEAR
        monthOfYear - the month-of-year to represent, from 1 (January) to 12 (December)
        dayOfMonth - the day-of-month to represent, from 1 to 31
        Returns:
        the local date-time, never null
        Throws:
        IllegalCalendarFieldValueException - if the value of any field is out of range
        InvalidCalendarFieldException - if the day-of-month is invalid for the month-year
      • ofMidnight

        public static LocalDateTime ofMidnight​(DateProvider dateProvider)
        Obtains an instance of LocalDateTime from a date with the time set to midnight at the start of day.

        The day must be valid for the year and month or an exception will be thrown.

        The time fields will be set to zero by this factory method.

        Parameters:
        dateProvider - the date provider to use, not null
        Returns:
        the local date-time, never null
      • of

        public static LocalDateTime of​(int year,
                                       MonthOfYear monthOfYear,
                                       int dayOfMonth,
                                       int hourOfDay,
                                       int minuteOfHour)
        Obtains an instance of LocalDateTime from year, month, day, hour and minute, setting the second and nanosecond to zero.

        The day must be valid for the year and month or an exception will be thrown.

        The second and nanosecond fields will be set to zero by this factory method.

        Parameters:
        year - the year to represent, from MIN_YEAR to MAX_YEAR
        monthOfYear - the month-of-year to represent, not null
        dayOfMonth - the day-of-month to represent, from 1 to 31
        hourOfDay - the hour-of-day to represent, from 0 to 23
        minuteOfHour - the minute-of-hour to represent, from 0 to 59
        Returns:
        the local date-time, never null
        Throws:
        IllegalCalendarFieldValueException - if the value of any field is out of range
        InvalidCalendarFieldException - if the day-of-month is invalid for the month-year
      • of

        public static LocalDateTime of​(int year,
                                       MonthOfYear monthOfYear,
                                       int dayOfMonth,
                                       int hourOfDay,
                                       int minuteOfHour,
                                       int secondOfMinute)
        Obtains an instance of LocalDateTime from year, month, day, hour, minute and second, setting the nanosecond to zero.

        The day must be valid for the year and month or an exception will be thrown.

        The nanosecond field will be set to zero by this factory method.

        Parameters:
        year - the year to represent, from MIN_YEAR to MAX_YEAR
        monthOfYear - the month-of-year to represent, not null
        dayOfMonth - the day-of-month to represent, from 1 to 31
        hourOfDay - the hour-of-day to represent, from 0 to 23
        minuteOfHour - the minute-of-hour to represent, from 0 to 59
        secondOfMinute - the second-of-minute to represent, from 0 to 59
        Returns:
        the local date-time, never null
        Throws:
        IllegalCalendarFieldValueException - if the value of any field is out of range
        InvalidCalendarFieldException - if the day-of-month is invalid for the month-year
      • of

        public static LocalDateTime of​(int year,
                                       MonthOfYear monthOfYear,
                                       int dayOfMonth,
                                       int hourOfDay,
                                       int minuteOfHour,
                                       int secondOfMinute,
                                       int nanoOfSecond)
        Obtains an instance of LocalDateTime from year, month, day, hour, minute, second and nanosecond.

        The day must be valid for the year and month or an exception will be thrown.

        Parameters:
        year - the year to represent, from MIN_YEAR to MAX_YEAR
        monthOfYear - the month-of-year to represent, not null
        dayOfMonth - the day-of-month to represent, from 1 to 31
        hourOfDay - the hour-of-day to represent, from 0 to 23
        minuteOfHour - the minute-of-hour to represent, from 0 to 59
        secondOfMinute - the second-of-minute to represent, from 0 to 59
        nanoOfSecond - the nano-of-second to represent, from 0 to 999,999,999
        Returns:
        the local date-time, never null
        Throws:
        IllegalCalendarFieldValueException - if the value of any field is out of range
        InvalidCalendarFieldException - if the day-of-month is invalid for the month-year
      • of

        public static LocalDateTime of​(int year,
                                       int monthOfYear,
                                       int dayOfMonth,
                                       int hourOfDay,
                                       int minuteOfHour)
        Obtains an instance of LocalDateTime from year, month, day, hour and minute, setting the second and nanosecond to zero.

        The day must be valid for the year and month or an exception will be thrown.

        The second and nanosecond fields will be set to zero by this factory method.

        Parameters:
        year - the year to represent, from MIN_YEAR to MAX_YEAR
        monthOfYear - the month-of-year to represent, from 1 (January) to 12 (December)
        dayOfMonth - the day-of-month to represent, from 1 to 31
        hourOfDay - the hour-of-day to represent, from 0 to 23
        minuteOfHour - the minute-of-hour to represent, from 0 to 59
        Returns:
        the local date-time, never null
        Throws:
        IllegalCalendarFieldValueException - if the value of any field is out of range
        InvalidCalendarFieldException - if the day-of-month is invalid for the month-year
      • of

        public static LocalDateTime of​(int year,
                                       int monthOfYear,
                                       int dayOfMonth,
                                       int hourOfDay,
                                       int minuteOfHour,
                                       int secondOfMinute)
        Obtains an instance of LocalDateTime from year, month, day, hour, minute and second, setting the nanosecond to zero.

        The day must be valid for the year and month or an exception will be thrown.

        The nanosecond field will be set to zero by this factory method.

        Parameters:
        year - the year to represent, from MIN_YEAR to MAX_YEAR
        monthOfYear - the month-of-year to represent, from 1 (January) to 12 (December)
        dayOfMonth - the day-of-month to represent, from 1 to 31
        hourOfDay - the hour-of-day to represent, from 0 to 23
        minuteOfHour - the minute-of-hour to represent, from 0 to 59
        secondOfMinute - the second-of-minute to represent, from 0 to 59
        Returns:
        the local date-time, never null
        Throws:
        IllegalCalendarFieldValueException - if the value of any field is out of range
        InvalidCalendarFieldException - if the day-of-month is invalid for the month-year
      • of

        public static LocalDateTime of​(int year,
                                       int monthOfYear,
                                       int dayOfMonth,
                                       int hourOfDay,
                                       int minuteOfHour,
                                       int secondOfMinute,
                                       int nanoOfSecond)
        Obtains an instance of LocalDateTime from year, month, day, hour, minute, second and nanosecond.

        The day must be valid for the year and month or an exception will be thrown.

        Parameters:
        year - the year to represent, from MIN_YEAR to MAX_YEAR
        monthOfYear - the month-of-year to represent, from 1 (January) to 12 (December)
        dayOfMonth - the day-of-month to represent, from 1 to 31
        hourOfDay - the hour-of-day to represent, from 0 to 23
        minuteOfHour - the minute-of-hour to represent, from 0 to 59
        secondOfMinute - the second-of-minute to represent, from 0 to 59
        nanoOfSecond - the nano-of-second to represent, from 0 to 999,999,999
        Returns:
        the local date-time, never null
        Throws:
        IllegalCalendarFieldValueException - if the value of any field is out of range
        InvalidCalendarFieldException - if the day-of-month is invalid for the month-year
      • of

        public static LocalDateTime of​(DateProvider dateProvider,
                                       TimeProvider timeProvider)
        Obtains an instance of LocalDateTime from a date and time.
        Parameters:
        dateProvider - the date provider to use, not null
        timeProvider - the time provider to use, not null
        Returns:
        the local date-time, never null
      • of

        public static LocalDateTime of​(DateTimeProvider dateTimeProvider)
        Obtains an instance of LocalTime from a date-time provider.

        The purpose of this method is to convert a DateTimeProvider to a LocalDateTime in the safest possible way. Specifically, the means checking whether the input parameter is null and whether the result of the provider is null.

        Parameters:
        dateTimeProvider - the date-time provider to use, not null
        Returns:
        the local date-time, never null
      • parse

        public static LocalDateTime parse​(java.lang.String text)
        Obtains an instance of LocalDateTime from a text string such as 2007-12-03T10:15:30.

        The following formats are accepted in ASCII:

        • {Year}-{MonthOfYear}-{DayOfMonth}T{Hour}:{Minute}
        • {Year}-{MonthOfYear}-{DayOfMonth}T{Hour}:{Minute}:{Second}
        • {Year}-{MonthOfYear}-{DayOfMonth}T{Hour}:{Minute}:{Second}.{NanosecondFraction}

        The year has between 4 and 10 digits with values from MIN_YEAR to MAX_YEAR. If there are more than 4 digits then the year must be prefixed with the plus symbol. Negative years are allowed, but not negative zero.

        The month-of-year has 2 digits with values from 1 to 12.

        The day-of-month has 2 digits with values from 1 to 31 appropriate to the month.

        The hour has 2 digits with values from 0 to 23. The minute has 2 digits with values from 0 to 59. The second has 2 digits with values from 0 to 59. The nanosecond fraction has from 1 to 9 digits with values from 0 to 999,999,999.

        Parameters:
        text - the text to parse such as '2007-12-03T10:15:30', not null
        Returns:
        the parsed local date-time, never null
        Throws:
        CalendricalException - if the text cannot be parsed
      • parse

        public static LocalDateTime parse​(java.lang.String text,
                                          DateTimeFormatter formatter)
        Obtains an instance of LocalDateTime from a text string using a specific formatter.

        The text is parsed using the formatter, returning a date-time.

        Parameters:
        text - the text to parse, not null
        formatter - the formatter to use, not null
        Returns:
        the parsed local date-time, never null
        Throws:
        java.lang.UnsupportedOperationException - if the formatter cannot parse
        CalendricalException - if the text cannot be parsed
      • with

        private LocalDateTime with​(LocalDate newDate,
                                   LocalTime newTime)
        Returns a copy of this date-time with the new date and time, checking to see if a new object is in fact required.
        Parameters:
        newDate - the date of the new date-time, not null
        newTime - the time of the new date-time, not null
        Returns:
        the date-time, never null
      • getChronology

        public ISOChronology getChronology()
        Gets the chronology that this date-time uses, which is the ISO calendar system.
        Returns:
        the ISO chronology, never null
      • get

        public <T> T get​(CalendricalRule<T> rule)
        Gets the value of the specified calendrical rule.

        This method queries the value of the specified calendrical rule. If the value cannot be returned for the rule from this date-time then null will be returned.

        Specified by:
        get in interface Calendrical
        Parameters:
        rule - the rule to use, not null
        Returns:
        the value for the rule, null if the value cannot be returned
      • getYear

        public int getYear()
        Gets the year field.

        This method returns the primitive int value for the year. Additional information about the year can be obtained by creating a Year.

        Returns:
        the year, from MIN_YEAR to MAX_YEAR
      • getMonthOfYear

        public MonthOfYear getMonthOfYear()
        Gets the month-of-year field, which is an enum MonthOfYear.

        This method returns the enum MonthOfYear for the month. This avoids confusion as to what int values mean. If you need access to the primitive int value then the enum provides the int value.

        Additional information can be obtained from the MonthOfYear. This includes month lengths, textual names and access to the quarter-of-year and month-of-quarter values.

        Returns:
        the month-of-year, never null
      • getDayOfMonth

        public int getDayOfMonth()
        Gets the day-of-month field.

        This method returns the primitive int value for the day-of-month.

        Returns:
        the day-of-month, from 1 to 31
      • getDayOfYear

        public int getDayOfYear()
        Gets the day-of-year field.

        This method returns the primitive int value for the day-of-year.

        Returns:
        the day-of-year, from 1 to 365, or 366 in a leap year
      • getDayOfWeek

        public DayOfWeek getDayOfWeek()
        Gets the day-of-week field, which is an enum DayOfWeek.

        This method returns the enum DayOfWeek for the day-of-week. This avoids confusion as to what int values mean. If you need access to the primitive int value then the enum provides the int value.

        Additional information can be obtained from the DayOfWeek. This includes textual names of the values.

        Returns:
        the day-of-week, never null
      • getHourOfDay

        public int getHourOfDay()
        Gets the hour-of-day field.
        Returns:
        the hour-of-day, from 0 to 23
      • getMinuteOfHour

        public int getMinuteOfHour()
        Gets the minute-of-hour field.
        Returns:
        the minute-of-hour, from 0 to 59
      • getSecondOfMinute

        public int getSecondOfMinute()
        Gets the second-of-minute field.
        Returns:
        the second-of-minute, from 0 to 59
      • getNanoOfSecond

        public int getNanoOfSecond()
        Gets the nano-of-second field.
        Returns:
        the nano-of-second, from 0 to 999,999,999
      • isLeapYear

        public boolean isLeapYear()
        Checks if the year is a leap year, according to the ISO proleptic calendar system rules.

        This method applies the current rules for leap years across the whole time-line. In general, a year is a leap year if it is divisible by four without remainder. However, years divisible by 100, are not leap years, with the exception of years divisible by 400 which are.

        For example, 1904 is a leap year it is divisible by 4. 1900 was not a leap year as it is divisible by 100, however 2000 was a leap year as it is divisible by 400.

        The calculation is proleptic - applying the same rules into the far future and far past. This is historically inaccurate, but is correct for the ISO8601 standard.

        Returns:
        true if the year is leap, false otherwise
      • with

        public LocalDateTime with​(DateAdjuster adjuster)
        Returns a copy of this LocalDateTime with the date altered using the adjuster.

        Adjusters can be used to alter the date in various ways. A simple adjuster might simply set the one of the fields, such as the year field. A more complex adjuster might set the date to the last day of the month.

        The time does not affect the calculation and will be the same in the result.

        This instance is immutable and unaffected by this method call.

        Parameters:
        adjuster - the adjuster to use, not null
        Returns:
        a LocalDateTime based on this date-time with the date adjusted, never null
        Throws:
        java.lang.NullPointerException - if the adjuster returned null
      • with

        public LocalDateTime with​(TimeAdjuster adjuster)
        Returns a copy of this LocalDateTime with the time altered using the adjuster.

        Adjusters can be used to alter the time in various ways. A simple adjuster might simply set the one of the fields, such as the hour field. A more complex adjuster might set the time to end of the working day.

        The date does not affect the calculation and will be the same in the result.

        This instance is immutable and unaffected by this method call.

        Parameters:
        adjuster - the adjuster to use, not null
        Returns:
        a LocalDateTime based on this date-time with the time adjusted, never null
        Throws:
        java.lang.IllegalArgumentException - if the adjuster returned null
      • withYear

        public LocalDateTime withYear​(int year)
        Returns a copy of this LocalDateTime with the year altered. If the resulting LocalDateTime is invalid, it will be resolved using DateResolvers.previousValid(). The time does not affect the calculation and will be the same in the result.

        This method does the same as withYear(year, DateResolvers.previousValid()).

        This instance is immutable and unaffected by this method call.

        Parameters:
        year - the year to set in the returned date, from MIN_YEAR to MAX_YEAR
        Returns:
        a LocalDateTime based on this date-time with the requested year, never null
        Throws:
        IllegalCalendarFieldValueException - if the year value is invalid
      • withYear

        public LocalDateTime withYear​(int year,
                                      DateResolver dateResolver)
        Returns a copy of this LocalDateTime with the year altered. If the resulting LocalDateTime is invalid, it will be resolved using dateResolver. The time does not affect the calculation and will be the same in the result.

        This instance is immutable and unaffected by this method call.

        Parameters:
        year - the year to set in the returned date, from MIN_YEAR to MAX_YEAR
        dateResolver - the DateResolver to be used if the resulting date would be invalid
        Returns:
        a LocalDateTime based on this date-time with the requested year, never null
        Throws:
        IllegalCalendarFieldValueException - if the year value is invalid
      • withMonthOfYear

        public LocalDateTime withMonthOfYear​(int monthOfYear)
        Returns a copy of this LocalDateTime with the month-of-year altered. If the resulting LocalDateTime is invalid, it will be resolved using DateResolvers.previousValid(). The time does not affect the calculation and will be the same in the result.

        This method does the same as withMonthOfYear(monthOfYear, DateResolvers.previousValid()).

        This instance is immutable and unaffected by this method call.

        Parameters:
        monthOfYear - the month-of-year to set in the returned date, from 1 (January) to 12 (December)
        Returns:
        a LocalDateTime based on this date-time with the requested month, never null
        Throws:
        IllegalCalendarFieldValueException - if the month-of-year value is invalid
      • withMonthOfYear

        public LocalDateTime withMonthOfYear​(int monthOfYear,
                                             DateResolver dateResolver)
        Returns a copy of this LocalDateTime with the month-of-year altered. If the resulting LocalDateTime is invalid, it will be resolved using dateResolver. The time does not affect the calculation and will be the same in the result.

        This instance is immutable and unaffected by this method call.

        Parameters:
        monthOfYear - the month-of-year to set in the returned date, from 1 (January) to 12 (December)
        dateResolver - the DateResolver to be used if the resulting date would be invalid
        Returns:
        a LocalDateTime based on this date-time with the requested month, never null
        Throws:
        IllegalCalendarFieldValueException - if the month-of-year value is invalid
      • with

        public LocalDateTime with​(MonthOfYear monthOfYear)
        Returns a copy of this LocalDateTime with the month-of-year altered. If the resulting LocalDateTime is invalid, it will be resolved using DateResolvers.previousValid(). The time does not affect the calculation and will be the same in the result.

        This method does the same as with(monthOfYear, DateResolvers.previousValid()).

        This instance is immutable and unaffected by this method call.

        Parameters:
        monthOfYear - the month-of-year to set in the returned date, not null
        Returns:
        a LocalDateTime based on this date-time with the requested month, never null
      • with

        public LocalDateTime with​(MonthOfYear monthOfYear,
                                  DateResolver dateResolver)
        Returns a copy of this LocalDateTime with the month-of-year altered. If the resulting LocalDateTime is invalid, it will be resolved using dateResolver. The time does not affect the calculation and will be the same in the result.

        This instance is immutable and unaffected by this method call.

        Parameters:
        monthOfYear - the month-of-year to set in the returned date, not null
        dateResolver - the DateResolver to be used if the resulting date would be invalid
        Returns:
        a LocalDateTime based on this date-time with the requested month, never null
      • withDayOfMonth

        public LocalDateTime withDayOfMonth​(int dayOfMonth)
        Returns a copy of this LocalDateTime with the day-of-month altered. If the resulting LocalDateTime is invalid, an exception is thrown. The time does not affect the calculation and will be the same in the result.

        This instance is immutable and unaffected by this method call.

        Parameters:
        dayOfMonth - the day-of-month to set in the returned date, from 1 to 28-31
        Returns:
        a LocalDateTime based on this date-time with the requested day, never null
        Throws:
        IllegalCalendarFieldValueException - if the day-of-month value is invalid
        InvalidCalendarFieldException - if the day-of-month is invalid for the month-year
      • withDayOfMonth

        public LocalDateTime withDayOfMonth​(int dayOfMonth,
                                            DateResolver dateResolver)
        Returns a copy of this LocalDateTime with the day-of-month altered. If the resulting LocalDateTime is invalid, it will be resolved using dateResolver. The time does not affect the calculation and will be the same in the result.

        This instance is immutable and unaffected by this method call.

        Parameters:
        dayOfMonth - the day-of-month to set in the returned date, from 1 to 31
        dateResolver - the DateResolver to be used if the resulting date would be invalid
        Returns:
        a LocalDateTime based on this date-time with the requested day, never null
        Throws:
        IllegalCalendarFieldValueException - if the day-of-month value is invalid
      • withDayOfYear

        public LocalDateTime withDayOfYear​(int dayOfYear)
        Returns a copy of this LocalDateTime with the day-of-year altered. If the resulting LocalDateTime is invalid, an exception is thrown.

        This instance is immutable and unaffected by this method call.

        Parameters:
        dayOfYear - the day-of-year to set in the returned date, from 1 to 365-366
        Returns:
        a LocalDateTime based on this date with the requested day, never null
        Throws:
        IllegalCalendarFieldValueException - if the day-of-year value is invalid
        InvalidCalendarFieldException - if the day-of-year is invalid for the year
      • withDate

        public LocalDateTime withDate​(int year,
                                      MonthOfYear monthOfYear,
                                      int dayOfMonth)
        Returns a copy of this LocalDateTime with the date values altered.

        This method will return a new instance with the same time fields, but altered date fields.

        This instance is immutable and unaffected by this method call.

        Parameters:
        year - the year to represent, from MIN_YEAR to MAX_YEAR
        monthOfYear - the month-of-year to represent, not null
        dayOfMonth - the day-of-month to represent, from 1 to 31
        Returns:
        a LocalDateTime based on this date-time with the requested date, never null
        Throws:
        IllegalCalendarFieldValueException - if any field value is invalid
        InvalidCalendarFieldException - if the day-of-month is invalid for the month-year
      • withDate

        public LocalDateTime withDate​(int year,
                                      int monthOfYear,
                                      int dayOfMonth)
        Returns a copy of this LocalDateTime with the date values altered.

        This method will return a new instance with the same time fields, but altered date fields.

        This instance is immutable and unaffected by this method call.

        Parameters:
        year - the year to represent, from MIN_YEAR to MAX_YEAR
        monthOfYear - the month-of-year to represent, from 1 (January) to 12 (December)
        dayOfMonth - the day-of-month to represent, from 1 to 31
        Returns:
        a LocalDateTime based on this date-time with the requested date, never null
        Throws:
        IllegalCalendarFieldValueException - if any field value is invalid
        InvalidCalendarFieldException - if the day-of-month is invalid for the month-year
      • withHourOfDay

        public LocalDateTime withHourOfDay​(int hourOfDay)
        Returns a copy of this LocalDateTime with the hour-of-day value altered.

        This instance is immutable and unaffected by this method call.

        Parameters:
        hourOfDay - the hour-of-day to represent, from 0 to 23
        Returns:
        a LocalDateTime based on this date-time with the requested hour, never null
        Throws:
        IllegalCalendarFieldValueException - if the hour value is invalid
      • withMinuteOfHour

        public LocalDateTime withMinuteOfHour​(int minuteOfHour)
        Returns a copy of this LocalDateTime with the minute-of-hour value altered.

        This instance is immutable and unaffected by this method call.

        Parameters:
        minuteOfHour - the minute-of-hour to represent, from 0 to 59
        Returns:
        a LocalDateTime based on this date-time with the requested minute, never null
        Throws:
        IllegalCalendarFieldValueException - if the minute value is invalid
      • withSecondOfMinute

        public LocalDateTime withSecondOfMinute​(int secondOfMinute)
        Returns a copy of this LocalDateTime with the second-of-minute value altered.

        This instance is immutable and unaffected by this method call.

        Parameters:
        secondOfMinute - the second-of-minute to represent, from 0 to 59
        Returns:
        a LocalDateTime based on this date-time with the requested second, never null
        Throws:
        IllegalCalendarFieldValueException - if the second value is invalid
      • withNanoOfSecond

        public LocalDateTime withNanoOfSecond​(int nanoOfSecond)
        Returns a copy of this LocalDateTime with the nano-of-second value altered.

        This instance is immutable and unaffected by this method call.

        Parameters:
        nanoOfSecond - the nano-of-second to represent, from 0 to 999,999,999
        Returns:
        a LocalDateTime based on this date-time with the requested nanosecond, never null
        Throws:
        IllegalCalendarFieldValueException - if the nano value is invalid
      • withTime

        public LocalDateTime withTime​(int hourOfDay,
                                      int minuteOfHour)
        Returns a copy of this LocalDateTime with the time values altered.

        This method will return a new instance with the same date fields, but altered time fields. This is a shorthand for withTime(int,int,int,int) and sets the second and nanosecond fields to zero.

        This instance is immutable and unaffected by this method call.

        Parameters:
        hourOfDay - the hour-of-day to represent, from 0 to 23
        minuteOfHour - the minute-of-hour to represent, from 0 to 59
        Returns:
        a LocalDateTime based on this date-time with the requested time, never null
        Throws:
        IllegalCalendarFieldValueException - if any field value is invalid
      • withTime

        public LocalDateTime withTime​(int hourOfDay,
                                      int minuteOfHour,
                                      int secondOfMinute)
        Returns a copy of this LocalDateTime with the time values altered.

        This method will return a new instance with the same date fields, but altered time fields. This is a shorthand for withTime(int,int,int,int) and sets the nanosecond fields to zero.

        This instance is immutable and unaffected by this method call.

        Parameters:
        hourOfDay - the hour-of-day to represent, from 0 to 23
        minuteOfHour - the minute-of-hour to represent, from 0 to 59
        secondOfMinute - the second-of-minute to represent, from 0 to 59
        Returns:
        a LocalDateTime based on this date-time with the requested time, never null
        Throws:
        IllegalCalendarFieldValueException - if any field value is invalid
      • withTime

        public LocalDateTime withTime​(int hourOfDay,
                                      int minuteOfHour,
                                      int secondOfMinute,
                                      int nanoOfSecond)
        Returns a copy of this LocalDateTime with the time values altered.

        This method will return a new instance with the same date fields, but altered time fields.

        This instance is immutable and unaffected by this method call.

        Parameters:
        hourOfDay - the hour-of-day to represent, from 0 to 23
        minuteOfHour - the minute-of-hour to represent, from 0 to 59
        secondOfMinute - the second-of-minute to represent, from 0 to 59
        nanoOfSecond - the nano-of-second to represent, from 0 to 999,999,999
        Returns:
        a LocalDateTime based on this date-time with the requested time, never null
        Throws:
        IllegalCalendarFieldValueException - if any field value is invalid
      • plus

        public LocalDateTime plus​(PeriodProvider periodProvider)
        Returns a copy of this LocalDateTime with the specified period added.

        This adds the specified period to this date-time, returning a new date-time. Before addition, the period is converted to a Period using Period.of(PeriodProvider).

        The detailed rules for the addition effectively treat the date and time parts of this date-time completely separately during the calculation.

        The rules are expressed in four steps:

        1. Add the date part of the period to the date part of this date-time using LocalDate.plus(PeriodProvider) - which has some complex rules
        2. Add the time part of the period to the time part of this date-time using LocalTime#plusWithOverflow(int, int, int, long)
        3. Add the overflow days from the time calculation to the calculated date
        4. Combine the new date and time parts to form the result

        The effect of this definition is that time periods are always evenly spaced. For example, adding 5 hours will always result in a date-time one hour later than adding 4 hours. However, another effect of the definition is that adding 24 hour periods is not the same as adding 1 day periods. See the rules of date addition to understand why.

        For example, this table shows what happens when for various inputs and periods:

           2010-01-30T00:00 plus P1M2DT-5H  = 2010-03-01T19:00
           2010-01-30T00:00 plus P1M2D      = 2010-03-02T00:00
           2010-01-30T00:00 plus P1M2DT4H   = 2010-03-02T04:00
           
           2010-01-30T00:00 plus P1M1DT-5H  = 2010-02-28T19:00
           2010-01-30T00:00 plus P1M1D      = 2010-03-01T00:00
           2010-01-30T00:00 plus P1M1DT4H   = 2010-03-01T04:00
           
           2010-01-30T00:00 plus P1MT-5H    = 2010-02-27T19:00
           2010-01-30T00:00 plus P1M        = 2010-02-28T00:00
           2010-01-30T00:00 plus P1MT4H     = 2010-02-28T04:00
           
           2010-01-30T00:00 plus P1M-1DT-5H = 2010-02-27T19:00
           2010-01-30T00:00 plus P1M-1D     = 2010-02-28T00:00
           2010-01-30T00:00 plus P1M-1DT4H  = 2010-02-28T04:00
           
           2010-01-30T00:00 plus P1M-2DT-5H = 2010-02-27T19:00
           2010-01-30T00:00 plus P1M-2D     = 2010-02-28T00:00
           2010-01-30T00:00 plus P1M-2DT4H  = 2010-02-28T04:00
           
           2010-01-30T00:00 plus P1M-3DT-5H = 2010-02-26T19:00
           2010-01-30T00:00 plus P1M-3D     = 2010-02-27T00:00
           2010-01-30T00:00 plus P1M-3DT4H  = 2010-02-27T04:00
         

        This instance is immutable and unaffected by this method call.

        Parameters:
        periodProvider - the period to add, not null
        Returns:
        a LocalDateTime based on this date-time with the period added, never null
        Throws:
        CalendricalException - if the specified period cannot be converted to a Period
        CalendricalException - if the result exceeds the supported date range
      • plus

        public LocalDateTime plus​(Duration duration)
        Returns a copy of this LocalDateTime with the specified duration added.

        This adds the specified duration to this date-time, returning a new date-time.

        The calculation is equivalent to using plusSeconds(long) and plusNanos(long) on the two parts of the duration.

        This instance is immutable and unaffected by this method call.

        Parameters:
        duration - the duration to add, not null
        Returns:
        a LocalDateTime based on this date-time with the duration added, never null
        Throws:
        CalendricalException - if the result exceeds the supported date range
      • plusYears

        public LocalDateTime plusYears​(long years)
        Returns a copy of this LocalDateTime with the specified period in years added.

        This method adds the specified amount to the years field in three steps:

        1. Add the input years to the year field
        2. Check if the resulting date would be invalid
        3. Adjust the day-of-month to the last valid day if necessary

        For example, 2008-02-29 (leap year) plus one year would result in the invalid date 2009-02-29 (standard year). Instead of returning an invalid result, the last valid day of the month, 2009-02-28, is selected instead.

        This method does the same as plusYears(years, DateResolvers.previousValid()).

        This instance is immutable and unaffected by this method call.

        Parameters:
        years - the years to add, may be negative
        Returns:
        a LocalDateTime based on this date-time with the years added, never null
        Throws:
        CalendricalException - if the result exceeds the supported date range
        See Also:
        plusYears(long, javax.time.calendar.DateResolver)
      • plusYears

        public LocalDateTime plusYears​(long years,
                                       DateResolver dateResolver)
        Returns a copy of this LocalDateTime with the specified period in years added.

        This method adds the specified amount to the years field in three steps:

        1. Add the input years to the year field
        2. Check if the resulting date would be invalid
        3. Adjust the date using dateResolver if necessary

        This instance is immutable and unaffected by this method call.

        Parameters:
        years - the years to add, may be negative
        dateResolver - the DateResolver to be used if the resulting date would be invalid
        Returns:
        a LocalDateTime based on this date-time with the years added, never null
        Throws:
        CalendricalException - if the result exceeds the supported date range
      • plusMonths

        public LocalDateTime plusMonths​(long months)
        Returns a copy of this LocalDateTime with the specified period in months added.

        This method adds the specified amount to the months field in three steps:

        1. Add the input months to the month-of-year field
        2. Check if the resulting date would be invalid
        3. Adjust the day-of-month to the last valid day if necessary

        For example, 2007-03-31 plus one month would result in the invalid date 2007-04-31. Instead of returning an invalid result, the last valid day of the month, 2007-04-30, is selected instead.

        This method does the same as plusMonths(months, DateResolvers.previousValid()).

        This instance is immutable and unaffected by this method call.

        Parameters:
        months - the months to add, may be negative
        Returns:
        a LocalDateTime based on this date-time with the months added, never null
        Throws:
        CalendricalException - if the result exceeds the supported date range
        See Also:
        plusMonths(long, javax.time.calendar.DateResolver)
      • plusMonths

        public LocalDateTime plusMonths​(long months,
                                        DateResolver dateResolver)
        Returns a copy of this LocalDateTime with the specified period in months added.

        This method adds the specified amount to the months field in three steps:

        1. Add the input months to the month-of-year field
        2. Check if the resulting date would be invalid
        3. Adjust the date using dateResolver if necessary

        This instance is immutable and unaffected by this method call.

        Parameters:
        months - the months to add, may be negative
        dateResolver - the DateResolver to be used if the resulting date would be invalid
        Returns:
        a LocalDateTime based on this date-time with the months added, never null
        Throws:
        CalendricalException - if the result exceeds the supported date range
      • plusWeeks

        public LocalDateTime plusWeeks​(long weeks)
        Returns a copy of this LocalDateTime with the specified period in weeks added.

        This method adds the specified amount in weeks to the days field incrementing the month and year fields as necessary to ensure the result remains valid. The result is only invalid if the maximum/minimum year is exceeded.

        For example, 2008-12-31 plus one week would result in the 2009-01-07.

        This instance is immutable and unaffected by this method call.

        Parameters:
        weeks - the weeks to add, may be negative
        Returns:
        a LocalDateTime based on this date-time with the weeks added, never null
        Throws:
        CalendricalException - if the result exceeds the supported date range
      • plusDays

        public LocalDateTime plusDays​(long days)
        Returns a copy of this LocalDateTime with the specified period in days added.

        This method adds the specified amount to the days field incrementing the month and year fields as necessary to ensure the result remains valid. The result is only invalid if the maximum/minimum year is exceeded.

        For example, 2008-12-31 plus one day would result in the 2009-01-01.

        This instance is immutable and unaffected by this method call.

        Parameters:
        days - the days to add, may be negative
        Returns:
        a LocalDateTime based on this date-time with the days added, never null
        Throws:
        CalendricalException - if the result exceeds the supported date range
      • plusHours

        public LocalDateTime plusHours​(long hours)
        Returns a copy of this LocalDateTime with the specified period in hours added.

        This instance is immutable and unaffected by this method call.

        Parameters:
        hours - the hours to add, may be negative
        Returns:
        a LocalDateTime based on this date-time with the hours added, never null
        Throws:
        CalendricalException - if the result exceeds the supported date range
      • plusMinutes

        public LocalDateTime plusMinutes​(long minutes)
        Returns a copy of this LocalDateTime with the specified period in minutes added.

        This instance is immutable and unaffected by this method call.

        Parameters:
        minutes - the minutes to add, may be negative
        Returns:
        a LocalDateTime based on this date-time with the minutes added, never null
        Throws:
        CalendricalException - if the result exceeds the supported date range
      • plusSeconds

        public LocalDateTime plusSeconds​(long seconds)
        Returns a copy of this LocalDateTime with the specified period in seconds added.

        This instance is immutable and unaffected by this method call.

        Parameters:
        seconds - the seconds to add, may be negative
        Returns:
        a LocalDateTime based on this date-time with the seconds added, never null
        Throws:
        CalendricalException - if the result exceeds the supported date range
      • plusNanos

        public LocalDateTime plusNanos​(long nanos)
        Returns a copy of this LocalDateTime with the specified period in nanoseconds added.

        This instance is immutable and unaffected by this method call.

        Parameters:
        nanos - the nanos to add, may be negative
        Returns:
        a LocalDateTime based on this date-time with the nanoseconds added, never null
        Throws:
        CalendricalException - if the result exceeds the supported date range
      • minus

        public LocalDateTime minus​(PeriodProvider periodProvider)
        Returns a copy of this LocalDateTime with the specified period subtracted.

        This subtracts the specified period from this date-time, returning a new date-time. Before subtraction, the period is converted to a Period using Period.of(PeriodProvider).

        The detailed rules for the subtraction effectively treat the date and time parts of this date-time completely separately during the calculation.

        The rules are expressed in four steps:

        1. Subtract the date part of the period from the date part of this date-time using LocalDate.minus(PeriodProvider) - which has some complex rules
        2. Subtract the time part of the period from the time part of this date-time using LocalTime#minusWithOverflow(int, int, int, long)
        3. Subtract the overflow days from the time calculation from the calculated date
        4. Combine the new date and time parts to form the result

        The effect of this definition is that time periods are always evenly spaced. For example, subtracting 5 hours will always result in a date-time one hour earlier than adding 4 hours. However, another effect of the definition is that subtracting 24 hour periods is not the same as subtracting 1 day periods. See the rules of date subtraction to understand why.

        For example, this table shows what happens when for various inputs and periods:

           2010-03-30T00:00 minus P1M3DT-5H  = 2010-02-27T05:00
           2010-03-30T00:00 minus P1M3D      = 2010-02-27T00:00
           2010-03-30T00:00 minus P1M3DT4H   = 2010-02-26T20:00
           
           2010-03-30T00:00 minus P1M2DT-5H  = 2010-02-28T05:00
           2010-03-30T00:00 minus P1M2D      = 2010-02-28T00:00
           2010-03-30T00:00 minus P1M2DT4H   = 2010-02-27T20:00
           
           2010-03-30T00:00 minus P1M1DT-5H  = 2010-02-28T05:00
           2010-03-30T00:00 minus P1M1D      = 2010-02-28T00:00
           2010-03-30T00:00 minus P1M1DT4H   = 2010-02-27T20:00
           
           2010-03-30T00:00 minus P1MT-5H    = 2010-02-28T05:00
           2010-03-30T00:00 minus P1M        = 2010-02-28T00:00
           2010-03-30T00:00 minus P1MT4H     = 2010-02-27T20:00
           
           2010-03-30T00:00 minus P1M-1DT-5H = 2010-03-01T05:00
           2010-03-30T00:00 minus P1M-1D     = 2010-03-01T00:00
           2010-03-30T00:00 minus P1M-1DT4H  = 2010-02-28T20:00
           
           2010-03-30T00:00 minus P1M-2DT-5H = 2010-03-02T05:00
           2010-03-30T00:00 minus P1M-2D     = 2010-03-02T00:00
           2010-03-30T00:00 minus P1M-2DT4H  = 2010-03-01T20:00
         

        This instance is immutable and unaffected by this method call.

        Parameters:
        periodProvider - the period to subtract, not null
        Returns:
        a LocalDateTime based on this date-time with the period subtracted, never null
        Throws:
        CalendricalException - if the specified period cannot be converted to a Period
        CalendricalException - if the result exceeds the supported date range
      • minus

        public LocalDateTime minus​(Duration duration)
        Returns a copy of this LocalDateTime with the specified duration subtracted.

        This subtracts the specified duration from this date-time, returning a new date-time.

        The calculation is equivalent to using minusSeconds(long) and minusNanos(long) on the two parts of the duration.

        This instance is immutable and unaffected by this method call.

        Parameters:
        duration - the duration to subtract, not null
        Returns:
        a LocalDateTime based on this date-time with the duration subtracted, never null
        Throws:
        CalendricalException - if the result exceeds the supported date range
      • minusYears

        public LocalDateTime minusYears​(long years)
        Returns a copy of this LocalDateTime with the specified period in years subtracted.

        This method subtracts the specified amount from the years field in three steps:

        1. Subtract the input years to the year field
        2. Check if the resulting date would be invalid
        3. Adjust the day-of-month to the last valid day if necessary

        For example, 2008-02-29 (leap year) minus one year would result in the invalid date 2009-02-29 (standard year). Instead of returning an invalid result, the last valid day of the month, 2009-02-28, is selected instead.

        This method does the same as minusYears(years, DateResolvers.previousValid()).

        This instance is immutable and unaffected by this method call.

        Parameters:
        years - the years to subtract, may be negative
        Returns:
        a LocalDateTime based on this date-time with the years subtracted, never null
        Throws:
        CalendricalException - if the result exceeds the supported date range
        See Also:
        minusYears(long, javax.time.calendar.DateResolver)
      • minusYears

        public LocalDateTime minusYears​(long years,
                                        DateResolver dateResolver)
        Returns a copy of this LocalDateTime with the specified period in years subtracted.

        This method subtracts the specified amount from the years field in three steps:

        1. Subtract the input years to the year field
        2. Check if the resulting date would be invalid
        3. Adjust the date using dateResolver if necessary

        This instance is immutable and unaffected by this method call.

        Parameters:
        years - the years to subtract, may be negative
        dateResolver - the DateResolver to be used if the resulting date would be invalid
        Returns:
        a LocalDateTime based on this date-time with the years subtracted, never null
        Throws:
        CalendricalException - if the result exceeds the supported date range
      • minusMonths

        public LocalDateTime minusMonths​(long months)
        Returns a copy of this LocalDateTime with the specified period in months subtracted.

        This method subtracts the specified amount from the months field in three steps:

        1. Subtract the input months to the month-of-year field
        2. Check if the resulting date would be invalid
        3. Adjust the day-of-month to the last valid day if necessary

        For example, 2007-03-31 minus one month would result in the invalid date 2007-04-31. Instead of returning an invalid result, the last valid day of the month, 2007-04-30, is selected instead.

        This method does the same as minusMonts(months, DateResolvers.previousValid()).

        This instance is immutable and unaffected by this method call.

        Parameters:
        months - the months to subtract, may be negative
        Returns:
        a LocalDateTime based on this date-time with the months subtracted, never null
        Throws:
        CalendricalException - if the result exceeds the supported date range
        See Also:
        minusMonths(long, javax.time.calendar.DateResolver)
      • minusMonths

        public LocalDateTime minusMonths​(long months,
                                         DateResolver dateResolver)
        Returns a copy of this LocalDateTime with the specified period in months subtracted.

        This method subtracts the specified amount from the months field in three steps:

        1. Subtract the input months to the month-of-year field
        2. Check if the resulting date would be invalid
        3. Adjust the date using dateResolver if necessary

        This instance is immutable and unaffected by this method call.

        Parameters:
        months - the months to subtract, may be negative
        dateResolver - the DateResolver to be used if the resulting date would be invalid
        Returns:
        a LocalDateTime based on this date-time with the months subtracted, never null
        Throws:
        CalendricalException - if the result exceeds the supported date range
      • minusWeeks

        public LocalDateTime minusWeeks​(long weeks)
        Returns a copy of this LocalDateTime with the specified period in weeks subtracted.

        This method subtracts the specified amount in weeks from the days field decrementing the month and year fields as necessary to ensure the result remains valid. The result is only invalid if the maximum/minimum year is exceeded.

        For example, 2008-12-31 minus one week would result in the 2009-01-07.

        This instance is immutable and unaffected by this method call.

        Parameters:
        weeks - the weeks to subtract, may be negative
        Returns:
        a LocalDateTime based on this date-time with the weeks subtracted, never null
        Throws:
        CalendricalException - if the result exceeds the supported date range
      • minusDays

        public LocalDateTime minusDays​(long days)
        Returns a copy of this LocalDateTime with the specified period in days subtracted.

        This method subtracts the specified amount from the days field incrementing the month and year fields as necessary to ensure the result remains valid. The result is only invalid if the maximum/minimum year is exceeded.

        For example, 2008-12-31 minus one day would result in the 2009-01-01.

        This instance is immutable and unaffected by this method call.

        Parameters:
        days - the days to subtract, may be negative
        Returns:
        a LocalDateTime based on this date-time with the days subtracted, never null
        Throws:
        CalendricalException - if the result exceeds the supported date range
      • minusHours

        public LocalDateTime minusHours​(long hours)
        Returns a copy of this LocalDateTime with the specified period in hours subtracted.

        This instance is immutable and unaffected by this method call.

        Parameters:
        hours - the hours to subtract, may be negative
        Returns:
        a LocalDateTime based on this date-time with the hours subtracted, never null
        Throws:
        CalendricalException - if the result exceeds the supported date range
      • minusMinutes

        public LocalDateTime minusMinutes​(long minutes)
        Returns a copy of this LocalDateTime with the specified period in minutes subtracted.

        This instance is immutable and unaffected by this method call.

        Parameters:
        minutes - the minutes to subtract, may be negative
        Returns:
        a LocalDateTime based on this date-time with the minutes subtracted, never null
        Throws:
        CalendricalException - if the result exceeds the supported date range
      • minusSeconds

        public LocalDateTime minusSeconds​(long seconds)
        Returns a copy of this LocalDateTime with the specified period in seconds subtracted.

        This instance is immutable and unaffected by this method call.

        Parameters:
        seconds - the seconds to subtract, may be negative
        Returns:
        a LocalDateTime based on this date-time with the seconds subtracted, never null
        Throws:
        CalendricalException - if the result exceeds the supported date range
      • minusNanos

        public LocalDateTime minusNanos​(long nanos)
        Returns a copy of this LocalDateTime with the specified period in nanoseconds subtracted.

        This instance is immutable and unaffected by this method call.

        Parameters:
        nanos - the nanos to subtract, may be negative
        Returns:
        a LocalDateTime based on this date-time with the nanoseconds subtracted, never null
        Throws:
        CalendricalException - if the result exceeds the supported date range
      • matches

        public boolean matches​(CalendricalMatcher matcher)
        Checks whether this LocalDateTime matches the specified matcher.

        Matchers can be used to query the date-time. A simple matcher might simply compare one of the fields, such as the year field. A more complex matcher might check if the date is the last day of the month.

        Parameters:
        matcher - the matcher to use, not null
        Returns:
        true if this date-time matches the matcher, false otherwise
      • matchesCalendrical

        public boolean matchesCalendrical​(Calendrical calendrical)
        Checks if the date-time extracted from the calendrical matches this.

        This method implements the CalendricalMatcher interface. It is intended that applications use matches(javax.time.calendar.CalendricalMatcher) rather than this method.

        Specified by:
        matchesCalendrical in interface CalendricalMatcher
        Parameters:
        calendrical - the calendrical to match, not null
        Returns:
        true if the calendrical matches, false otherwise
      • adjustDate

        public LocalDate adjustDate​(LocalDate date)
        Adjusts a date to have the value of the date part of this object.

        This method implements the DateAdjuster interface. It is intended that applications use with(DateAdjuster) rather than this method.

        Specified by:
        adjustDate in interface DateAdjuster
        Parameters:
        date - the date to be adjusted, not null
        Returns:
        the adjusted date, never null
      • adjustTime

        public LocalTime adjustTime​(LocalTime time)
        Adjusts a time to have the value of the time part of this object.

        This method implements the TimeAdjuster interface. It is intended that applications use with(TimeAdjuster) rather than this method.

        Specified by:
        adjustTime in interface TimeAdjuster
        Parameters:
        time - the time to be adjusted, not null
        Returns:
        the adjusted time, never null
      • atOffset

        public OffsetDateTime atOffset​(ZoneOffset offset)
        Returns an offset date-time formed from this date-time and the specified offset.

        This merges the two objects - this and the specified offset - to form an instance of OffsetDateTime.

        This instance is immutable and unaffected by this method call.

        Parameters:
        offset - the offset to use, not null
        Returns:
        the offset date-time formed from this date-time and the specified offset, never null
      • atZone

        public ZonedDateTime atZone​(TimeZone zone)
        Returns a zoned date-time formed from this date-time and the specified time-zone.

        Time-zone rules, such as daylight savings, mean that not every time on the local time-line exists. When this method converts the date to a date-time it adjusts the time and offset according to the ZoneResolvers.postTransition() rules. This selects the date-time immediately after a gap and the later offset in overlaps.

        Finer control over gaps and overlaps is available in two ways. If you simply want to use the earlier offset at overlaps then call ZonedDateTime.withEarlierOffsetAtOverlap() immediately after this method. Alternately, pass a specific resolver to atZone(TimeZone, ZoneResolver).

        This instance is immutable and unaffected by this method call.

        Parameters:
        zone - the time-zone to use, not null
        Returns:
        the zoned date-time formed from this date-time, never null
      • atZone

        public ZonedDateTime atZone​(TimeZone zone,
                                    ZoneResolver resolver)
        Returns a zoned date-time formed from this date-time and the specified time-zone taking control of what occurs in time-line gaps and overlaps.

        Time-zone rules, such as daylight savings, mean that not every time on the local time-line exists. When this method converts the date to a date-time it adjusts the time and offset according to the specified zone resolver.

        This instance is immutable and unaffected by this method call.

        Parameters:
        zone - the time-zone to use, not null
        resolver - the zone resolver to use for gaps and overlaps, not null
        Returns:
        the zoned date-time formed from this date-time, never null
        Throws:
        CalendricalException - if the date-time cannot be resolved
      • toLocalDate

        public LocalDate toLocalDate()
        Converts this date-time to a LocalDate.
        Specified by:
        toLocalDate in interface DateProvider
        Returns:
        a LocalDate representing the date fields of this date-time, never null
      • toLocalTime

        public LocalTime toLocalTime()
        Converts this date-time to a LocalTime.
        Specified by:
        toLocalTime in interface TimeProvider
        Returns:
        a LocalTime representing the time fields of this date-time, never null
      • compareTo

        public int compareTo​(LocalDateTime other)
        Compares this LocalDateTime to another date-time.

        The comparison is based on the time-line position of the date-times.

        Specified by:
        compareTo in interface java.lang.Comparable<LocalDateTime>
        Parameters:
        other - the other date-time to compare to, not null
        Returns:
        the comparator value, negative if less, positive if greater
      • isAfter

        public boolean isAfter​(LocalDateTime other)
        Checks if this LocalDateTime is after the specified date-time.

        The comparison is based on the time-line position of the date-times.

        Parameters:
        other - the other date-time to compare to, not null
        Returns:
        true if this is after the specified date-time
      • isBefore

        public boolean isBefore​(LocalDateTime other)
        Checks if this LocalDateTime is before the specified date-time.

        The comparison is based on the time-line position of the date-times.

        Parameters:
        other - the other date-time to compare to, not null
        Returns:
        true if this is before the specified date-time
      • equals

        public boolean equals​(java.lang.Object other)
        Checks if this LocalDateTime is equal to the specified date-time.

        The comparison is based on the time-line position of the date-times.

        Overrides:
        equals in class java.lang.Object
        Parameters:
        other - the other date-time to compare to, null returns false
        Returns:
        true if this point is equal to the specified date-time
      • hashCode

        public int hashCode()
        A hash code for this LocalDateTime.
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        a suitable hash code
      • toString

        public java.lang.String toString()
        Outputs this date-time as a String, such as 2007-12-03T10:15:30.

        The output will be one of the following formats:

        • yyyy-MM-dd'T'HH:mm
        • yyyy-MM-dd'T'HH:mm:ss
        • yyyy-MM-dd'T'HH:mm:ssfnnn
        • yyyy-MM-dd'T'HH:mm:ssfnnnnnn
        • yyyy-MM-dd'T'HH:mm:ssfnnnnnnnnn
        The format used will be the shortest that outputs the full value of the time where the omitted parts are implied to be zero.
        Overrides:
        toString in class java.lang.Object
        Returns:
        the formatted date-time, never null
      • toString

        public java.lang.String toString​(DateTimeFormatter formatter)
        Outputs this date-time as a String using the formatter.
        Parameters:
        formatter - the formatter to use, not null
        Returns:
        the formatted date-time string, never null
        Throws:
        java.lang.UnsupportedOperationException - if the formatter cannot print
        CalendricalPrintException - if an error occurs during printing
      • rule

        public static CalendricalRule<LocalDateTime> rule()
        Gets the rule for LocalDateTime.
        Returns:
        the rule for the date-time, never null