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
添加回答
举报