Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes Daylight saving time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Daylight saving time" Watch "Daylight saving time" New topic
Author

Daylight saving time

Mkhaya Tini
Greenhorn

Joined: Oct 06, 2005
Posts: 23
Hi I want to show some information to people who are in different time zones (for now say only in usa).

The information includes timestump that is in GMT. I want to show them in EST, EDT, PST, PDT and so on.

I can ask users to give their timezone. Then on, I want to automate the whole process of showing the time. I know the main part is just adding or subtracting the hour, but the problem comes with daylight saving time. How do I handle that?
I'm not expectating the code (even though a full function that takes GMT and a timezone and give back the time would be ideal), but some headstart would be appreciated.

Thanks in advanced.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You would use SimpleDateFormat to format a timestamp for human viewing. And if you have persuaded your users to give you a java.util.TimeZone object (maybe via a drop-down list) then you can call the SimpleDateFormat's setTimeZone() method with that time zone, and it will automatically format the date as if it were in that time zone. It takes care of daylight saving time and everything. You don't have to add any hours or minutes. So, it's only a few lines of code:
Mkhaya Tini
Greenhorn

Joined: Oct 06, 2005
Posts: 23
Cool.

Thanks a lot.
Mkhaya Tini
Greenhorn

Joined: Oct 06, 2005
Posts: 23


I'm having problem with this line.

for example, if I say



I get an error. Also, is the Date java.util.Date?

Thanks.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Yes, "Date" is java.util.Date. Naturally you can't assign a String to a Date variable. Use SimpleDateFormat to convert a String to a Date... as I already said in an earlier response in this same thread.
 
Consider Paul's rocket mass heater.
 
subject: Daylight saving time
 
Similar Threads
Change Timezone from GMT to EST, the best way to accomplish?
Daylight Saving Time in Java5
Daylight savings - USA
TimeZone and Daylight Saving Time
Handling Different TimeZones/ Daylight Savings Time