Java 程式將日期格式化為 2019 年 4 月 19 日,下午 1:27


要設定格式並顯示日期時間,你需要使用 DateTimeFormatter。格式風格為 MEDIUM 和 SHORT

DateTimeFormatter formatter = DateTimeFormatter .ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.SHORT);

顯示格式化的日期

formatter.format(LocalDateTime.now()

示例

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class Demo {
   public static void main(String[] args) {
      DateTimeFormatter formatter = DateTimeFormatter .ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.SHORT);
      System.out.println("Formatted Date = "+formatter.format(LocalDateTime.now()));
   }
}

輸出

Formatted Date = Apr 19, 2019, 1:27 PM

更新於:2019 年 7 月 30 日

166 次瀏覽

開啟您的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.