• 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:

How To Convert GMT

 
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am working on an application. Every time a user (User A) submits an article or a message, the Greenwich time of the submission is stored in the database no matter which the time zone the user (User A) is.

Another user (User B who may be in another time zone) may later on retrieve the article or the message submitted by User A.

I want to display the User B local time of the Greenwich time saved in the database. How do I convert the GMT?
 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simply create a java.util.Date or java.util.Calendar object with the GMT time zone specified. Then any date/times you feed into it will be interpreted with that timezone. Or, you can create either of those objects with the default timezone (which is determined by the default locale) and specify when you parse dates into it (using the java.text.DateFormat class) that those dates you're feeding in are GMT. That way, GMT goes in and local time out.

Or, another solution is to convert all of those date times to milliseconds since the epoch, and that number can be used in Calendar or Date objects to set the time.

sev
 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Simply create a java.util.Date or java.util.Calendar object with the GMT time zone specified. Then any date/times you feed into it will be interpreted with that timezone. Or, you can create either of those objects with the default timezone (which is determined by the default locale) and specify when you parse dates into it (using the java.text.DateFormat class) that those dates you're feeding in are GMT. That way, GMT goes in and local time out.



Thanks for your reply. However, I still do not fully understand. The time is saved in the database in Greewich time. That time will be retrieved by users in any time zones. I want to display the time that is local to those users.
 
sever oon
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you create a Date or a Calendar object, you can specify the time zone in which you'd like it to use by default. If you specify GMT, then that's what you'll get out of it.

I don't know how much clearer I can make my response--I can only point out that time zone is a settable property of a Calendar and a Date, just like hour, minute, second, etc...just like any other property. Read the javadocs for details.

sev
 
It's feeding time! Give me the food you were going to give to this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic