我尝试解析电子邮件中的日期标头。
有时电子邮件有这样的日期标题:
Mon, 18 Jul 2011 05:05:53 UT
看起来像javadoc中描述的格式
所以我为此编写了一个测试:
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。
将UT重写为UTC是否保存?
“UT”代表什么?
最有可能是“通用时间”。但不要让我确定这一点,有人实际上可能会在“犹他州时间”发布应用程序(请不要这样做)。时区毕竟是一个政治问题......
“世界时”应称为协调世界时/UTC。 timeanddate.com 有基础知识的总结。 “UT”的含义可能不清楚(是 UTC 但未协调?是 UT1 但有人忘记了 1?)。但是,可以在 IANA tz 数据库存储库文档 (example) 或描述用于提供 tz 规则的文件格式的 RFC (example) 中找到它。与 UTC 一样,它表示太阳时,而不是原子时 (TAI)。还有更多相关术语,请参阅维基百科上的 UTC Etymology 部分。 TL;DR:使用“UTC”。
是否可以将不支持的时区UT重写为UTC?
日期时间/时区库将接受“UTC”,但不太可能接受“UT”。所以你可能想更换它。 TL;DR:可能这是安全的。您能绝对确定现在不是犹他州时间吗?没有。