Cannot parse DateTime - Illegal instant due to time zone offset transition (Europe/Berlin)

后端 未结 1 1222
误落风尘
误落风尘 2021-01-05 04:13

Why can\'t I parse the following date?

DateTime.parse(\"2015-03-29 02:35:00\", DateTimeFormat.forPattern(\"yyyy-MM-dd HH:mm:ss\"));

Result:

相关标签:
1条回答
  • 2021-01-05 04:55

    As this link explains:

    Joda-Time only allows the key classes to store valid date-times. For example, 31st February is not a valid date so it can't be stored (except in Partial). The same principle of valid date-times applies to daylight savings time (DST). In many places DST is used, where the local clock moves forward by an hour in spring and back by an hour in autumn/fall. This means that in spring, there is a "gap" where a local time does not exist. The error "Illegal instant due to time zone offset transition" refers to this gap. It means that your application tried to create a date-time inside the gap - a time that did not exist. Since Joda-Time objects must be valid, this is not allowed.

    You are referencing a datetime that does not exist due to daylight savings time changes.

    A solution is to use parseLocalDateTime instead.

    0 讨论(0)
提交回复
热议问题