使用 TemporalAdjusters 類的 Java 程式將 LocalDate 調整到下個月的第一天


我們先設定一個日期 −

LocalDate localDate = LocalDate.of(2019, Month.JUNE, 15);

然後,將 LocalDate 調整到下個月的第一天 −

LocalDate day = localDate.with(TemporalAdjusters.firstDayOfNextMonth());

示例

 即時演示

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.JUNE, 15);
      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);
      day = localDate.with(TemporalAdjusters.firstDayOfNextMonth());
      System.out.println("First day of next month = "+day);
   }
}

輸出

Current Date = 2019-06-15
Current Month = JUNE
First day of month = 2019-06-01
First day of next month = 2019-07-01

更新於: 2019 年 7 月 30 日

262 次瀏覽

開啟你的 職業生涯

完成課程以獲得認證

開始
廣告
© . All rights reserved.