Monday, May 11, 2009

Java Time and Daylight Savings

I wanted to create a Java based clock which can show timings of London and Newyork. I got stuck at the point of handling DayLight savings. Searched over Google and found out that Java on its own is not handling DayLight Savings.

For ex:

Calendar londonCal = Calendar.getInstance(TimeZone.getTimeZone("Europe/London"));
String londonTime = londonCal.get(Calendar.HOUR_OF_DAY)+":"+londonCal.get(Calendar.MINUTE)+":"+londonCal.get(Calendar.SECOND);

Calendar newyorkCal = Calendar.getInstance(TimeZone.getTimeZone("US/Central"));
String newyorkTime = newyorkCal.get(Calendar.HOUR_OF_DAY)+":"+newyorkCal.get(Calendar.MINUTE)+":"+newyorkCal.get(Calendar.SECOND);

System.out.println("London: "+londonTime+"\nNewYork: "+newyorkTime);

I thought the above code would have handled DayLight savings but to my surprise i got below result in console:

London: 12:23:17
Newyork: 6:23:17

stating diff in time to be:6 hours

wherein currently its daylight savings so it should have been 5 hours and i should have got a result of

London: 12:23:17
Newyork: 7:23:17

hmm either i am not finding a way to get java calendar handle daylight saving or there isnt one which does tht?