java UnsupportedTemporalTypeException:不支持的字段:InstantSeconds
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48408688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UnsupportedTemporalTypeException: Unsupported field: InstantSeconds
提问by Steve
I have this code which is producing a timestamp and then parsing.
我有这段代码,它产生一个时间戳,然后解析。
DateTimeFormatter formatter =
DateTimeFormatter
.ofPattern("yyyyMMdd kk:HH:ss.SSSZ")
.withLocale(Locale.getDefault())
.withZone(ZoneId.systemDefault());
Instant now = Instant.now();
String formatted = formatter.format(now);
Instant parsed = formatter.parse(formatted, Instant::from);
When it runs, the last line produces an exception:
当它运行时,最后一行产生一个异常:
java.time.format.DateTimeParseException: Text '20180123 12:12:45.648-0500' could not be parsed: Unable to obtain Instant from TemporalAccessor: {SecondOfMinute=45, NanoOfSecond=648000000, OffsetSeconds=-18000, MilliOfSecond=648, MicroOfSecond=648000, HourOfDay=12},ISO,America/New_York resolved to 2018-01-23 of type java.time.format.Parsed
Caused by: java.time.DateTimeException: Unable to obtain Instant from TemporalAccessor: {SecondOfMinute=45, NanoOfSecond=648000000, OffsetSeconds=-18000, MilliOfSecond=648, MicroOfSecond=648000, HourOfDay=12},ISO,America/New_York resolved to 2018-01-23 of type java.time.format.Parsed
Caused by: java.time.temporal.UnsupportedTemporalTypeException: **Unsupported field: InstantSeconds**
I replace the formatter with DateTimeFormatter.ISO_INSTANT, it works correctly. The actual data produced are nearly identical. What is the disconnect?
我用DateTimeFormatter.ISO_INSTANT替换了格式化程序,它工作正常。产生的实际数据几乎相同。什么是断开连接?
ISO_INSTANT: 2018-01-23T16:51:25.516Z
My Format: 20180119 23:59:59.999-0800
I am required to use my format. What is the problem here?
我需要使用我的格式。这里有什么问题?
采纳答案by IanGabes
The problem is that your format does not completely represent an Instant because your format does not have a representation for minutes at all. The formatter can correctly go from Instant and output the result in your format because an Instant has all of the data that your format requires, but your format does not have everything that an Instant requires.
问题是您的格式并不完全代表 Instant,因为您的格式根本没有分钟的表示。格式化程序可以正确地从 Instant 开始并以您的格式输出结果,因为 Instant 拥有您的格式所需的所有数据,但您的格式并不具备 Instant 所需的一切。
Try changing your pattern to yyyyMMdd kk:HH:mm:ss.SSS
, and you will see that your code now works. Note the addition of mm
.
尝试将您的模式更改为yyyyMMdd kk:HH:mm:ss.SSS
,您将看到您的代码现在可以工作了。注意添加mm
.
If you absolutely require a minuteless pattern, you should make your own TemporalQuery to extract the information you require from the TemporalAccessor
In this case, I simply set minutes to 0
:
如果您绝对需要无分钟模式,则应该制作自己的 TemporalQuery 以从 TemporalAccessor 中提取您需要的信息。在这种情况下,我只需将分钟设置为0
:
public class MyQuery implements TemporalQuery<Instant> {
@Override
public Instant queryFrom(TemporalAccessor temporal) {
LocalDate ld = LocalDate.from(temporal);
LocalTime lt = LocalTime.of(temporal.get(ChronoField.HOUR_OF_DAY), 0, temporal.get(ChronoField.SECOND_OF_MINUTE), temporal.get(ChronoField.NANO_OF_SECOND));
return ZonedDateTime.of(ld, lt, ZoneId.systemDefault()).toInstant();
}
}
We can then use this TemporalQuery like this:
然后我们可以像这样使用这个 TemporalQuery:
public class Test {
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter
.ofPattern("yyyyMMdd kk:HH:mm:ss.SSS")
.withLocale(Locale.getDefault())
.withZone(ZoneId.systemDefault());
Instant now = Instant.now();
String formatted = formatter.format(now);
System.out.println(formatted);
Instant ld = formatter.parse(formatted, new MyQuery());
}
}