如何在 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

更新於:2019 年 7 月 30 日

85 次瀏覽

開啟您的職業生涯

透過完成課程獲得認證

入門
廣告
© . All rights reserved.