• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem in printing locale specific time  RSS feed

 
Montano Mazvik
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to print Current time of US, I tried with following code but
its not working, its printing time in IST which my current locale.


Calendar calendar = Calendar.getInstance(Locale.US);
System.out.println(" Time "+calendar.getTime());
Calendar calendar1 = Calendar.getInstance(new Locale("EN","US"));

Can somebody help ?

Thanks in Advance.
[ March 11, 2008: Message edited by: Montano Mazvik ]
 
Joe Ess
Bartender
Posts: 9439
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Locale is concerned with local text formatting. It does not do time zone resolution.
It should be simple to determine the difference between your time zone and your target time zone (remember the continental US spans 4 time zones) and simply add/subtract that from the Calendar instance you are working with.
 
Chris Beckey
Ranch Hand
Posts: 116
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The short answer is that you need to specify the TimeZone instance referenced by the Calendar (which is implicit in a DateFormat instance).

The longer answer follows:
It seems somewhat strange because of the semantics but the time, as in getTime() is independent of the TimeZone. The time is just the milliseconds since Jan 1, 1970 UTC and will be, nearly, the same on any machine in the world. A Calendar is the endpoint you're getting to.
A Calendar has a TimeZone and and a Locale. The Locale provides the formatting and the text, the TimeZone provides the offset from UTC plus the local daylight savings parameters. A DateFormat has a Calendar reference and does the actual formatting to a String.
The following (ridiculously verbose output) will print all of the Locales and all the times formatted for that Locale. By itself its utility is limited (unless of course you really need to know the Fijian means of referring to local time in Lima, Peru) but it shows the relation.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!