File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Java TimeZone: How to get current date and daylight saving Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java TimeZone: How to get current date and daylight saving" Watch "Java TimeZone: How to get current date and daylight saving" New topic
Author

Java TimeZone: How to get current date and daylight saving

Vikas Aggarwal
Ranch Hand

Joined: Jun 22, 2001
Posts: 140
Hi,

I have a country e.g. "Canada" and I know its offset i.e. GMT-8:00.

My questions are:

1. How can I get the current date in Canada using Java TimeZone or any other method?

2. How can I know whether at this moment (at current datetime) daylight saving behaviour is being observed in Canada or not. (Basically if I get Country to Daylight Saving Rules mapping using a java method, that will solve this problem.)

Is there any example for reference?

Thanks a lot for your help.

Regards,
Vikas
Chris Beckey
Ranch Hand

Joined: Jun 09, 2006
Posts: 116

Canada has more than one time zone, each of which may also have different DST rules (don't know for sure that this applies to Canada but the US has/had a couple of places where DST rules are not standardized).

1.) Get the TimeZone instance from the time zone identifier. If you need a list of time zone identifiers use TimeZone.getAvailableIDs().
2.) To simply print a date in the selected TimeZone, create a SimpleDateFormat in the format you want and call its setTimeZone() with the TimeZone discovered above. Then call format() for a String representation of the date.
3.) Call inDaylightTime(new Date()) on the TimeZone instance obtained earlier to return if it is now DST in the time zone.
Vikas Aggarwal
Ranch Hand

Joined: Jun 22, 2001
Posts: 140
Thanks Chris.

As you said there are different ids for Canada, can I make use of avaiable "offset" to judge which ID I need to use? If I get just one ID or list of all ids matching that offset I will use that and continue with rest of the way told by you.

Thanks,
Vikas
Chris Beckey
Ranch Hand

Joined: Jun 09, 2006
Posts: 116

Before using just the offset to pick the TimeZone, check out this article in Wikipedia on Canadian time zones, which includes DST observance: http://en.wikipedia.org/wiki/Time_in_Canada
I wouldn't bet that Java implements the more obscure time zones but you should be able to pick a TZ from that.

Just for grins, from the Wikipedia article on US time zones:

"Arizona and Hawaii use standard time throughout the year. However:
The Navajo Nation observes DST throughout its entire territory, including the portion that lies in Arizona. But the Hopi Nation, which is entirely surrounded by the Navajo Nation, does not observe DST."

... and your code is not truly internationalized until it supports that! <g>
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Turns out it's pretty easy to support those in Java, Chris. The Hopi use America/Phoenix and the Navajo use America/Shiprock - which is pretty much the same as America/Denver. I bet all the Canadian rules can be handled similarly.

The main issue is making sure the default time zone has been set correctly on the computer - which is really a job for the person setting setting up the computer. If someone buys a new computer at Fry's in Phoenix and then takes it to Shiprock, chances are they'll need to change the time/date settings in the operating system. And quite possibly the OS won't distinguish between America/Shiprock and America/Denver, so they'll just have to settle for Denver. Which is identical in all but name.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java TimeZone: How to get current date and daylight saving