Java程序,通过TemporalAdjusters类将LocalDate调整为每月的第一天
我们首先设置一个日期-
LocalDate localDate = LocalDate.of(2019, Month.APRIL, 10);
现在,将LocalDate调整为每月的第一天-
LocalDate day = localDate.with(TemporalAdjusters.firstDayOfMonth());
示例
import java.time.LocalDate; import java.time.Month; import java.time.temporal.TemporalAdjusters; public class Demo { public static void main(String[] args) { LocalDate localDate = LocalDate.of(2019, Month.APRIL, 10); System.out.println("Current Date = "+localDate); System.out.println("Current Month = "+localDate.getMonth()); LocalDate day = localDate.with(TemporalAdjusters.firstDayOfMonth()); System.out.println("First day of month = "+day); } }
输出结果
Current Date = 2019-04-10 Current Month = APRIL First day of month = 2019-04-01