Java8 LocalDateTime parsing error

戏子无情 提交于 2019-12-04 13:46:02

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!