• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Get date/time in a timezone corresponding to local time

 
Ranch Hand
Posts: 73
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 73
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
ice is for people that are not already cool. Chill with this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic