TemporalAdjuster
s, introduced in Java 8's new Date and Time API, allow you to perform complex date manipulations. For example, you can adjust a date to the next Friday, or to the last day of the month. There are already several pre-defined TemporalAdjuster
s, which can be accessed using the static factory methods in the TemporalAdjusters
class, as shown below:
import java.time.DayOfWeek; import java.time.LocalDate; import static java.time.temporal.TemporalAdjusters.*; LocalDate date = LocalDate.of(2016, 7, 30); LocalDate nextFriday = date.with(nextOrSame(DayOfWeek.FRIDAY)); // 2016-08-05 LocalDate monthEnd = date.with(lastDayOfMonth()); // 2016-07-31
If you can't find a suitable TemporalAdjuster
, it's quite easy to create your own, by implementing the TemporalAdjuster
interface.
Here is an example of a custom TemporalAdjuster
, which moves the date forward to the next working day:
public class NextWorkingDay implements TemporalAdjuster { @Override public Temporal adjustInto(Temporal temporal) { DayOfWeek dayOfWeek = DayOfWeek.of(temporal.get(ChronoField.DAY_OF_WEEK)); int daysToAdd = dayOfWeek == DayOfWeek.FRIDAY ? 3 : (dayOfWeek == DayOfWeek.SATURDAY ? 2 : 1); return temporal.plus(daysToAdd, ChronoUnit.DAYS); } }
Since TemporalAdjuster
is a functional interface, you could use a lambda expression, but it is very likely that you will want to use this adjuster in other parts of your code, so it is better to encapsulate the logic in a proper class, which can then be re-used.
To define a TemporalAdjuster
with a lambda expression, it is more convenient to use the ofDateAdjuster
static factory method because it allows you work with a LocalDate
object instead of a low level Temporal
.
static TemporalAdjuster NEXT_WORKING_DAY = TemporalAdjusters.ofDateAdjuster( date -> { DayOfWeek dayOfWeek = date.getDayOfWeek(); int daysToAdd = dayOfWeek == DayOfWeek.FRIDAY ? 3 : (dayOfWeek == DayOfWeek.SATURDAY ? 2 : 1); return date.plusDays(daysToAdd); });