据我所知,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/
我做错了什么吗?
dfNoDay.setTimeZone(TimeZone.getTimeZone("IST"))
,则可以检索到正确的时间。 - Sotirios DelimanolisSimpleDateFormat
使用两种不同的方式来获取和设置TimeZone
呢? - Sotirios Delimanolisjava.util
的日期相关内容了,因为我遇到了太多奇怪的问题,现在也记不清细节了。 - Mike Samuel