Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Daylight saving time

 
Mkhaya Tini
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool.

Thanks a lot.
 
Mkhaya Tini
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic