0

当应用 GMT 转换时,我尝试将时间转换回默认值。

public static void main(String[] args) {

    // initial time is: 14:43
    Calendar cal = Calendar.getInstance();    
    cal.set(2013, 8, 1, 14, 43, 0);

    // configuring formatter to apply GMT rule.. now I would have: 18:43 (+4 hours shift)
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");        
    formatter.setTimeZone(TimeZone.getTimeZone("GMT"));

    // but then I want rest my GMT shift back to initial time format

    String startDate = formatter.format(new Date(cal.getTimeInMillis() + TimeZone.getDefault().getRawOffset()));

    System.out.println(startDate);
}

它打印:2013/09/01 13:43:00

但我期望:2013/09/01 14:43:00

方法是什么?

4

2 回答 2

1

你有夏令时的问题。将时区与日期/时间一起打印,您会发现。

于 2013-09-04T22:53:30.700 回答
0

啊..明白了(对不起):

    int offset = cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET);

    String startDate = formatter.format(new Date(cal.getTimeInMillis() + offset));
于 2013-09-04T22:53:29.400 回答