public final class TemporalAdjusters extends Object
Adjusters are a key tool for modifying temporal objects. They exist to externalize the process of adjustment, permitting different approaches, as per the strategy design pattern. Examples might be an adjuster that sets the date avoiding weekends, or one that sets the date to the last day of the month.
There are two equivalent ways of using a TemporalAdjuster
.
The first is to invoke the method on the interface directly.
The second is to use Temporal.with(TemporalAdjuster)
:
// these two lines are equivalent, but the second approach is recommended temporal = thisAdjuster.adjustInto(temporal); temporal = temporal.with(thisAdjuster);It is recommended to use the second approach,
with(TemporalAdjuster)
,
as it is a lot clearer to read in code.
This class contains a standard set of adjusters, available as static methods. These include:
TemporalAdjuster
Modifier and Type  Method and Description 

static TemporalAdjuster 
dayOfWeekInMonth(int ordinal,
DayOfWeek dayOfWeek)
Returns the dayofweek in month adjuster, which returns a new date
in the same month with the ordinal dayofweek.

static TemporalAdjuster 
firstDayOfMonth()
Returns the "first day of month" adjuster, which returns a new date set to
the first day of the current month.

static TemporalAdjuster 
firstDayOfNextMonth()
Returns the "first day of next month" adjuster, which returns a new date set to
the first day of the next month.

static TemporalAdjuster 
firstDayOfNextYear()
Returns the "first day of next year" adjuster, which returns a new date set to
the first day of the next year.

static TemporalAdjuster 
firstDayOfYear()
Returns the "first day of year" adjuster, which returns a new date set to
the first day of the current year.

static TemporalAdjuster 
firstInMonth(DayOfWeek dayOfWeek)
Returns the first in month adjuster, which returns a new date
in the same month with the first matching dayofweek.

static TemporalAdjuster 
lastDayOfMonth()
Returns the "last day of month" adjuster, which returns a new date set to
the last day of the current month.

static TemporalAdjuster 
lastDayOfYear()
Returns the "last day of year" adjuster, which returns a new date set to
the last day of the current year.

static TemporalAdjuster 
lastInMonth(DayOfWeek dayOfWeek)
Returns the last in month adjuster, which returns a new date
in the same month with the last matching dayofweek.

static TemporalAdjuster 
next(DayOfWeek dayOfWeek)
Returns the next dayofweek adjuster, which adjusts the date to the
first occurrence of the specified dayofweek after the date being adjusted.

static TemporalAdjuster 
nextOrSame(DayOfWeek dayOfWeek)
Returns the nextorsame dayofweek adjuster, which adjusts the date to the
first occurrence of the specified dayofweek after the date being adjusted
unless it is already on that day in which case the same object is returned.

static TemporalAdjuster 
ofDateAdjuster(UnaryOperator<LocalDate> dateBasedAdjuster)
Obtains a
TemporalAdjuster that wraps a date adjuster. 
static TemporalAdjuster 
previous(DayOfWeek dayOfWeek)
Returns the previous dayofweek adjuster, which adjusts the date to the
first occurrence of the specified dayofweek before the date being adjusted.

static TemporalAdjuster 
previousOrSame(DayOfWeek dayOfWeek)
Returns the previousorsame dayofweek adjuster, which adjusts the date to the
first occurrence of the specified dayofweek before the date being adjusted
unless it is already on that day in which case the same object is returned.

public static TemporalAdjuster ofDateAdjuster(UnaryOperator<LocalDate> dateBasedAdjuster)
TemporalAdjuster
that wraps a date adjuster.
The TemporalAdjuster
is based on the low level Temporal
interface.
This method allows an adjustment from LocalDate
to LocalDate
to be wrapped to match the temporalbased interface.
This is provided for convenience to make userwritten adjusters simpler.
In general, userwritten adjusters should be static constants:
static TemporalAdjuster TWO_DAYS_LATER =
TemporalAdjusters.ofDateAdjuster(date > date.plusDays(2));
dateBasedAdjuster
 the datebased adjuster, not nullpublic static TemporalAdjuster firstDayOfMonth()
The ISO calendar system behaves as follows:
The input 20110115 will return 20110101.
The input 20110215 will return 20110201.
The behavior is suitable for use with most calendar systems. It is equivalent to:
temporal.with(DAY_OF_MONTH, 1);
public static TemporalAdjuster lastDayOfMonth()
The ISO calendar system behaves as follows:
The input 20110115 will return 20110131.
The input 20110215 will return 20110228.
The input 20120215 will return 20120229 (leap year).
The input 20110415 will return 20110430.
The behavior is suitable for use with most calendar systems. It is equivalent to:
long lastDay = temporal.range(DAY_OF_MONTH).getMaximum(); temporal.with(DAY_OF_MONTH, lastDay);
public static TemporalAdjuster firstDayOfNextMonth()
The ISO calendar system behaves as follows:
The input 20110115 will return 20110201.
The input 20110215 will return 20110301.
The behavior is suitable for use with most calendar systems. It is equivalent to:
temporal.with(DAY_OF_MONTH, 1).plus(1, MONTHS);
public static TemporalAdjuster firstDayOfYear()
The ISO calendar system behaves as follows:
The input 20110115 will return 20110101.
The input 20110215 will return 20110101.
The behavior is suitable for use with most calendar systems. It is equivalent to:
temporal.with(DAY_OF_YEAR, 1);
public static TemporalAdjuster lastDayOfYear()
The ISO calendar system behaves as follows:
The input 20110115 will return 20111231.
The input 20110215 will return 20111231.
The behavior is suitable for use with most calendar systems. It is equivalent to:
long lastDay = temporal.range(DAY_OF_YEAR).getMaximum(); temporal.with(DAY_OF_YEAR, lastDay);
public static TemporalAdjuster firstDayOfNextYear()
The ISO calendar system behaves as follows:
The input 20110115 will return 20120101.
The behavior is suitable for use with most calendar systems. It is equivalent to:
temporal.with(DAY_OF_YEAR, 1).plus(1, YEARS);
public static TemporalAdjuster firstInMonth(DayOfWeek dayOfWeek)
The ISO calendar system behaves as follows:
The input 20111215 for (MONDAY) will return 20111205.
The input 20111215 for (FRIDAY) will return 20111202.
The behavior is suitable for use with most calendar systems.
It uses the DAY_OF_WEEK
and DAY_OF_MONTH
fields
and the DAYS
unit, and assumes a seven day week.
dayOfWeek
 the dayofweek, not nullpublic static TemporalAdjuster lastInMonth(DayOfWeek dayOfWeek)
The ISO calendar system behaves as follows:
The input 20111215 for (MONDAY) will return 20111226.
The input 20111215 for (FRIDAY) will return 20111230.
The behavior is suitable for use with most calendar systems.
It uses the DAY_OF_WEEK
and DAY_OF_MONTH
fields
and the DAYS
unit, and assumes a seven day week.
dayOfWeek
 the dayofweek, not nullpublic static TemporalAdjuster dayOfWeekInMonth(int ordinal, DayOfWeek dayOfWeek)
The ISO calendar system behaves as follows:
The input 20111215 for (1,TUESDAY) will return 20111206.
The input 20111215 for (2,TUESDAY) will return 20111213.
The input 20111215 for (3,TUESDAY) will return 20111220.
The input 20111215 for (4,TUESDAY) will return 20111227.
The input 20111215 for (5,TUESDAY) will return 20120103.
The input 20111215 for (1,TUESDAY) will return 20111227 (last in month).
The input 20111215 for (4,TUESDAY) will return 20111206 (3 weeks before last in month).
The input 20111215 for (5,TUESDAY) will return 20111129 (4 weeks before last in month).
The input 20111215 for (0,TUESDAY) will return 20111129 (last in previous month).
For a positive or zero ordinal, the algorithm is equivalent to finding the first dayofweek that matches within the month and then adding a number of weeks to it. For a negative ordinal, the algorithm is equivalent to finding the last dayofweek that matches within the month and then subtracting a number of weeks to it. The ordinal number of weeks is not validated and is interpreted leniently according to this algorithm. This definition means that an ordinal of zero finds the last matching dayofweek in the previous month.
The behavior is suitable for use with most calendar systems.
It uses the DAY_OF_WEEK
and DAY_OF_MONTH
fields
and the DAYS
unit, and assumes a seven day week.
ordinal
 the week within the month, unbounded but typically from 5 to 5dayOfWeek
 the dayofweek, not nullpublic static TemporalAdjuster next(DayOfWeek dayOfWeek)
The ISO calendar system behaves as follows:
The input 20110115 (a Saturday) for parameter (MONDAY) will return 20110117 (two days later).
The input 20110115 (a Saturday) for parameter (WEDNESDAY) will return 20110119 (four days later).
The input 20110115 (a Saturday) for parameter (SATURDAY) will return 20110122 (seven days later).
The behavior is suitable for use with most calendar systems.
It uses the DAY_OF_WEEK
field and the DAYS
unit,
and assumes a seven day week.
dayOfWeek
 the dayofweek to move the date to, not nullpublic static TemporalAdjuster nextOrSame(DayOfWeek dayOfWeek)
The ISO calendar system behaves as follows:
The input 20110115 (a Saturday) for parameter (MONDAY) will return 20110117 (two days later).
The input 20110115 (a Saturday) for parameter (WEDNESDAY) will return 20110119 (four days later).
The input 20110115 (a Saturday) for parameter (SATURDAY) will return 20110115 (same as input).
The behavior is suitable for use with most calendar systems.
It uses the DAY_OF_WEEK
field and the DAYS
unit,
and assumes a seven day week.
dayOfWeek
 the dayofweek to check for or move the date to, not nullpublic static TemporalAdjuster previous(DayOfWeek dayOfWeek)
The ISO calendar system behaves as follows:
The input 20110115 (a Saturday) for parameter (MONDAY) will return 20110110 (five days earlier).
The input 20110115 (a Saturday) for parameter (WEDNESDAY) will return 20110112 (three days earlier).
The input 20110115 (a Saturday) for parameter (SATURDAY) will return 20110108 (seven days earlier).
The behavior is suitable for use with most calendar systems.
It uses the DAY_OF_WEEK
field and the DAYS
unit,
and assumes a seven day week.
dayOfWeek
 the dayofweek to move the date to, not nullpublic static TemporalAdjuster previousOrSame(DayOfWeek dayOfWeek)
The ISO calendar system behaves as follows:
The input 20110115 (a Saturday) for parameter (MONDAY) will return 20110110 (five days earlier).
The input 20110115 (a Saturday) for parameter (WEDNESDAY) will return 20110112 (three days earlier).
The input 20110115 (a Saturday) for parameter (SATURDAY) will return 20110115 (same as input).
The behavior is suitable for use with most calendar systems.
It uses the DAY_OF_WEEK
field and the DAYS
unit,
and assumes a seven day week.
dayOfWeek
 the dayofweek to check for or move the date to, not null