3 回答

TA貢獻1111條經驗 獲得超0個贊
到目前為止,這不是一個好的解決方案,但它適用于我遇到的一種情況,即我要顯示的時間在解析之前位于原始字符串中。
departureScheduled?keep_before("+")?datetime.iso?string("EEEE dd. MMMM yyyy HH:mm")?capitalize
注意keep_before("+")
.
+
上面的解決方案通過刪除in之后的任何內容來工作2019-03-12T16:02:00+02:00
。然后,日期時間解析器假定時間為 UTC。因此,我通過使用一個內置的字符串操作函數來避免整個問題,該函數返回不會被進一步修改的子字符串:2019-03-12T16:02:00(+00:00)
。括號顯示了它是如何被解釋的。
如果有人有更好的答案,我會將其標記為正確,但幾天后,如果沒有給出答案,我會將其標記為正確,以供可能遇到相同問題的任何人使用。

TA貢獻1946條經驗 獲得超4個贊
使用該類java.time.OffsetDateTime來處理偏移量:
public static void main(String[] args) {
String t = "2019-03-12T16:02:00+02:00";
OffsetDateTime odt = OffsetDateTime.parse(t);
System.out.println(odt.format(DateTimeFormatter.ofPattern("EEEE dd. MMMM yyyy HH:mm")));
}
此打印Dienstag 12. M?rz 2019 16:02,翻譯取決于您的系統默認值Locale。
請注意,正確的代碼不會產生您想要的輸出,因為 2019 年 3 月 12 日是星期二而不是星期一。

TA貢獻1831條經驗 獲得超10個贊
上面的答案都不適用于實際實現,所以我做了一些實驗,這很完美(在 Java 中):
ZoneId userTimezone = ZoneId.of("Europe/Rome"); // To provide as parameter
DateTimeFormatter formatter = DateTimeFormatter
.ofPattern("dd/MM/yyyy HH:mm")
.withZone(userTimezone);
String formattedDateTime = <any instance of ZonedDateTime>
.format(formatter);
// OR
String formattedDateTime = <any instance of LocalDateTime> // JVM with UTC time
.atZone(ZoneId.of("UTC")) // Adds timezone info <- Very important
.format(formatter); // Transforms the time at the desired zone
該字符串現在可用于該時區用戶將看到的任何模板/電子郵件。
UTC Value: 20/03/2022 10:00
Output: 20/03/2022 11:00 // Without the timezone at the end
添加回答
舉報