• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem in printing locale specific time

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 116
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic