代码之家  ›  专栏  ›  技术社区  ›  Alex R

Java Instant&DateTimeFormatter中的超级奇怪的“Y2K20”错误?

  •  0
  • Alex R  · 技术社区  · 5 年前

    完整源代码。。。

    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)
    

    休息一年

    真实性检查 你可以使用几种可用的在线计算器中的任何一种来验证,当英国人在庆祝新年后4个小时基本上都在睡觉时,加州人仍然坐下来吃晚饭,因为他们还有4个小时的时间。

    1 回复  |  直到 5 年前
        1
  •  2
  •   zoli    5 年前

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