使用 TemporalAdjusters 類將 LocalDate 調整為本年度最後一天的 Java 程式
我們首先設定一個日期
LocalDate localDate = LocalDate.of(2019, Month.FEBRUARY, 11);
現在,將 LocalDate 調整到該年的最後一天
LocalDate day = localDate.with(TemporalAdjusters.lastDayOfYear());
範例
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, 11);
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.lastDayOfMonth());
System.out.println("Last day of month = "+day);
day = localDate.with(TemporalAdjusters.lastDayOfYear());
System.out.println("Last day of year = "+day);
day = localDate.with(TemporalAdjusters.firstDayOfNextMonth());
System.out.println("First day of next month = "+day);
}
}輸出
Current Date = 2019-02-11 Current Month = FEBRUARY First day of month = 2019-02-01 Last day of month = 2019-02-28 Last day of year = 2019-12-31 First day of next month = 2019-03-01
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP