aspose file tools*
The moose likes Beginning Java and the fly likes Convert Local time to UTC and vice versa Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Convert Local time to UTC and vice versa" Watch "Convert Local time to UTC and vice versa" New topic
Author

Convert Local time to UTC and vice versa

O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Hi,

Im working on a program that displays the time in LOCAL TIME or UTC depending on what the user wants.

The problem i have is i dont really understand the difference between localtime, UTC, GMT etc. This is proving to be really difficult to do any work on it.

Could anyone give me some guidelines or refer me to usefull tutorials that i can look at. Also is it possible to do this in Java?

Thanks
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30919
    
158

This page explains UTC/GMT well. I also like The World Clock as it lists timezones throughout the world for easy testing/reference.

In the event you are outside the US, please note that we change our clocks to end Daylight Savings Time this weekend. We changed weekends a couple of years ago. While most versions of Java should be patched by now, you might get unusual results around 2 am US time if you working on an old one. (If you are in the US or already knew this, it's old hat. If not, it could contribute to a bunch of confusion. So I err on the side of saying it.)


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Hi
Thanks for your comment. I spent last night trying to read up about this and i think i understand it a little bit now.

What i would like to do is to convert a supplied date which is on a specific time zone to UTC or from a date that is in UTC to a specific timezone.
Note: The date will not come from the system but will be provided as a String parameter

Here is how i tried to achieve the above

- To convert from local timezone to UTC


- To Convert from UTC to Local Timezone


I tested the above with the following test date



And here was the output
[code]
convertLocalTimeToUTC: LON: The Date in the local time zone 03-11-2008 11:00:00 GMT(+0000)
convertLocalTimeToUTC: LON: The Date in the UTC time zone 03-11-2008 11:00:00 UTC(+0000)
convertLocalTimeToUTC: NBI: The Date in the local time zone 03-11-2008 11:00:00 EAT(+0300)
convertLocalTimeToUTC: NBI: The Date in the UTC time zone 03-11-2008 08:00:00 UTC(+0000)
convertLocalTimeToUTC: BRS: The Date in the local time zone 03-11-2008 11:00:00 CET(+0100)
convertLocalTimeToUTC: BRS: The Date in the UTC time zone 03-11-2008 10:00:00 UTC(+0000)
convertLocalTimeToUTC: MNT: The Date in the local time zone 03-11-2008 11:00:00 EST(-0500)
convertLocalTimeToUTC: MNT: The Date in the UTC time zone 03-11-2008 16:00:00 UTC(+0000)
convertLocalTimeToUTC: LAS: The Date in the local time zone 03-11-2008 11:00:00 PST(-0800)
convertLocalTimeToUTC: LAS: The Date in the UTC time zone 03-11-2008 19:00:00 UTC(+0000)
convertUTCtoLocalTime LON: The Date in the UTC time zone(UTC) 03-11-2008 11:00:00 GMT(+0000)
convertUTCtoLocalTime: LON: The Date in the LocalTime Zone time zone 03-11-2008 11:00:00 GMT(+0000)
convertUTCtoLocalTime NBI: The Date in the UTC time zone(UTC) 03-11-2008 11:00:00 GMT(+0000)
convertUTCtoLocalTime: NBI: The Date in the LocalTime Zone time zone 03-11-2008 14:00:00 EAT(+0300)
convertUTCtoLocalTime BRS: The Date in the UTC time zone(UTC) 03-11-2008 11:00:00 GMT(+0000)
convertUTCtoLocalTime: BRS: The Date in the LocalTime Zone time zone 03-11-2008 12:00:00 CET(+0100)
convertUTCtoLocalTime MNT: The Date in the UTC time zone(UTC) 03-11-2008 11:00:00 GMT(+0000)
convertUTCtoLocalTime: MNT: The Date in the LocalTime Zone time zone 03-11-2008 06:00:00 EST(-0500)
convertUTCtoLocalTime LAS: The Date in the UTC time zone(UTC) 03-11-2008 11:00:00 GMT(+0000)
convertUTCtoLocalTime: LAS: The Date in the LocalTime Zone time zone 03-11-2008 03:00:00 PST(-0800)

[code]

Now as this is my first time playing with dates im not sure if this is correct. I think it is but im not sure if its the correct way of doing it. Is there another standard way of doing this? Do you think that there might be situations where this wont work? And most importantly, what will happen when the clocks move forward or back? Does java recognise this for each city and adjust accordingly?

Thanks
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Originally posted by O. Ziggy:

Now as this is my first time playing with dates im not sure if this is correct. I think it is but im not sure if its the correct way of doing it. Is there another standard way of doing this?


For converting between timezones, it is better to use a Calendar class implementation (such as the GregorianCalendar class) and not the Date class. (At least this is what I have read a number of times and have found from personal experience.) This does seem counterintuitive, but it is the way it is. I'd recommend taking a look at the Converting Times Between Time Zones example at exampledepot.com for an example of how to do this. There are some other Time Zone related examples there as well.

The good news is that Java 7 is suppose to have a much improved Date/Time/Calendar API based on the fine Joda Time API. But until then, you are stuck with the Calendar classes. Or you could use the Joda Time API. However, if you are learning Java, I'd recommend learning how to do this in the native Java APIs before using a third party API.

Originally posted by O. Ziggy:

Do you think that there might be situations where this wont work?

I did not look at your code in detail. Just enough to see that you were using the Date class and not a Calendar class. Given that, I would say the answer to this question is possibly yes. There are a lot of "gotchas" when working with timezones. Most are dealt with in the Calendar class. But again, I only glanced through your code.

Originally posted by O. Ziggy:
And most importantly, what will happen when the clocks move forward or back? Does java recognize this for each city and adjust accordingly?

Yes it does, but with one major caveat. The rules for when date light savings time starts and ends do get changed more frequently than you might expect. This is especially true lately as a lot of countries (and even some individual cities) are adjusting these rules. As Jeanne mentioned, the US change the rules in 2006. Australia just changed their rules this year. So as long as you have the latest patch level for the version of Java you are using, you will be ok. But if you are using an older patch -- say Java 1.5.0_09 instead of the current Java 1.5.0_16; or Java 1.6.0_06 instead of the current Java 1.6.0_10 -- then you might have a problem. For example, take a look at this thread in which someone was bitten by the Australia change this past August.

I hope that helps.
[ November 03, 2008: Message edited by: Mark Vedder ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Convert Local time to UTC and vice versa