Class DateAdjusters


  • public final class DateAdjusters
    extends java.lang.Object
    Provides common implementations of DateAdjuster.

    DateAdjusters is a utility class. All adjusters returned are immutable and thread-safe.

    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private DateAdjusters()
      Private constructor since this is a utility class.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static DateAdjuster dayOfWeekInMonth​(int ordinal, DayOfWeek dayOfWeek)
      Returns the day-of-week in month adjuster, which returns a new date in the same month with the ordinal day-of-week.
      static DateAdjuster firstDayOfMonth()
      Returns the first day-of-month adjuster, which returns a new date with the day-of-month changed to be the first day of the month.
      static DateAdjuster firstDayOfYear()
      Returns the first day-of-year adjuster, which returns a new date with the day-of-year changed to be the first day of the year - January 1.
      static DateAdjuster firstInMonth​(DayOfWeek dayOfWeek)
      Returns the first in month adjuster, which returns a new date in the same month with the first matching day-of-week.
      static DateAdjuster lastDayOfMonth()
      Returns the last day-of-month adjuster, which returns a new date with the day-of-month changed to be the last valid day of the month.
      static DateAdjuster lastDayOfYear()
      Returns the last day-of-year adjuster, which returns a new date with the day-of-year changed to be the last day of the year - December 31.
      static DateAdjuster next​(DayOfWeek dow)
      Returns the next day-of-week adjuster, which adjusts the date to be the next of the specified day-of-week after the specified date.
      static DateAdjuster nextNonWeekendDay()
      Returns the next non weekend day adjuster, which adjusts the date one day forward skipping Saturday and Sunday.
      static DateAdjuster nextOrCurrent​(DayOfWeek dow)
      Returns the next or current day-of-week adjuster, which adjusts the date to be be the next of the specified day-of-week, returning the input date if the day-of-week matched.
      static DateAdjuster previous​(DayOfWeek dow)
      Returns the previous day-of-week adjuster, which adjusts the date to be the previous of the specified day-of-week after the specified date.
      static DateAdjuster previousOrCurrent​(DayOfWeek dow)
      Returns the previous or current day-of-week adjuster, which adjusts the date to be be the previous of the specified day-of-week, returning the input date if the day-of-week matched.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • DateAdjusters

        private DateAdjusters()
        Private constructor since this is a utility class.
    • Method Detail

      • firstDayOfMonth

        public static DateAdjuster firstDayOfMonth()
        Returns the first day-of-month adjuster, which returns a new date with the day-of-month changed to be the first day of the month.

        The input 2007-01-15 will return 2007-01-01.
        The input 2008-02-15 will return 2008-02-01.

        Returns:
        the first day-of-month adjuster, never null
      • lastDayOfMonth

        public static DateAdjuster lastDayOfMonth()
        Returns the last day-of-month adjuster, which returns a new date with the day-of-month changed to be the last valid day of the month.

        The input 2007-01-15 will return 2007-01-31.
        The input 2007-02-15 will return 2007-02-28.
        The input 2007-03-15 will return 2007-03-31.
        The input 2007-04-15 will return 2007-04-30.
        The input 2008-02-15 will return 2008-02-29.

        Returns:
        the last day-of-month adjuster, never null
      • firstDayOfYear

        public static DateAdjuster firstDayOfYear()
        Returns the first day-of-year adjuster, which returns a new date with the day-of-year changed to be the first day of the year - January 1.

        The input 2007-01-15 will return 2007-01-01.
        The input 2008-02-15 will return 2008-01-01.

        Returns:
        the first day-of-year adjuster, never null
      • lastDayOfYear

        public static DateAdjuster lastDayOfYear()
        Returns the last day-of-year adjuster, which returns a new date with the day-of-year changed to be the last day of the year - December 31.

        The input 2007-01-15 will return 2007-12-31.
        The input 2008-02-15 will return 2008-12-31.

        Returns:
        the last day-of-year adjuster, never null
      • nextNonWeekendDay

        public static DateAdjuster nextNonWeekendDay()
        Returns the next non weekend day adjuster, which adjusts the date one day forward skipping Saturday and Sunday.
        Returns:
        the next working day adjuster, never null
      • firstInMonth

        public static DateAdjuster firstInMonth​(DayOfWeek dayOfWeek)
        Returns the first in month adjuster, which returns a new date in the same month with the first matching day-of-week. This is used for expressions like 'first Tuesday in March'.

        The input 2007-12-15 for (MONDAY) will return 2007-12-03.
        The input 2007-12-15 for (TUESDAY) will return 2007-12-04.

        Parameters:
        dayOfWeek - the day-of-week, not null
        Returns:
        the first in month adjuster, never null
      • dayOfWeekInMonth

        public static DateAdjuster dayOfWeekInMonth​(int ordinal,
                                                    DayOfWeek dayOfWeek)
        Returns the day-of-week in month adjuster, which returns a new date in the same month with the ordinal day-of-week. This is used for expressions like 'second Tuesday in March'.

        The input 2007-12-15 for (1,MONDAY) will return 2007-12-03.
        The input 2007-12-15 for (2,TUESDAY) will return 2007-12-11.
        The input 2007-12-15 for (3,TUESDAY) will return 2007-12-18.
        The input 2007-12-15 for (4,TUESDAY) will return 2007-12-25.
        The input 2007-12-15 for (5,TUESDAY) will return 2008-01-01.

        If the ordinal is 5 and there is no 5th of the requested day-of-week, then the first of the next month is returned.

        Parameters:
        ordinal - ordinal, from 1 to 5
        dayOfWeek - the day-of-week, not null
        Returns:
        the day-of-week in month adjuster, never null
        Throws:
        java.lang.IllegalArgumentException - if the ordinal is invalid
      • next

        public static DateAdjuster next​(DayOfWeek dow)
        Returns the next day-of-week adjuster, which adjusts the date to be the next of the specified day-of-week after the specified date.
        Parameters:
        dow - the day-of-week to move the date to, not null
        Returns:
        the next day-of-week adjuster, never null
      • nextOrCurrent

        public static DateAdjuster nextOrCurrent​(DayOfWeek dow)
        Returns the next or current day-of-week adjuster, which adjusts the date to be be the next of the specified day-of-week, returning the input date if the day-of-week matched.
        Parameters:
        dow - the day-of-week to move the date to, not null
        Returns:
        the next day-of-week adjuster, never null
      • previous

        public static DateAdjuster previous​(DayOfWeek dow)
        Returns the previous day-of-week adjuster, which adjusts the date to be the previous of the specified day-of-week after the specified date.
        Parameters:
        dow - the day-of-week to move the date to, not null
        Returns:
        the next day-of-week adjuster, never null
      • previousOrCurrent

        public static DateAdjuster previousOrCurrent​(DayOfWeek dow)
        Returns the previous or current day-of-week adjuster, which adjusts the date to be be the previous of the specified day-of-week, returning the input date if the day-of-week matched.
        Parameters:
        dow - the day-of-week to move the date to, not null
        Returns:
        the next day-of-week adjuster, never null