1
我有以下代码:Java的时区与DST
public static void main(String[] args) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//get current date time with Date()
Date date = new Date();
String currentDate = dateFormat.format(date);
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
System.out.println(sdf.format(getSomeDate(currentDate,TimeZone.getTimeZone("Asia/Omsk"))));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Date getSomeDate(final String str, final TimeZone tz) throws ParseException {
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
sdf.setTimeZone(tz);
return sdf.parse(str);
}
现在的问题是,如果我用我想要的时区(这是“美国/芝加哥”),我得到了完全错误的时间。它的当前时间是+6而不是-6。
那么我该如何解决这个问题,因为现在我必须从GMT + 6开始计算时区,以获得我的程序的正确时间&日期。
此外,Java是否自动合并DST设置?由于世界各地的人都在使用这个功能,所以有些DST时间不同,很难保持正确的时间。
为什么不按照这个答案? [java的如何对变化GMT-时间和本地时间](http://stackoverflow.com/questions/10750279/java-how-to-change-gmt-time-to-local-time) – Serdalis 2013-03-24 13:09:44
是的,Java处理DST。事实上,TimeZone类有几种与DST相关的方法。 Calendar类也使用所有DST设置。 – VGR 2013-03-24 13:22:48
您并未在'sdf'上设置TZ,因此它将格式化日期,就像在本地TZ中一样(JVM的默认TZ)。 – Blake 2013-03-24 15:11:25