Java 8 – TemporalAdjusters

Learn to use Java 8 TemporalAdjusters where we want to deal with recurring dates such as processing weekly reports, sending automated monthly report outs, etc.

1. TemporalAdjuster interface

The TemporalAdjuster interface and the TemporalAdjusters factory class provide many useful inbuilt adjusters for handling recurring events. The names of most of these tell you directly what they do.

If there are any specific business requirements not solved by provided adjusters, we can build our own custom TemporalAdjuster.

We can apply then to any temporal object with the with() method of that date/time object.

For example, in a team, there is always a weekly meeting every Monday. We want to get the list of dates for the next 5 meetings.

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;

public class TemporalAdjusterExamples 
{
	public static void main(String[] args) 
	{
		LocalDate localDate = LocalDate.of(2020, 5, 9);
		
		List<LocalDate> meetingDates = getWeeklyMeetingDates(localDate, 5);
		System.out.println(meetingDates);
	}
	
	private static List<LocalDate> 
			getWeeklyMeetingDates(LocalDate localDate, int count) 
	{
		List<LocalDate> dates = new ArrayList<>();
		for(int i = 0; i < count; i++)
		{
			localDate = localDate
				.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
			dates.add(localDate);
		}
	    return dates;
	}
}

Program output.

[2020-05-11, 
2020-05-18, 
2020-05-25, 
2020-06-01, 
2020-06-08]

2. Builtin Adjusters

This is the list of defualt provided adjusters for easy use. Visit official Java Doc for detailed information.

Adjuster
Description
firstDayOfMonth()
returns a new date set it to the first day of the current month.
lastDayOfMonth()
returns a new date set it to the last day of the current month.
firstDayOfNextMonth()
returns a new date set it to the first day of the next month.
firstDayOfYear()
returns a new date set it to the first day of the current year.
lastDayOfYear()
returns a new date set it to the last day of the current year.
firstDayOfNextYear()
returns a new date set it to the first day of the next year.
firstInMonth()
returns a new date in the same month with the first matching day-of-week. Such as “first Wednesday in May”.
lastInMonth()
returns a new date in the same month with the last matching day-of-week.
dayOfWeekInMonth()
returns a new date in the same month with the ordinal day-of-week.
next()
returns the date to the first occurrence of the specified day-of-week after the date being adjusted.
previous()
returns the date to the first occurrence of the specified day-of-week before the date being adjusted.

3. Creating Custom TemporalAdjuster

Create a custom adjuster which can be used to get recurring dates adjusted to some business logic. It can be done in two ways:

  • Implement TemporalAdjuster interface
  • Inline Lambda expression

    >

    //Custom temporal adjuster with lambda
    TemporalAdjuster temporalAdjuster = t -> t.plus(Period.ofDays(7));
    
    
    //Custom temporal adjuster with TemporalAdjuster interface
    class NextBirthDay implements TemporalAdjuster 
    {
        @Override
        public Temporal adjustInto(Temporal temporal) 
        {
            return temporal.with(ChronoField.MONTH_OF_YEAR, 11)
            				.with(ChronoField.DAY_OF_MONTH, 22);
        }
    }
    

    Drop me your questions and suggestions related using Java 8 TemporalAdjuster for date manipulation in comments.

    Happy Learning !!

    Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.