Java Reference
In-Depth Information
This TemporalAdjuster normally moves a date forward one day, except if today is a Friday or
Saturday, in which case it advances the dates three or two days, respectively. Note that since a
TemporalAdjuster is a functional interface, you could just pass the behavior of this adjuster in a
lambda expression:
date = date.with(temporal -> {
DayOfWeek dow =
DayOfWeek.of(temporal.get(ChronoField.DAY_OF_WEEK));
int dayToAdd = 1;
if (dow == DayOfWeek.FRIDAY) dayToAdd = 3;
else if (dow == DayOfWeek.SATURDAY) dayToAdd = 2;
return temporal.plus(dayToAdd, ChronoUnit.DAYS);
});
It's likely that you may want to apply this manipulation to a date in several points of your code,
and for this reason we suggest encapsulating its logic in a proper class as we did here. Do the
same for all the manipulations you use frequently. You'll end up with a small library of adjusters
you and your team could easily reuse in your codebase.
If you want to define the TemporalAdjuster with a lambda expression, it's preferable to do it
using the ofDateAdjuster static factory of the TemporalAdjusters class that accepts a
UnaryOperator<LocalDate> as follows:
TemporalAdjuster nextWorkingDay = TemporalAdjusters.ofDateAdjuster(
temporal -> {
DayOfWeek dow =
DayOfWeek.of(temporal.get(ChronoField.DAY_OF_WEEK));
 
Search WWH ::




Custom Search