我try 解析邮箱中的日期标头.

有时,邮箱有这样的日期标题:

Mon, 18 Jul 2011 05:05:53 UT

看起来像是在

enter image description here

所以我为此写了一个测试:

public void testParser() throws ParseException {
    SimpleDateFormat FULL_FORMAT = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
    try {
        String baddate = "Mon, 18 Jul 2011 05:05:53 UT";
        System.out.println("NOW: " + FULL_FORMAT.format(new Date()));
        System.out.println("THEN: " + FULL_FORMAT.parse(baddate + "C"));
        System.out.println("---");
        System.out.println(FULL_FORMAT.parse(baddate));
    } catch (Exception e) {
        e.printStackTrace(System.out);
    }
    System.out.println("UTC: " + TimeZone.getTimeZone("UTC"));
    System.out.println("UT: " + TimeZone.getTimeZone("UT"));
}

这是输出结果:

[INFO] Running de.e_nexus.web.rm.mail.TestDateParser
NOW: Thu, 29 Feb 2024 11:50:52 +0100
THEN: Mon Jul 18 07:05:53 CEST 2011
---
java.text.ParseException: Unparseable date: "Mon, 18 Jul 2011 05:05:53 UT"
    at java.base/java.text.DateFormat.parse(DateFormat.java:399)
    at de.e_nexus.web.rm.mail/de.e_nexus.web.rm.mail.TestDateParser.testParser(TestDateParser.java:17)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at org.apache.maven.surefire.junit.PojoTestSetExecutor.executeTestMethod(PojoTestSetExecutor.java:104)
    at org.apache.maven.surefire.junit.PojoTestSetExecutor.execute(PojoTestSetExecutor.java:63)
    at org.apache.maven.surefire.junit.JUnit3Provider.executeTestSet(JUnit3Provider.java:131)
    at org.apache.maven.surefire.junit.JUnit3Provider.invoke(JUnit3Provider.java:93)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:385)
    at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:162)
    at org.apache.maven.surefire.booter.ForkedBooter.run(ForkedBooter.java:507)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:495)
UTC: sun.util.calendar.ZoneInfo[id="UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]
UT: sun.util.calendar.ZoneInfo[id="GMT",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.044 s -- in de.e_nexus.web.rm.mail.TestDateParser

时区UT明显存在(可能与UTC相似或相同),但Java不支持时区UT AFAICT.

将UT重写为UTC是否保存?

推荐答案

100

最有可能是"世界时".但别逼我,有人可能真的会把一款应用程序发到现在是"犹他州时间"的地方(请不要这样做).毕竟,时区是一个政治问题.

"世界时"应称为协调世界时/UTC.TimeandDate.com有a wrap-up of the basics个."UT"的含义可能不明确(它是UTC,但没有协调?是UT1,有人忘了1吗?)然而,它可以在例如IANA TZ数据库REPO文档(example)或描述用于提供TZ规则(example)的文件格式的RFC中找到.和UTC一样,它的意思是太阳时,而不是原子时(TAI).还有更多的相关术语,参见维基百科上的第UTC Etymology节.104:使用"UTC".

100

日期时间/时区库将接受"UTC",但不太可能接受"UT".所以你可能想把它换掉.100:Probably那很安全.你能绝对确定现在不是犹他州时间吗?不是的.


*)犹他州没有自己的TZ,在那里使用US/Mountain(MST/MDT).适当的IANA TZ DB时区标识符应为"America/Denver".

Java相关问答推荐

如何从片段请求数据到活动?在主要活动中单击按钮请求数据?

Java字符串常数池困惑

无法在WebSocket onMessage中捕获错误

Spring Boot 3.2.2中的@Inject和@Resource Remove

我无法获取我的Java Spring应用程序的Logback跟踪日志(log)输出

在Java中,在单个逻辑行中连接列表和单个元素的正确方法是什么?

将Spring Boot 3.2.0升级到3.2.1后查询执行错误

由于在生成器模式中使用泛型,lambda表达式中的返回类型错误

Java页面筛选器问题

如何在JUNIT测试中覆盖ExecutorService?

Java组件项目中的JavaFX对话框国际化

如何获得凌空cookies ,并设置它在下一个请求- android

JavaFX:无论何时显示应用程序,如何更改组件/ node 位置?

在应用getCellFormula()时,Excel引用中的文件名始终为";[1]";使用Apache POI()

如何在ApacheHttpClient 5中为单个请求设置代理?

使用MediaPlayer类在一段时间后停止播放音乐

如何在Maven Central上部署?

Java类型推断:为什么要编译它?

在Spring Boot中使用咖啡因进行缓存

HBox内部的左对齐按钮(如果重要的话,在页码内)