DST changes caused an java.text.ParseException: Unparseable date

前端 未结 2 955
被撕碎了的回忆
被撕碎了的回忆 2021-01-29 09:58

Following is the code snippet which is throwing an exception:

SimpleDateformat dateFormatter = new SimpleDateFormat(\"yyyyMMddHHmm\");
Date date = dateFormatter.         


        
2条回答
  •  悲哀的现实
    2021-01-29 10:19

    You're trying to parse a date/time that didn't occur.

    We now know that this was in the Sydney time zone. At 2am on October 1st 2017 in Sydney, the clocks went forward to 3am. If you were looking at a clock every minute you'd see:

    • 01:58
    • 01:59
    • 03:00
    • 03:01

    So any date/time between 2am (inclusive) and 3am (exclusive) simply didn't occur in that time zone. We don't know what produced the values you're trying to parse, but:

    • If they're timestamps, it would almost certainly be better to both format and parse in UTC. Keep an offset from UTC and potentially a time zone ID if the time zone in which they were produced is important for future analysis.
    • If they're date/time values which aren't linked to any particular time zone, don't parse them as if they were in a time zone. Ideally, use Java 8's java.time package and parse them as LocalDateTime values

提交回复
热议问题