Java - Create your own TemporalAdjuster

Introduction

You can use the ofDateAdjuster() method to create your own date adjuster for a LocalDate.

The following code creates a date adjuster to add 3 months and 2 days to the date being adjusted.

TemporalAdjuster adjuster = 
   TemporalAdjusters.ofDateAdjuster((LocalDate date) -> date.plusMonths(3).plusDays(2));

Use the adjuster

LocalDate today = LocalDate.now();
LocalDate dayAfter3Mon2Day = today.with(adjuster);
System.out.println("Today: " + today);
System.out.println("After 3 months and 2 days: " + dayAfter3Mon2Day);

Demo

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

public class Main {
  public static void main(String[] args) {
    TemporalAdjuster adjuster = /*from   w  ww  .  j  ava2s. c  o  m*/
       TemporalAdjusters.ofDateAdjuster((LocalDate date) -> date.plusMonths(3).plusDays(2));

    LocalDate today = LocalDate.now();
    LocalDate dayAfter3Mon2Day = today.with(adjuster);
    System.out.println("Today: " + today);
    System.out.println("After 3 months and 2 days: " + dayAfter3Mon2Day);
    

  }
}

Result

Related Topic