Java - Date Time Adjusting

Introduction

It is common task to adjust a date and time to have a particular date, for example, the first Monday of the month, the next Tuesday, etc.

You can perform adjustments to a date and time using an instance of the TemporalAdjuster interface.

The interface has one method, adjustInto(), that takes a Temporal and returns a Temporal.

TemporalAdjusters class consists of all static methods that return different types of predefined date adjusters.

The following code computes the first Monday after January 1, 2014:

Demo

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.TemporalAdjusters;

public class Main {
  public static void main(String[] args) {
    LocalDate ld1 = LocalDate.of(2014, Month.JANUARY, 1);
    LocalDate ld2 = ld1.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
    System.out.println(ld1);// www. ja va  2  s .c o m
    System.out.println(ld2);
  }
}

Result

Method for date adjusters

The following table lists Some Useful Methods in the TemporalAdjusters Class.

Method
Description
next(DayOfWeek dayOfWeek)

Returns an adjuster that adjusts the date to be the first specified day of
week after the date being adjusted.
nextOrSame(DayOfWeek dayOfWeek)


Returns an adjuster that adjusts the date to be the first specified day of
week after the date being adjusted. If the date being adjusted is already on
the specified day of week, it returns the same date.
previous(DayOfWeek dayOfWeek)

Returns an adjuster that adjusts the date to be the first specified day of
week before the date being adjusted.
previousOrSame(DayOfWeek
dayOfWeek)

Returns an adjuster that adjusts the date to be the first specified day of
week before the date being adjusted. If the date being adjusted is already
on the specified day of week, it returns the same date.
firstInMonth(DayOfWeek
dayOfWeek),
lastInMonth(DayOfWeek dayOfWeek)
Each returns an adjuster that adjusts the date to be the first/last
(respectively) specified day of week in the month represented by the date
being adjusted.
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<LocalDate>
dateBasedAdjuster)
A convenience method for developers to write their own LocalDate-based
adjusters.

Related Topics