Java 程式將日期格式化為 2019 年 4 月 14 日 下午 01:35 IST


要格式化和顯示日期時間,你需要使用 DateTimeFormatter 並按照以下方式使用模式

DateTimeFormatter dtFormat = DateTimeFormatter.ofPattern("MMM dd yyyy hh:mm a z");

上文中,z 是時區

MMM dd yyyy hh:mm a z

現在,針對區域使用以下內容

ZonedDateTime dateTime = ZonedDateTime.now();

示例

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class Demo {
   public static void main(String[] argv) {
      DateTimeFormatter dtFormat = DateTimeFormatter.ofPattern("MMM dd yyyy hh:mm a z");
      ZonedDateTime dateTime = ZonedDateTime.now();
      String res = dateTime.format(dtFormat);
      System.out.printf("Date = %s %n", res);
   }
}

輸出

Date = Apr 14 2019 01:35 PM IST

更新於:2019 年 7 月 30 日

88 次瀏覽

開啟職業生涯 篇章 

完成課程獲取認證

開始吧
廣告
© . All rights reserved.