LocalDate.ofEpochDay(ChronoField.EPOCH_DAY.range().getMinimum());
上面的代码以抛出以下堆栈跟踪结束:
java.time.DateTimeException: Invalid value for Year (valid values -999999999 - 999999999): -1000018564
at java.time.temporal.ValueRange.checkValidIntValue(ValueRange.java:330)
at java.time.temporal.ChronoField.checkValidIntValue(ChronoField.java:722)
at java.time.LocalDate.ofEpochDay(LocalDate.java:341)
我不明白为什么这个指令不受支持。
实际上,java-time 策略是查询自己的 Temporal 以ValueRange获取一个TemporalFieldthroughrange (TemporalField)方法。并且,对于这种情况LocalDate,不会更改 EpochDay 字段的默认范围。
@Override
public ValueRange range(TemporalField field) {
if (field instanceof ChronoField) {
ChronoField f = (ChronoField) field;
if (f.isDateBased()) {
switch (f) {
case DAY_OF_MONTH: return ValueRange.of(1, lengthOfMonth());
case DAY_OF_YEAR: return ValueRange.of(1, lengthOfYear());
case ALIGNED_WEEK_OF_MONTH: return ValueRange.of(1, getMonth() == Month.FEBRUARY && isLeapYear() == false ? 4 : 5);
case YEAR_OF_ERA:
return (getYear() <= 0 ? ValueRange.of(1, Year.MAX_VALUE + 1) : ValueRange.of(1, Year.MAX_VALUE));
}
return field.range();
}
throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
}
return field.rangeRefinedBy(this);
}
EpochDay 字段有效值应该可转换为LocalDate,否则为什么它在范围内?
Instant和INSTANT_SECONDS字段也会发生同样的情况:Instant.ofEpochSecond(ChronoField.INSTANT_SECONDS.rangeRefinedBy(Instant.now()).getMinimum());
有人能帮助我吗?
相关分类