【发布时间】:2020-12-29 18:53:32
【问题描述】:
我有一个日期字符串,我想将其解析为 Instant
48962-08-06T23:16:59.000Z 但无法做到,因为大多数标准 DateTimeFormatter 规则都不支持它。
参考:https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
这是我尝试做的,使用我的自定义格式化程序
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendValue(YEAR, 5, 10, SignStyle.EXCEEDS_PAD)
.appendLiteral('-')
.appendValue(MONTH_OF_YEAR, 2)
.appendLiteral('-')
.appendValue(DAY_OF_MONTH, 2)
.appendLiteral('T')
.appendValue(HOUR_OF_DAY, 2).appendLiteral(':')
.appendValue(MINUTE_OF_HOUR, 2).appendLiteral(':')
.appendValue(SECOND_OF_MINUTE, 2)
.appendFraction(NANO_OF_SECOND, 0, 9, true)
.appendLiteral('Z')
.toFormatter();
Instant value = Instant.from(formatter.parse("48962-08-06T23:16:59.000Z"));
但它失败了Unable to obtain Instant from TemporalAccessor: {},ISO resolved to +48962-08-06T23:16:59 of type java.time.format.Parsed
【问题讨论】:
-
这5位数字代表什么年份?是来自某个特定文化的日历,还是您真的想解析那么远的未来一年?
-
@Fildor Instant 支持 max to
+1000000000-12-31T23:59:59.999999999Z那么我们如何解析呢? -
@Fildor 对我来说这个问题似乎很明显,尽管他可能应该提到它。那就是“如何获得时间戳为 48962-08-06T23:16:59.000Z 的 Instant 对象?”
-
@deHaar 只是我们需要解析的某个未来日期
-
查看我对 deHaars 答案的评论:高年份数字是一个红鲱鱼,问题与时区有关(或在这种情况下缺少时区)。