TemporalAdjusters next() method in Java with Examples
One essential tool for changing temporal objects is an adjuster. They allow for various approaches based on the strategy design pattern and serve to externalize the process of adjustment. Some examples of date adjusters would be ones that fix the date to the final day of the month or to avoid weekends.
A temporal adjuster can be used in two compatible methods. One way to start is by directly calling the method on the interface. Using Temporal.with(TemporalAdjuster) is the second option.
// While these two lines are similar, it is best to choose the second strategy.
temporal = thisAdjuster.adjustInto(temporal);
temporal = temporal.with(thisAdjuster);
One should use the second strategy, which uses a temporal adjuster, since the code is much easier to understand. Standard adjusters are included in this class and can be accessed by static methods.
Example 1:
Input:
var d1 = localDate.with(Temporal.firstDayOfMonth());
Output:
today : 2023-12-03
Explanation:
We can determine the first day of the month using firstDayOfMonth.
Example 2:
Input:
var d2 = localDate.with(Temporal.firstDayOfYear());
Output:
first day of year : 2023-01-01
Explanation:
We can identify the first day of the year using firstDayOfYear.
Example 3:
Input:
var d3 = localDate.with(Temporal.firstDayOfNextMonth());
Output:
first day of next month : 2024-01-01
Explanation:
We could find the first day of the following month by using firstDayOfNextMonth.
Summary of Some Methods
- dayOfWeekInMonth() - an adjuster-for the ordinary day of the week.
- firstDayOfMonth() - an adjuster-for the current month's first day of the month.
- firstDayOfNextMonth() - an adjuster-for the date of the following month's first day.
- firstDayOfNextYear() - a date adjuster for the first day of the upcoming year
- firstDayOfYear() - an adjuster-for the date of the current year's first day.
- lastDayOfMonth() - an adjuster-for the date of the current year's first day.
- nextOrSame() - a date adjuster for the following occurrence of a particular day of the week, or the same day if today falls on the needed day of the week.
The next(DayOfWeek) method of a TemporalAdjusters class is used to return a TemporalAdjuster object representing the next day of the week. This TemporalAdjuster object can then be used to obtain a new Date object, which is the date that follows any Date object to that this TemporalAdjuster is applied to, and which is the next date with the same matching DayOfWeek as passed as a parameter.
Syntax:
public static TemporalAdjuster next(DayOfWeek dayOfWeek)
Parameters: The next date with the same corresponding DayOfWeek is the new Date object that can be obtained by using the dayOfWeek parameter that this method requires.
Return value: Rather than returning null, this function returns the adjuster for the following day of the week.
Implementation:
FileName: TemporalExample.java
import java.io.*;
import java.util.*;
import java.time.*;
import java.time.temporal.*;
public class TemporalExample
{
public static void main(String[] args)
{
// get TemporalAdjuster using
// the following in-month adjuster
TemporalAdjuster temporal = TemporalAdjusters.next(DayOfWeek.SUNDAY);
// utilising a local date time adjuster
LocalDate localDate = LocalDate.of(2023, 12, 03);
LocalDate next = localDate.with(temporal);
// returns the date
System.out.println("The following day of the week after" + " SUNDAY for localdate " + localDate + " is given by: " + next);
}
}
Output:
The following day of the week after SUNDAY for localdate 2023-12-03 is given by: 2023-12-10