Open In App

java.time.temporal.TemporalAdjusters Class in Java

Improve
Improve
Like Article
Like
Save
Share
Report

TemporalAdjusters Class in Java provides Adjusters, which are a key tool for modifying temporal objects. Examples include an adjuster that sets the date like “Second Saturday of the Month” or “Next Tuesday”, or one that sets the date to the last day of the month.

TemporalAdjuster can be used in two ways. The first is by invoking the method on the interface directly. The second is by using Temporal.with(TemporalAdjuster):

The following two ways of using TemporalAdjuster are equivalent, but it is recommended to use the second approach, as it is cleaner and readable

temporal = thisAdjuster.adjustInto(temporal);
temporal = temporal.with(thisAdjuster);

Methods

Method Description 
dayOfWeekInMonth(int ordinal, DayOfWeek dayOfWeek) This method is used to return the day-of-week in month adjuster, which returns a new date object within the same month with the ordinal day-of-week.
firstDayOfMonth() This method is used to return the “first day of month” adjuster, which returns a new date set to the first day of the current month.
firstDayOfNextMonth() This method is used to return the “first day of next month” adjuster, which returns a new date set to the first day of the next month.
firstDayOfNextYear() This method is used to return the “first day of next year” adjuster, which returns a new date set to the first day of the next year.
firstDayOfYear() This method is used to return the “first day of year” adjuster, which returns a new date set to the first day of the current year.
firstInMonth(DayOfWeek dayOfWeek) This method is used to return the first in month adjuster, which returns a new date in the same month with the first matching day-of-week.
lastDayOfMonth() This method is used to return the “last day of month” adjuster, which returns a new date set to the last day of the current month.
lastDayOfYear() This method is used to return the “last day of year” adjuster, which returns a new date set to the last day of the current year.
lastInMonth(DayOfWeek dayOfWeek) This method is used to return the last in month adjuster, which returns a new date in the same month with the last matching day-of-week.
next(DayOfWeek dayOfWeek) This method is used to return the next day-of-week adjuster, which adjusts the date to the first occurrence of the specified day-of-week after the date being adjusted.
nextOrSame(DayOfWeek dayOfWeek) This method is used to return the next-or-same day-of-week adjuster, which adjusts the date to the first occurrence of the specified day-of-week after the date being adjusted unless it is already on that day in which case the same object is returned.
ofDateAdjuster(UnaryOperator<LocalDate> dateBasedAdjuster) This method is used to obtain a TemporalAdjuster that wraps a date adjuster.
previous(DayOfWeek dayOfWeek) This method is used to return the previous day-of-week adjuster, which adjusts the date to the first occurrence of the specified day-of-week before the date being adjusted.
previousOrSame(DayOfWeek dayOfWeek) This method is used to return the previous-or-same day-of-week adjuster, which adjusts the date to the first occurrence of the specified day-of-week before the date being adjusted unless it is already on that day in which case the same object is returned.

Java




// Implementation of TemporalAdjuster Class Output will
// be different at the time of execution for different
// days. All the dates in the output will be with respect
// to the current date of executing the program
 
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
 
public class TemporalAdjusterExample {
    public static void main(String args[])
    {
        TemporalAdjusterExample gfg
            = new TemporalAdjusterExample();
        gfg.testAdjusters();
    }
 
    public void testAdjusters()
    {
        // to get the current date
        LocalDate date1 = LocalDate.now();
        System.out.println("Today's date is: " + date1);
 
        // to get the next monday
        LocalDate nextTuesday = date1.with(
            TemporalAdjusters.next(DayOfWeek.MONDAY));
        System.out.println("Next Monday is on : "
                           + nextTuesday);
 
        // to get the second saturday of next month
        LocalDate firstInYear = LocalDate.of(
            date1.getYear(), date1.getMonth(), 1);
 
        LocalDate secondSaturday
            = firstInYear
                  .with(TemporalAdjusters.nextOrSame(
                      DayOfWeek.SATURDAY))
                  .with(TemporalAdjusters.next(
                      DayOfWeek.SATURDAY));
       
        // print date of second Saturday of next month
        System.out.println("Second saturday is on : "
                           + secondSaturday);
    }
}


Output:

Today's date is: 2021-02-24
Next Monday is on : 2021-03-01
Second saturday is on : 2021-02-13


Last Updated : 13 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads