This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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 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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
subject: Get date/time in a timezone corresponding to local time