我正在尝试解析来自欧洲RSS feed的日期时间。看起来像这样的日期:"06.03.2018 06:00 CET"
。我想要ZonedDateTime
或OffsetDateTime
,但我无法说服Java解析字符串。我在做什么错?
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd.MM.YYYY HH:mm z");
ZonedDateTime zdt = ZonedDateTime.parse("06.03.2018 06:00 CET", dtf);
Caused by: java.time.DateTimeException: Unable to obtain ZonedDateTime from TemporalAccessor: {WeekBasedYear[WeekFields[SUNDAY,1]]=2018, MonthOfYear=3, DayOfMonth=6},ISO,Europe/Paris resolved to 06:00 of type java.time.format.Parsed
// seems to be crashing here: LocalDate.java:363
public static LocalDate from(TemporalAccessor temporal) {
Objects.requireNonNull(temporal, "temporal");
LocalDate date = temporal.query(TemporalQueries.localDate());
if (date == null) { // <== SOMEHOW THIS IS NULL
throw new DateTimeException("Unable to obtain LocalDate from TemporalAccessor: " +
temporal + " of type " + temporal.getClass().getName());
}
return date;
}
temporal
的值是:
{WeekBasedYear[WeekFields[SUNDAY,1]]=2018, MonthOfYear=3, DayOfMonth=6},ISO,Europe/Paris resolved to 06:00
大写Y
代表基于周的年度(请参阅此处的详细说明(,而年度字段则由小写y
表示。有关详细信息,请参见Javadoc:https://docs.oracle.com/javase/8/docs/api/java/time/time/format/datetimeformatter.html#patterns
因此,将您的模式更改为" dd.mm. yyyy hh:mm z"。
另一个细节是时区缩写是模棱两可的。CET由多个国家使用,因此可以映射到多个时区:https://www.timeanddate.com/time/zones/cet#tz-where
某些缩写可能会起作用(又称"不引发异常"(,但是它们会被映射到您无法控制的任意默认值中。在我的JVM中,运行此代码:
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm z");
ZonedDateTime zdt = ZonedDateTime.parse("06.03.2018 06:00 CET", dtf);
产生ZonedDateTime
等于:
2018-03-06T06:00 01:00 [欧洲/巴黎]
CET被映射到"欧洲/巴黎"(这是JVM的某种任意选择,但不能保证您始终得到这一点(。以及其他许多时区也使用了CET,例如"欧洲/柏林","欧洲/马德里"等。
如果要准确控制缩写时想要的时区,则可以创建一个带有选择的集合并使用DateTimeFormatterBuilder
创建DateTimeFormatter
:
// set of preferred zones
Set<ZoneId> preferredZones = new HashSet<ZoneId>();
// my arbitrary choice for CET
preferredZones.add(ZoneId.of("Europe/Berlin"));
DateTimeFormatter dtf = new DateTimeFormatterBuilder()
// date/time
.appendPattern("dd.MM.yyyy HH:mm ")
// zone names
.appendZoneText(TextStyle.SHORT, preferredZones)
// create formatter
.toFormatter(Locale.US);
ZonedDateTime zdt = ZonedDateTime.parse("06.03.2018 06:00 CET", dtf);
现在,ZonedDateTime
将设置为我在我的首选区域中选择的时区:
2018-03-06T06:00 01:00 [欧洲/柏林]