Time zone conversion in java

Welcome lonely wanderer, you have stumbled upon the knowledge for timezone handling in the Java catacombs. This has been tried and tested and works on Android to so hopefully this will help you with the tricky task of server datetime -> local device datetime.

First things first, we need to create a timezone object for the current system time.

TimeZone modifier = TimeZone.getDefault();

Next we create a Calendar object, this will handle the modification of the date for us.

Calendar cal = Calendar.getInstance();

Next we create a long variable to hold the current time in milliseconds.

long timeInMil = cal.getTimeInMillis();

Now an int to hold the difference in milliseconds.

int difference = modifier.getOffest(timeinMil);

This will output the time difference the local device from GMT.
Now comes the handy bit, if you need to convert a calendar object to the server (in this case the server is in GMT, if it is not in GMT you can add an additional or substract time away from ‘difference’ before you apply it to the calendar object).

if you want to convert the time to the correct time it will be on the server you do this.

difference *= -1;

To turn a returned date, say from the server and you are wanting to convert it back to the device time. You do not need to change it, handy eh?

Now to bring it all together, you set the calendar object with the date you want to convert then you add the difference to it. That’s all you need to do.

cal.set(year,month, day, hour, min, sec);
cal.add(Calendar.MILLISECOND, difference);

There you have it, timezone conversion on the fly that is highly editable based on what you want to do or your current servers timezone difference inaccordance with GMT.

Here is a quick and dirty example of converting a MySql DATETIME stamp to something you can use for this example.

String date = "1992-02-02 23:22:11";

int year, month, day, hour, min;
int dateFinal[] = {00,00,00,00,00};

String[] dateSpliter = date.split(" ");
String[] dateSpliter2 = dateSpliter[0].split("-");
String[] dateSpliter3 = dateSpliter[1].split(":");

dateFinal[0] = Integer.parseInt(dateSpliter2[0]);
dateFinal[1] = Integer.parseInt(dateSpliter2[1]);
dateFinal[2] = Integer.parseInt(dateSpliter2[2]);
dateFinal[3] = Integer.parseInt(dateSpliter3[0]);
dateFinal[4] = Integer.parseInt(dateSpliter3[1]);

year = dateFinal[0];
month = dateFinal[1];
day = dateFinal[2];

hour = dateFinal[3];
min = dateFinal[4];

Author:Alan Hamlyn

-- Alan Hamlyn Founder of Wuup

Subscribe

Subscribe to our e-mail newsletter to receive updates.

  • http://chirpir.com/2010/04/28/time-zone-conversion-in-java-wuup/ Chirpir News | Time zone conversion in java | Wuup

    [...] Read full story [...]

  • http://topsy.com/trackback?utm_source=pingback&utm_campaign=L2&url=http://www.wuup.co.uk/time-zone-conversion-in-java Tweets that mention Time zone conversion in java | Wuup — Topsy.com

    [...] This post was mentioned on Twitter by Alan Hamlyn and PieDog Media, MarketMeTweet. MarketMeTweet said: RT @Wuup Time zone conversion in java http://bit.ly/drESka [...]

  • http://www.gmtslider.com Tim

    That’s exactly the chunk of code I was looking for! Thanks, dude!