0

I try to convert time back to default one, when GMT conversion was applied.

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);
}

It prints: 2013/09/01 13:43:00

But I expect: 2013/09/01 14:43:00

What is the way?

ses
  • 13,174
  • 31
  • 123
  • 226

2 Answers2

1

You have a problem with daylight savings. Print timezones together with day/time and you will find-out.

Mario Rossi
  • 7,651
  • 27
  • 37
0

Ah.. got it (sorry):

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

    String startDate = formatter.format(new Date(cal.getTimeInMillis() + offset));
ses
  • 13,174
  • 31
  • 123
  • 226