如何在 Java 中將日期格式化為並顯示為“201904”
要將日期格式化為並顯示為“YearMonth”,你需要設定模式
uuuuMM
首先,設定一個 LocalDate
LocalDate localDate = LocalDate.now();
現在將日期格式化為“201904”並顯示出來
localDate.format(DateTimeFormatter.ofPattern("uuuuMM"))示例
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Demo {
public static void main(String[] args) {
LocalDate localDate = LocalDate.now();
System.out.println("Date = "+localDate);
System.out.println("Date (Year and Month) = "+localDate.format(DateTimeFormatter.ofPattern("uuuuMM")));
}
}輸出
Date = 2019-04-19 Date (Year and Month) = 201904
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP