Reputation: 3502
I'm trying to take a Date and update it based on another timezome and I've written this code below. I noticed then I set the new time zone which is CST a call to getHour reflects the update but when getTime() the date does not reflect the time based on the timezone last set.
TimeZone zone = TimeZone.getTimeZone(TimeZoneConstants.AmericaNewYork);
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone(TimeZoneConstants.AmericaNewYork));
System.out.println(cal.get(Calendar.HOUR_OF_DAY));
System.out.println(DateHelper.formatDate(cal.getTime(), DateHelper.FORMAT_TIME_LONG));
Calendar cal2 = Calendar.getInstance();
TimeZone zone2 = cal2.getTimeZone();
cal.setTimeZone(zone2);
System.out.println(cal.get(Calendar.HOUR_OF_DAY));
System.out.println(DateHelper.formatDate(cal.getTime(), DateHelper.FORMAT_TIME_LONG));
16 3:14:36 PM CST 15 3:14:36 PM CST
From this the console output is below as you can see the hour is updated but the Date object returned from getTime() is not. I can write a uitl method to get what I want but am wondering why this happens by defauit? - Duncan Krebs
Upvotes: 0
Views: 806
Reputation: 47994
You never changed the time, in terms of raw milliseconds since epoch, at all. Changing the timezone just changes the 'human' representation. 3:14pm Central and 4:14pm Eastern are exactly the same when represented by a java.util.Date
. They're both the same amount of raw time since epoch. If you want the human readable representation to be in Eastern time, you need to tell that to the DateHelper doing the formatting.
Upvotes: 1