Java Reference
In-Depth Information
Table 12-3. ( continued )
Method
Description
dayOfWeekInMonth(int ordinal,
DayOfWeek dayOfWeek)
Returns an adjuster that adjusts the date to be the specified ordinal day of
week in the month represented by the date being adjusted. It is suitable for
computing dates like the third Monday in January 2014.
firstDayOfMonth()
lastDayOfMonth()
Each returns an adjuster that adjusts the date to be the first/last day of the
month represented by the date being adjusted.
firstDayOfYear()
lastDayOfYear()
Each returns an adjuster that adjusts the date to be the first/last day of the
year represented by the date being adjusted.
firstDayOfNextMonth()
Returns an adjuster that adjusts the date to be the first day of the next
month represented by the date being adjusted.
firstDayOfNextYear()
Returns an adjuster that adjusts the date to be the first day of the next year
represented by the date being adjusted.
ofDateAdjuster(UnaryOperator<Loca
lDate> dateBasedAdjuster)
A convenience method for developers to write their own LocalDate -based
adjusters.
The TemporalAdjuster class provides a dayOfWeekInMonth() method. This method returns a date adjuster
that adjusts a date to the specified ordinal day of week, for example, the first Sunday of month, the third Friday of
month, etc. The specified ordinal value may be between 1 and 5. If the ordinal is 5 and the month does not have
a fifth specified dayOfWeek , it returns the first specified dayOfWeek from the next month. The following snippet of
code requests the date adjuster the fifth Sunday in May 2012. The date adjuster returns the first Sunday in June 2012
because May 2012 does not have a fifth Sunday.
LocalDate ld1 = LocalDate.of(2012, Month.MAY, 22);
LocalDate ld2 = ld1.with(TemporalAdjusters.dayOfWeekInMonth(5, DayOfWeek.SUNDAY));
System.out.println(ld1);
System.out.println(ld2);
2012-05-22
2012-06-03
You can use a date adjuster with other methods to perform a complex adjustment. You can obtain the date for the
second Friday of month after 3 months and 14 days from today as follows:
LocalDate date = LocalDate.now()
.plusMonths(3)
.plusDays(14)
.with(DateAdjusters.dayOfWeekInMonth(2, DayOfWeek.FRIDAY));
You can use the ofDateAdjuster() method to create your own date adjuster for a LocalDate . The following
snippet of code creates a date adjuster and uses it. The adjuster adds 3 months and 2 days to the date being adjusted.
Note that I have used a lambda expression to create the adjuster, which I have not discussed yet.
 
 
Search WWH ::




Custom Search