应用GMT后,Java将时间转换回初始值

时间:2013-09-04 22:48:20

标签: java date gmt

当我应用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

是什么方式?

2 个答案:

答案 0 :(得分:1)

您遇到夏令时问题。打印时区以及日期/时间,您将找到它。

答案 1 :(得分:0)

啊..得到了(对不起):

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

    String startDate = formatter.format(new Date(cal.getTimeInMillis() + offset));