使用 TemporalAdjusters 類將 LocalDate 調整到下週二的 Java 程式


首先,設定一個 LocalDate

LocalDate localDate = LocalDate.of(2019, Month.FEBRUARY, 2);

現在,使用 next() 方法將 LocalDate 調整到下週二

LocalDate date = localDate.with(TemporalAdjusters.next(DayOfWeek.TUESDAY));

示例

import java.time.DayOfWeek;
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.FEBRUARY, 2);
      System.out.println("Current Date = "+localDate);
      System.out.println("Current Month = "+localDate.getMonth());
      LocalDate date = localDate.with(TemporalAdjusters.firstDayOfMonth());
      System.out.println("First day of month = "+date);
      date = localDate.with(TemporalAdjusters.next(DayOfWeek.TUESDAY));
      System.out.println("Next Tuesday date = "+date);
   }
}

輸出

Current Date = 2019-02-02
Current Month = FEBRUARY
First day of month = 2019-02-01
Next Tuesday date = 2019-02-05

更新於: 30-Jul-2019

115 次瀏覽

開啟您的 職業之旅

完成課程,獲取認證

開始學習
廣告
© . All rights reserved.