我希望将数组列表中的元素解析后转换为
请见下面的方法:
然而,我无法解析该字符串。我得到以下错误:
有没有使用
ZonedDateTime
对象。下面是一个字符串示例。"2017-02-12 06:59:00 +1300"
目前我使用 DateTimeFormatter
:
DateTimeFormatter dateTimeFormatter =
DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss ZZ");
尝试使用parse
方法获取时间:
this.actionTime = dateTimeFormatter.parse(actionTime, ZonedDateTime::from);
请见下面的方法:
public DateCalculatorTest(String actionTime, int expectedDayOfWeek) {
DateTimeFormatter dateTimeFormatter =
DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss ZZ");
DateTimeFormatter localTimeFormatter =
DateTimeFormatter.ofPattern("YYYY-MM-dd");
this.actionTime = dateTimeFormatter.parse(actionTime, ZonedDateTime::from);
this.expectedDayOfWeek = expectedDayOfWeek;
}
然而,我无法解析该字符串。我得到以下错误:
Text '2017-02-12 06:59:00 +1300' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {WeekBasedYear[WeekFields[SUNDAY,1]]=2017, DayOfMonth=12, MonthOfYear=2, OffsetSeconds=46800},ISO resolved to 06:59 of type java.time.format.Parsed
有没有使用
java.time
的方法可以实现这个功能?
YYYY
(基于周的年份)更改为yyyy
(纪元年份),并使用System.out.println(ZonedDateTime.parse("2017-02-12 06:59:00 +1300", dateTimeFormatter));
,输出结果为2017-02-12T06:59+13:00
。 - MadProgrammerOffsetDateTime
而不是ZonedDateTime
更正确,也更简单。 - Ole V.V.