Jodatime:从日期获取毫秒数 "Illegal instant"
Jodatime: Getting milliseconds from date yields "Illegal instant"
val date = "01-10-1967"
val pattern = "dd-MM-yyyy"
val formatter = DateTimeFormat.forPattern(pattern)
formatter.parseMillis(date) // this line fails
最后一行失败:
Cannot parse "01-10-1967": Illegal instant due to time zone offset transition (America/Argentina/Buenos_Aires)
知道为什么吗?
(JodaTime 版本为 2.3)
1967 年 10 月 1 日在阿根廷 changed from standard time to summer time,即 00:00。
增加了 1 小时
由于您没有提供具体时间,我假设它默认为 00:00,而那天根本不存在。
比照。 the official faq:
What does 'Illegal instant due to time zone offset transition' mean?
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.
可能的解决方案可能是(取自常见问题解答):
- 使用
LocalDateTime
,因为所有本地日期时间都有效。
- 将
LocalDate
转换为 DateTime
时,请使用 toDateTimeAsStartOfDay()
,因为它可以处理和管理任何间隙。
- 解析时,如果正在解析的字符串没有时区,则使用
parseLocalDateTime()
。
既然你对时间信息不感兴趣,我想你甚至可能想用 formatter.parseLocalDate(date)
替换 formatter.parseMillis(date)
。如果出于某种原因您仍然需要毫秒,this Stack Overflow question 可能会有所帮助。
val date = "01-10-1967"
val pattern = "dd-MM-yyyy"
val formatter = DateTimeFormat.forPattern(pattern)
formatter.parseMillis(date) // this line fails
最后一行失败:
Cannot parse "01-10-1967": Illegal instant due to time zone offset transition (America/Argentina/Buenos_Aires)
知道为什么吗?
(JodaTime 版本为 2.3)
1967 年 10 月 1 日在阿根廷 changed from standard time to summer time,即 00:00。
增加了 1 小时由于您没有提供具体时间,我假设它默认为 00:00,而那天根本不存在。
比照。 the official faq:
What does 'Illegal instant due to time zone offset transition' mean? 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.
可能的解决方案可能是(取自常见问题解答):
- 使用
LocalDateTime
,因为所有本地日期时间都有效。 - 将
LocalDate
转换为DateTime
时,请使用toDateTimeAsStartOfDay()
,因为它可以处理和管理任何间隙。 - 解析时,如果正在解析的字符串没有时区,则使用
parseLocalDateTime()
。
既然你对时间信息不感兴趣,我想你甚至可能想用 formatter.parseLocalDate(date)
替换 formatter.parseMillis(date)
。如果出于某种原因您仍然需要毫秒,this Stack Overflow question 可能会有所帮助。