Note that there are some explanatory texts on larger screens.

plurals
  1. POConvert time back from UTC to specified TimeZone
    primarykey
    data
    text
    <p>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:</p> <pre><code>Calendar cal = new GregorianCalendar(); cal.setTimeInMillis(DateTimeZone.getDefault().convertLocalToUTC( cal.getTimeInMillis(), false)); </code></pre> <p>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.</p> <p>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.</p> <blockquote> <p>Calendar cal = new GregorianCalendar();</p> <pre><code> 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())); </code></pre> </blockquote> <p><a href="http://www.timeanddate.com/worldclock/city.html?n=660" rel="nofollow">http://www.timeanddate.com/worldclock/city.html?n=660</a> Time zone details for specific timezone</p> <p>Output:</p> <p>Local London Hours: 21</p> <p>Local London Milliseconds: 1381869901339</p> <p>UTC Hours: 20</p> <p>UTC Milliseconds: 1381866301339</p> <p>Vilnius Hours: 22 // <strong>this should be 23 (according to link)</strong></p> <p>Vilnius Milliseconds: 1381866301339</p> <p>Vilnius time is in DST: true // <strong>no, it is not since hours value is not 23</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload