Java Data Type How to - Adjust LocalDate to first Day Of Month








Question

We would like to know how to adjust LocalDate to first Day Of Month.

Answer

import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.TemporalAdjusters;
//from   w  w  w .  jav a  2 s  . c  o  m
public class Main {
  public static void main(String[] args) {
    LocalDate date = LocalDate.of(2014, Month.FEBRUARY, 25); // 2014-02-25

    // first day of February 2014 (2014-02-01)
    LocalDate firstDayOfMonth = date.with(TemporalAdjusters.firstDayOfMonth());

    System.out.println(firstDayOfMonth);
  }
}

The code above generates the following result.