• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Solution for displaying time by inputting country code

 
tom osayo
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. ok any other country with one timezone like Germany.

Tomas
 
Rok Ć telcer
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
tom osayo
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks. but i need to input the standard iso country code and then display the time accordingly.
Thomas
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic