Java Reference
In-Depth Information
// Create an adjuster that retruns a date after 3 months and 2 days
TemporalAdjuster adjuster =
TemporalAdjusters.ofDateAdjuster((LocalDate date) -> date.plusMonths(3).plusDays(2));
// Use the adjuster
LocalDate today = LocalDate.now();
LocalDate dayAfter3Mon2Day = today.with(adjuster);
System.out.println("Today: " + today);
System.out.println("After 3 months and 2 days: " + dayAfter3Mon2Day);
Today: 2014-03-01
After 3 months and 2 days: 2014-06-03
Listing 12-15 demonstrates how to how adjust dates.
Listing 12-15. Adjusting Dates and Times
// AdjustDates.java
package com.jdojo.datetime;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
public class AdjustDates {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
System.out.println("Today: " + today);
// Use a DateAdjuster to adjust today's date to the next Monday
LocalDate nextMonday = today.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
System.out.println("Next Monday: " + nextMonday);
// Use a DateAdjuster to adjust today's date to the last day of month
LocalDate lastDayOfMonth = today.with(TemporalAdjusters.lastDayOfMonth());
System.out.println("Last day of month: " + lastDayOfMonth);
// Create an adjuster that retruns a date after 3 months and 2 days
TemporalAdjuster adjuster = TemporalAdjusters.ofDateAdjuster(
(LocalDate date) -> date.plusMonths(3).plusDays(2));
LocalDate dayAfter3Mon2Day = today.with(adjuster);
System.out.println("Date after adding 3 months and 2 days: " + dayAfter3Mon2Day);
}
}
Search WWH ::




Custom Search