HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Date Time / Java 8 – TemporalAdjusters

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.

AdjusterDescription
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 !!

    Sourcecode Download

    Share this:

    • Twitter
    • Facebook
    • LinkedIn
    • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

Java Date Time Tutorial

  • Java – Date Time APIs
  • Java – Date Parsing
  • Java – Date Formatting
  • Java 8 – LocalDate
  • Java 8 – LocalTime
  • Java 8 – LocalDateTime
  • Java 8 – ZonedDateTime
  • Java 8 – Period
  • Java 8 – DateTimeFormatter
  • Java 8 – TemporalAdjusters
  • Java 8 – TemporalQuery
  • Java 8 – DayOfWeek
  • Java – Date
  • Java – Locale

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)