ZetCode

Java TemporalAdjusters

last modified January 27, 2024

Java TemporalAdjusters tutorial shows how to modify Temporal objects in Java with TemporalAdjusters.

Temporal is the base interface type for date, time and offset objects, including LocalDate, LocalTime, LocalDateTime, and Instant.

Java TemporalAdjusters

TemporalAdjusters are used for modifying temporal objects. They allow to find the first or last day of the week, month, or year; the next or previous day of week and so on.

Java TemporalAdjusters example

The following example uses built-in TemporalAdjusters methods.

JavaTemporalAdjustersEx.java
package com.zetcode;

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


public class JavaTemporalAdjustersEx {

    public static void main(String[] args) {

        var localDate = LocalDate.now();
        System.out.printf("today: %s%n", localDate);

        var date1 = localDate.with(TemporalAdjusters.firstDayOfMonth());
        System.out.printf("first day of month: %s%n", date1);

        var date2 = localDate.with(TemporalAdjusters.lastDayOfMonth());
        System.out.printf("last day of month: %s%n", date2);

        var date3 = localDate.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
        System.out.printf("next Monday: %s%n", date3);

        var date4 = localDate.with(TemporalAdjusters.firstDayOfNextMonth());
        System.out.printf("first day of next month: %s%n", date4);

        var date5 = localDate.with(TemporalAdjusters.lastDayOfYear());
        System.out.printf("last day of year: %s%n", date5);

        var date6 = localDate.with(TemporalAdjusters.firstDayOfYear());
        System.out.printf("first day of year: %s%n", date6);

        var date7 = localDate.with(TemporalAdjusters.lastInMonth(DayOfWeek.SUNDAY));
        System.out.printf("last Sunday of month: %s%n", date7);
    }
}

The example presents seven temporal adjusters.

var localDate = LocalDate.now();

We calculate the current local date with LocalDate.now.

var date1 = localDate.with(TemporalAdjusters.firstDayOfMonth());

With firstDayOfMonth we find the first day of the month.

var date2 = localDate.with(TemporalAdjusters.lastDayOfMonth());

With lastDayOfMonth we find the last day of the month.

var date3 = localDate.with(TemporalAdjusters.next(DayOfWeek.MONDAY));

With next and DayOfWeek.MONDAY we find the next Monday.

var date4 = localDate.with(TemporalAdjusters.firstDayOfNextMonth());

With firstDayOfNextMonth we find the first day of the next month.

var date5 = localDate.with(TemporalAdjusters.lastDayOfYear());

With lastDayOfYear we find the last day of the year.

var date6 = localDate.with(TemporalAdjusters.firstDayOfYear());

With firstDayOfYear we find the first day of the year.

var date7 = localDate.with(TemporalAdjusters.lastInMonth(DayOfWeek.SUNDAY));

With lastInMonth and DayOfWeek.SUNDAY we find the last Sunday in the month.

today: 2018-12-03
first day of month: 2018-12-01
last day of month: 2018-12-31
next monday: 2018-12-10
first day of next month: 2019-01-01
last day of year: 2018-12-31
first day of year: 2018-01-01
last Sunday of month: 2018-12-30

Java custom TemporalAdjuster

We can create our custom temporal adjusters.

JavaCustomTemporalAdjusterEx.java
package com.zetcode;

import java.time.LocalDate;
import java.time.Period;
import java.time.temporal.TemporalAdjuster;

public class JavaCustomTemporalAdjusterEx {

    public static void main(String[] args) {

        var localDate = LocalDate.of(2018, 12, 3);

        TemporalAdjuster taj = t -> t.plus(Period.ofDays(14));
        var result = localDate.with(taj);

        System.out.printf("Adding 14 days to %s gives %s", 
            localDate, result);
    }
}

The example creates a date with LocalDate.of. It adds fourteen days to the date and prints the result.

TemporalAdjuster taj = t -> t.plus(Period.ofDays(14));

This is a lambda expression that creates a TemporalAdjuster which adds fourteen days to the created date object.

var result = localDate.with(taj);

We get the result.

Adding 14 days to 2018-12-03 gives 2018-12-17

We can create a temporal adjuster by implementing the TemporalAdjuster interface.

JavaCustomTemporalAdjusterEx2.java
package com.zetcode;

import java.time.LocalDate;
import java.time.temporal.ChronoField;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAdjuster;

class NextChristmas implements TemporalAdjuster {

    @Override
    public Temporal adjustInto(Temporal temporal) {

        return temporal.with(ChronoField.MONTH_OF_YEAR, 12)
                .with(ChronoField.DAY_OF_MONTH, 25);

    }
}

public class JavaCustomTemporalAdjusterEx2 {

    public static void main(String[] args) {

        var now = LocalDate.now();
        System.out.println("Today: " + now);

        var xmas = now.with(new NextChristmas());
        System.out.println("Next XMas: " + xmas);
    }
}

In the example, the custom TemporalAdjuster calculates the date of the next XMas.

@Override
public Temporal adjustInto(Temporal temporal) {

    return temporal.with(ChronoField.MONTH_OF_YEAR, 12)
            .with(ChronoField.DAY_OF_MONTH, 25);
}

We implement the adjustInto method, which returns the Temporal object for the XMas to which the date calling the method should adjust.

Today: 2018-12-03
Next XMas: 2018-12-25

Source

Java TemporalAdjuster - language reference

In this article we have done date and time modifications with Java TemporalAdjusters.

Author

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List all Java tutorials.