Java8 LocalDateTime解析错误 [英] Java8 LocalDateTime parsing error

查看:3865
本文介绍了Java8 LocalDateTime解析错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析以下时间戳字符串 03-feb-2014 13:16:31 使用 java.time 但它是抛出一个错误。这是我的代码。

I am trying to parse the following timestamp string 03-feb-2014 13:16:31 using java.time but it is throwing an error. Here is my code.

String timestamp = "03-feb-2014 13:16:31";

DateTimeFormatter format;
DateTimeFormatterBuilder formatBuilder = new DateTimeFormatterBuilder();
formatBuilder.parseCaseInsensitive();   
formatBuilder.append(DateTimeFormatter.ofPattern("dd-MMM-YYYY HH:mm:ss"));
format = formatBuilder.toFormatter();

LocalDateTime localdatetime = LocalDateTime.parse(timestamp, format);

但是我收到以下错误。

Exception in thread "main" java.time.format.DateTimeParseException: Text '03-feb-2014 13:16:31' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {DayOfMonth=3, MonthOfYear=2, WeekBasedYear[WeekFields[SUNDAY,1]]=2014},ISO resolved to 13:16:31 of type java.time.format.Parsed
    at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1920)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1855)
    at java.time.LocalDateTime.parse(LocalDateTime.java:492)
    at com.target.util.CntrlmProcessor.main(CntrlmProcessor.java:24)
Caused by: java.time.DateTimeException: Unable to obtain LocalDateTime from TemporalAccessor: {DayOfMonth=3, MonthOfYear=2, WeekBasedYear[WeekFields[SUNDAY,1]]=2014},ISO resolved to 13:16:31 of type java.time.format.Parsed
    at java.time.LocalDateTime.from(LocalDateTime.java:461)
    at java.time.format.Parsed.query(Parsed.java:226)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)

从错误中,它看起来像库已经能够解析字符串,因为它将时间戳记中的所有字段分离出来,但是似乎有一些我错过的东西。

From the error it looks like the library has been able to parse the string as it separated out all the fields from the timestamp, but there seems to be something that I am missing.

我试图解析一下时间戳的时间部分,而且工作正常。

I tried to parse just the time part of the timestamp alone and that is working just fine.

推荐答案

如果您使用 yyyy 而不是 YYYY 在你的模式,你给的代码工作。 YYYY 是周年,通常只有在您同时指定周数和星期几(例如 YYYY-ww-EEE )。这很罕见。

If you use yyyy instead of YYYY in your pattern, the code you've given works. YYYY is "week-based year" which would normally only be used if you're also specifying week number and day-of-week (e.g. a pattern of YYYY-ww-EEE). This is pretty rare.

请注意,即使只是年有 yyyy uuuu - yyyy 是年代(总是非负数 - 在公历中一直是积极的),而$ code > uuuu 是一种无耻的年 - 例如,5BCE是-4作为无耻的一年。如果您不需要在常规时代(或其他日历系统中的日期)之前处理日期,则可能不需要担心。

Note that even just "year" has yyyy and uuuu - yyyy is "year of era" (which is always non-negative - and always positive in the Gregorian calendar) whereas uuuu is a sort of "eraless year" - for example, 5BCE is -4 as an eraless year. If you don't need to deal with dates before the common era (or dates in other calendar systems) you probably don't need to worry about this.

我会还建议将代码重写为:

I would also suggest rewriting your code as:

DateTimeFormatter format = new DateTimeFormatterBuilder()
    .parseCaseInsensitive()
    .appendPattern("dd-MMM-yyyy HH:mm:ss")
    .toFormatter();

...只是为了简单起见。

... just for simplicity.

这篇关于Java8 LocalDateTime解析错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆