File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Get date/time in a timezone corresponding to local time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Get date/time in a timezone corresponding to local time" Watch "Get date/time in a timezone corresponding to local time" New topic
Author

Get date/time in a timezone corresponding to local time

Pranav Raulkar
Ranch Hand

Joined: Apr 20, 2011
Posts: 73

Hi all,

I need to get date/time in another timezone say MST that corresponds to my local date/time. Simply stated if my local date is 15 Jan 2009 8 AM what would be the corresponding date/time in MST.

I tried with calendar, but it seems to give me incorrect date.

Thanks.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6



Note, however, that you need to be careful how you name the target TZ. For instance, "MST" may or may not exist. If you ask for one that does not exist, I think you end up with either local time or GMT.

Also, do you always want MST? Or do you want MST when the U.S. is on Standard Time and MDT when it's on Daylight Time? If you always want it, them you'll probably want something like "GMT-7", or whatever the correct way to express that + correct offset is. If you want to follow the Daylight/Standard transitions, then you'd use something like "America/Denver", or whatever city is present and is in a region that uses DST.

So take this as a starting point, but a) Look into what TZs are actually available that may match what you want, and b) Play around with different times that are both in and out of DST to make sure you get what you expect. (You can create a Calendar, then set it's Month, Day, and Hour accordingly, then call its getTime() method to get a Date object to pass to format().
Pranav Raulkar
Ranch Hand

Joined: Apr 20, 2011
Posts: 73

Thanks Jeff, very brisk reply. I appreciate your pointed out the details and pitfalls that I need to llok out for.

I was aware of certain TZ not being into exsistence, along with the fact that I can use something like GMT-7:00 in getTimeZone.

I wrote the code below



One question again, can this be done using timezone offset? How does the timezone offset work?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Pranav Raulkar wrote:Thanks Jeff, very brisk reply.


You're very welcome.

I was aware of certain TZ not being into exsistence


Most of the rules in common use in the world are probably present. It's just that they might not be accessible by their "common" names that we're familiar with, like "MST", primarily because the same short name might refer to different TZs in different regions of the world.

One question again, can this be done using timezone offset? How does the timezone offset work?


I've never used it, but I suppose you could. The docs for getOffset() say it returns, "the amount of time in milliseconds to add to UTC to get local time", so I suppose if you want to know the difference between to TZs at some instant, you could subtract their getOffset() results. Note, however, that you do NOT want to then add that offset to a Date, thinking that will "convert" the Date from one TZ to another.

A Date object does not have any TZ associated with it. (Another view is that it always has GMT associated with it, but that line of thinking leads to confusion and mistakes, I've found.)

If you are in Denver and I am in LA and we have a friend in London and another in Tokyo, and, at the same instant, we all execute System.currentTimeMillis(), or new Date(), we will all get the exact same result for the long millis-since-epoch value, regardless of our TZs, and regardless of any DST that may or may not be in effect. TZ only comes into play when we format the Date for display. None of the code in my example changed the Date object in any way. The all just said, "Oh, the time is X millis since the epoch? Okay, well, that means that that instant in time is Y if you're in Dever and Z if you're in LA."

I have never found a reason to use the offset from Java's TZ class. That's not to say that you won't, but DON'T use it to adjust the millis of a Date object.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Get date/time in a timezone corresponding to local time