Java Reference
In-Depth Information
Listing 12-17 demonstrates how to use the custom date adjuster. December 12, 2013 was on Thursday. You use
the adjuster to adjust December 12, 13, and 14 in 2013. December 12, 2013 is returned without any adjustments.
The other two dates are adjusted to next Monday, which is December 16, 2013. Note that the adjuster can be used on
any datetime object that can supply a LocalDate . The program uses it to adjust a ZonedDateTime .
Listing 12-17. Using the Custom Date Adjuster
// CustomAdjusterTest.java
package com.jdojo.datetime;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class CustomAdjusterTest {
public static void main(String[] args) {
LocalDate ld1 = LocalDate.of(2013, Month.DECEMBER, 12); // Thursday
LocalDate ld2 = LocalDate.of(2013, Month.DECEMBER, 13); // Friday
LocalDate ld3 = LocalDate.of(2013, Month.DECEMBER, 14); // Saturday
LocalDate ld1Adjusted = ld1.with(CustomAdjusters.WEEKDAYS_WITH_NO_FRIDAY_13);
System.out.println(ld1 + " adjusted to " + ld1Adjusted);
LocalDate ld2Adjusted = ld2.with(CustomAdjusters.WEEKDAYS_WITH_NO_FRIDAY_13);
System.out.println(ld2 + " adjusted to " + ld2Adjusted);
LocalDate ld3Adjusted = ld3.with(CustomAdjusters.WEEKDAYS_WITH_NO_FRIDAY_13);
System.out.println(ld3 + " adjusted to " + ld3Adjusted);
// Use it to adjust a ZonedDateTime
ZonedDateTime zdt =
ZonedDateTime.of(ld2, LocalTime.of(8, 45), ZoneId.of("America/Chicago"));
ZonedDateTime zdtAdjusted = zdt.with(CustomAdjusters.WEEKDAYS_WITH_NO_FRIDAY_13);
System.out.println(zdt + " adjusted to " + zdtAdjusted);
}
}
2013-12-12 adjusted to 2013-12-12
2013-12-13 adjusted to 2013-12-16
2013-12-14 adjusted to 2013-12-16
2013-12-13T08:45-06:00[America/Chicago] adjusted to 2013-12-16T08:45-06:00[America/Chicago]
Search WWH ::




Custom Search