Date Time Adjuster
Description
We may want to adjust a date and time to the first Monday of the month, or the next Tuesday.
We can adjust date and time using TemporalAdjuster
interface.
The interface has one method, adjustInto()
,
which takes a Temporal
and returns a Temporal
.
TemporalAdjusters
class contains static methods that
return different types of predefined date adjusters.
Example
The following code shows how to compute the first Monday after January 1, 2014:
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.TemporalAdjusters;
//from w w w . j av a2 s. c o m
public class Main {
public static void main(String[] args) {
LocalDate ld1 = LocalDate.of(2014, Month.JANUARY, 1);
LocalDate ld2 = ld1.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
System.out.println(ld1);
System.out.println(ld2);
}
}
The code above generates the following result.