UnsupportedTemporalTypeException:不支持的字段:InstantSeconds [英] UnsupportedTemporalTypeException: Unsupported field: InstantSeconds

查看:837
本文介绍了UnsupportedTemporalTypeException:不支持的字段:InstantSeconds的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码会生成一个时间戳,然后进行解析.

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**

我将格式化程序替换为 DateTimeFormatter.ISO_INSTANT ,它可以正常工作.产生的实际数据几乎相同.什么是断开连接?

I replace the formatter with DateTimeFormatter.ISO_INSTANT, it works correctly. The actual data produced are nearly identical. What is the disconnect?

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?

推荐答案

问题是您的格式不能完全表示Instant,因为您的格式根本没有用于分钟的表示.格式化程序可以正确地使用Instant并以您的格式输出结果,因为Instant具有所需格式的所有数据,但是您的格式并不具有Instant所需的所有内容.

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.

尝试将模式更改为yyyyMMdd kk:HH:mm:ss.SSS,您将看到代码现在可以工作了.请注意mm的添加.

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.

如果绝对需要无缝模式,则应进行自己的TemporalQuery来从TemporalAccessor中提取所需的信息 在这种情况下,我只需将分钟设置为0:

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:

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();
    }

}

然后我们可以像这样使用此TemporalQuery:

We can then use this TemporalQuery like this:

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());
    }
}

这篇关于UnsupportedTemporalTypeException:不支持的字段:InstantSeconds的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆