從一種時間格式轉換為另一種時間格式 (Converting from one time format to the other)


問題描述

從一種時間格式轉換為另一種時間格式 (Converting from one time format to the other)

是否可以使用 Java 時間包將此日期字符串轉換

3‑6‑2017

到此格式

"Mon Mar 6 00:00:00 EST 2017"

我創建了這兩個格式化程序,但我應該使用哪個時間實例?我已經嘗試過 LocalDate、LocalDateTime 和 ZonedDateTime。

DateTimeFormatter inputFormat = DateTimeFormatter.ofPattern("M‑d‑uuuu");
DateTimeFormatter convertedToFormat = DateTimeFormatter.ofPattern("EEE MMM dd hh:mm:ss zzz yyyy");

參考解法

方法 1:

I believe that you have three issues:

  1. To accept month in either 1 or 2 digits (like 3 for March and 11 for November) you need to specify one pattern letter M, not two. Similarly for day of month. So your input format pattern string should be M‑d‑uuuu (or just M‑d‑u). Edit: You also need d instead of dd in the “converted to” pattern.
  2. To print hour of day (from 00 through 23) you need uppercase HH. Lowercase hh is for clock hour within AM or PM from 01 through 12.
  3. Since your input string did not contain time of day, you need to specify time of day some other way. Similar for time zone since your “converted to” format contains zzz for time zone abbreviation.

So in code I suggest:

    DateTimeFormatter inputFormat = DateTimeFormatter.ofPattern("M‑d‑uuuu");
    DateTimeFormatter convertedToFormat = DateTimeFormatter.ofPattern("EEE MMM d HH:mm:ss zzz yyyy");

    String input = "3‑6‑2017";
    ZonedDateTime startOfDay = LocalDate.parse(input, inputFormat)
            .atStartOfDay(ZoneId.of("America/New_York"));
    String output = startOfDay.format(convertedToFormat);

    System.out.println(output);

Output from my snippet is the desired:

Mon Mar 6 00:00:00 EST 2017

Or to answer your question a little more directly:

… which time instance should I use?

You need two of them: LocalDate for parsing your input and ZonedDateTime for formatting your output. And then a conversion between the two. The one‑arg atStartOfDay method provides the conversion we need. (There is a trick for parsing directly into a ZonedDateTime using default values for time and time zone, but it’s more complicated.)

There are other time zones that will also produce EST as time zone abbreviation. Since your profile says you’re in Boston, I think that America/New_York is the one you want.

(by AfternoonTigerOle V.V.)

參考文件

  1. Converting from one time format to the other (CC BY‑SA 2.5/3.0/4.0)

#java #datetime-format #Time






相關問題

電子郵件地址中帶有 + 字符的 Java 郵件 (Java mail with + character in email address)

如何快速原型化 Java 代碼? (How to quickly prototype Java code?)

如何使用 Maven 在目標(SVN-)服務器上創建 Javadoc? (How to create Javadoc on the target (SVN-) server using Maven?)

為什麼檢查二叉樹有效性的解決方案不起作用? (Why the solution for checking the validity of binary tree is not working?)

Selenium webdriver通過第一個數字找到texy (Selenium webdriver find texy by first digits)

setOnClickListener 沒有在圖像視圖上被調用 (setOnClickListener is not getting called on image view)

繪製多邊形:找不到錯誤 (Drawing Polygon : unable to find error)

半透明 JButton:對像出現在背景中 (Semi-Transparent JButton: Objects appear in Background)

比較同一數組的元素 (Compare elements of the same array)

Java 屏幕截圖小程序 (Java screen capture applet)

Minecraft 1.8.9 Forge Modding 的Java 開發工具包,需要什麼JDK/JRE,代碼是否正確? (Java Development Kit with Minecraft 1.8.9 Forge Modding, What JDK/JRE Is Needed, Is Code Correct?)

java while (resultset.next()) 不返回同一列中的所有數據 (java while (resultset.next()) does not return all data in the same column)







留言討論