如何将UTC偏移量dateformat字符串解析为以|分隔的结果日期象征 [英] How to parse a UTC offset dateformat string into the resulting date separated by | symbol
问题描述
我在尝试解析"2019-12-25T17:00:00-05:00"
时有一个非常特殊的问题
这样它应该给我结果DEC 12 | Thursday | 5:00pm
I have a very peculiar question where I am trying to parse "2019-12-25T17:00:00-05:00"
such that it should give me the result DEC 12 | Thursday | 5:00pm
我通过使用DateTimeFormatter
和LocalDate
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssz", Locale.US);
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("MM d | E | hh:mm a", Locale.US);
LocalDate date = LocalDate.parse("2019-12-25T17:00:00-05:00", inputFormatter);
String formattedDate = outputFormatter.format(date);
contentTextView.setText(formattedDate);
但它因DateTimeParseException: Text '2019-12-25T17:00:00-05:00' could not be parsed at index 19
有人知道它为什么崩溃以及我的输出是否可以提供预期的结果? 谢谢!
Any idea why it's crashing and if my output will render the expected result? Thanks!
推荐答案
您的字符串2019-12-25T17:00:00-05:00
表示UTC
时区,其偏移量 UTC偏移量,因此请使用OffsetDateTime
来解析该字符串
Your String 2019-12-25T17:00:00-05:00
represents UTC
timezone with offset UTC offset, so use OffsetDateTime
for parsing that string
OffsetDateTime odt = OffsetDateTime.parse("2019-12-25T17:00:00-05:00");
System.out.println(odt.format(DateTimeFormatter.ofPattern("MMM d | E | hh:mm a", Locale.US)));
如果要设置特定的时区,可以使用atZoneSameInstant
传递ZoneId
作为eaxmple
If you want to set particular time zone you can use atZoneSameInstant
to pass ZoneId
for eaxmple
ZoneId zone = ZoneId.of("America/Chicago");
ZonedDateTime zdt = odt.atZoneSameInstant(zone);
这篇关于如何将UTC偏移量dateformat字符串解析为以|分隔的结果日期象征的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!