gpt4 book ai didi

某些时区的 Java util dateformat 毫秒似乎不正确

转载 作者:搜寻专家 更新时间:2023-11-01 02:26:46 26 4
gpt4 key购买 nike

据我了解,java.util.Date 将日期存储为从 1970 年 1 月 1 日 00:00 开始的毫秒数......

所以,我试过下面这段代码:

public void testDateFormatBehavior()
{
DateFormat dfNoDay = new SimpleDateFormat(
"MMM d H:m:s zzz yyyy"
);
// This one should be correct as IST = GMT+5.30
String expStrDateBegIST1 = "Jan 01 05:30:01 IST 1970";
// Instead, this one seems to do the conversion to
// Jan 01 00:00:00 GMT 1970
String expStrDateBegIST2 = "Jan 01 02:00:01 IST 1970";
String expStrDateBegUTC = "Jan 01 00:00:01 GMT 1970";
String expStrDateBegCET = "Jan 01 01:00:00 CET 1970";
// Should convert to Jan 01 06:00:00 GMT 1970 as CST = GMT-6
String expStrDateBegCST = "Jan 01 00:00:00 CST 1970";
// This is EST, which is GMT+6...
String expStrDateBegEST = "Jan 01 10:00:00 EST 1970";
try {
Date dBegIST1 = dfNoDay.parse(expStrDateBegIST1);
Date dBegIST2 = dfNoDay.parse(expStrDateBegIST2);
Date dBegUTC = dfNoDay.parse(expStrDateBegUTC);
Date dBegCET = dfNoDay.parse(expStrDateBegCET);
Date dBegCST = dfNoDay.parse(expStrDateBegCST);
Date dBegEST = dfNoDay.parse(expStrDateBegEST);
System.out.println("IST1 milliseconds: " + dBegIST1.getTime());
System.out.println("IST2 milliseconds: " + dBegIST2.getTime());
System.out.println("UTC milliseconds: " + dBegUTC.getTime());
System.out.println("CET milliseconds: " + dBegCET.getTime());
System.out.println("CST milliseconds: " + dBegCST.getTime());
System.out.println("EST milliseconds: " + dBegEST.getTime());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

输出:

IST1 milliseconds: 12601000
IST2 milliseconds: 1000
UTC milliseconds: 1000
CET milliseconds: 0
CST milliseconds: 21600000
EST milliseconds: 0

UTC 毫秒行是正确的,因为我们指定了从 1970 年 1 月 1 日开始的 00:00:01 秒。CET 是正确的。 CST 是正确的,因为该毫秒数是 1970 年 1 月 1 日之后的 6 小时。

但是,IST 转换很奇怪。

http://wwp.greenwichmeantime.com/to/ist/to-gmt/index.htm

IST 似乎是 GMT + 5:30。在我的 Java 代码中,它认为现在是 GMT + 2:00。

此外,EST 不正确。它认为 EST 是 GMT+10:00,而不是 GMT+6:00。 GMT+10:00 是 AEST,不是 EST。 http://wwp.greenwichmeantime.com/time-zone/australia/time-zones/eastern-standard-time/

我做错了什么吗?

最佳答案

Timezone javadoc解释:

Three-letter time zone IDs

For compatibility with JDK 1.1.x, some other three-letter time zone IDs (such as "PST", "CTT", "AST") are also supported. However, their use is deprecated because the same abbreviation is often used for multiple time zones (for example, "CST" could be U.S. "Central Standard Time" and "China Standard Time"), and the Java platform can then only recognize one of them.


问题是您假设时区缩写始终指定特定时区。

“IST”有歧义:印度标准时间/爱尔兰标准时间/以色列标准时间。同样,“EST”可以表示美国东部标准时间或澳大利亚东部标准时间。


Joda-time's timezone list显示明确的 Olson 名称和当前时区偏移量。您最好使用 Joda-time 而不是 java.util 类,并使用 Olson 名称来识别时区。

Should I use Java date and time classes or go with a 3rd party library like Joda Time?讨论了许多人选择使用 Joda-time 的原因。

关于某些时区的 Java util dateformat 毫秒似乎不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20754288/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com