Being entirely confused of what's going in with java time and why there are thousands of posts related to this I somehow managed to store time in UTC using jodaTime:
Calendar cal = new GregorianCalendar();
cal.setTimeInMillis(DateTimeZone.getDefault().convertLocalToUTC(
cal.getTimeInMillis(), false));
Since I am using hibernate(and not willing to use adapters for JodaTime to work correctly with version 4) I only use this jodaTime method to convert jdk time to utc. This seems to produce expected result and the local time(current london GMT+1) gets converted to UTC via subtracting 1 from this local time.
So far so good. Then whenever I retrieve my time for specific timezone I get incorrect offset of -2 where it should be -3 including DST.
Calendar cal = new GregorianCalendar();
System.out.println("Local London Hours: "+cal.get(Calendar.HOUR_OF_DAY)); System.out.println("Local London Milliseconds: "+cal.getTimeInMillis()); cal.setTimeInMillis(DateTimeZone.getDefault().convertLocalToUTC( cal.getTimeInMillis(), false)); System.out.println("UTC Hours: "+cal.get(Calendar.HOUR_OF_DAY)); System.out.println("UTC Milliseconds: "+cal.getTimeInMillis()); // Time for specific time zone cal.setTimeZone(TimeZone.getTimeZone("Europe/Vilnius")); System.out.println("Vilnius Hours: "+cal.get(Calendar.HOUR_OF_DAY)); System.out.println("Vilnius Milliseconds: "+cal.getTimeInMillis()); // is this time in DST? - Yes System.out.println("Vilnius time is in DST: "+TimeZone.getTimeZone("Europe/Vilnius").inDaylightTime(cal.getTime()));
http://www.timeanddate.com/worldclock/city.html?n=660 Time zone details for specific timezone
Output:
Local London Hours: 21
Local London Milliseconds: 1381869901339
UTC Hours: 20
UTC Milliseconds: 1381866301339
Vilnius Hours: 22 // this should be 23 (according to link)
Vilnius Milliseconds: 1381866301339
Vilnius time is in DST: true // no, it is not since hours value is not 23