如何將 Java 日期顯示為“12/04/2019”


要以這種形式格式化並顯示日期,即日/月/年,您需要設定模式

dd/MM/yyyy

首先,設定一個 LocalDate

LocalDate localDate = LocalDate.now();

現在格式化並顯示日期為“12/04/2019”

localDate.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"))

示例

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Demo {
   public static void main(String[] args) {
      LocalDate date = LocalDate.now();
      System.out.println("Date = "+date);
      DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
      System.out.println("Formatted Date = "+date.format(formatter));
   }
}

輸出

Date = 2019-04-12
Formatted Date = 12/04/2019

更新於:30-Jul-2019

65 次瀏覽

開啟您的 職業生涯

透過完成該課程獲得認證

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