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 Solution for displaying time by inputting country code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Solution for displaying time by inputting country code" Watch "Solution for displaying time by inputting country code" New topic
Author

Solution for displaying time by inputting country code

tom osayo
Greenhorn

Joined: Nov 02, 2009
Posts: 13
Hi

Below you may find a code snippet which is called from a main method where I would like to display the time as per the country code. Somehow it is displaying the current system time but not the time in USA. I am testing from Hungary.

Any solution appreciated.

Thanks
tomas

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You mean like, it's now 9 PM in Hungary so it must be 1 PM in the USA?

That can't possibly work because the USA has more than one time zone.
tom osayo
Greenhorn

Joined: Nov 02, 2009
Posts: 13
Thanks. ok any other country with one timezone like Germany.

Tomas
Rok Štelcer
Ranch Hand

Joined: Nov 03, 2009
Posts: 101
Hi,

All you need is:
- current time
- list of all available TimeZones ( String[] TimeZone.getAvailableIDs() )
- call in a loop df.setTimeZone(timezone[idx]) and print it out to the console

This should work.

Regarding the Locale.
This is just a visual presentation of the time and nothing else (no GMT+/- or DST).


Regards,
Rok


SCJP, SCWCD
tom osayo
Greenhorn

Joined: Nov 02, 2009
Posts: 13
thanks. but i need to input the standard iso country code and then display the time accordingly.
Thomas
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Then you are going to have to do what Rok suggested. For each country go through that list and identify the time zone it uses. Then make a map from country code to time zone, and use that.

I already explained why your original idea wouldn't work. And because it won't work for a considerable number of countries, there would be no point in trying to implement it for the rest. What would it do for a country with several time zones? Throw an exception? Pick one time zone at random? Whatever it did wouldn't be of any use. So there's no point in doing that. So people didn't.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Solution for displaying time by inputting country code
 
Similar Threads
Stumped By HashSet
Date
Problem with Calendar, Locale,TimeZone
how to convert a string to date
Calendar with Locale