带有TimeZone的Java Instant&DateTimeFormatter中的超级怪异“ Y2K20”错误?

问题描述 投票:0回答:1

完整源代码...

package com.ctc.web.client;

import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

import org.junit.Assert;
import org.junit.Test;

public class WebConstantsTest {

    @Test
    public void test() {
        Instant ts = Instant.parse("2020-01-01T04:05:55Z");
        String format = DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm z").withZone(ZoneId.of("America/Los_Angeles")).format(ts);
        Assert.assertEquals("2019-12-31 20:05 PST", format);
    }

}

输出...

org.junit.ComparisonFailure: expected:<20[19]-12-31 20:05 PST> but was:<20[20]-12-31 20:05 PST>
    at org.junit.Assert.assertEquals(Assert.java:115)
    at org.junit.Assert.assertEquals(Assert.java:144)
    at com.ctc.web.client.WebConstantsTest.test(WebConstantsTest.java:16)

这里发生了什么?!生成的值是off off a year

现实检查您可以使用几个可用的在线计算器中的任何一个来验证,当英国人庆祝新年后4小时大部分时间都在睡觉时,加利福尼亚人仍然要坐下来吃饭,因为他们还有4个小时要去。

java timezone java-time
1个回答
2
投票

从上一个SO答案:https://stackoverflow.com/a/14755785/451210您的格式字符串错误,必须使用yyyy而不是YYYY

© www.soinside.com 2019 - 2024. All rights reserved.