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 JavaTimeZone 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.)
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.
Joined: Jun 22, 2001
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.
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>
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.