• 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

calendar.getTimeZone().getID() Vs calendar.getTime().toString()

 
Ranch Hand
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("EST"));

System.out.println(calendar.getTimeZone().getID()); --> prints EST

System.out.println(calendar.getTime().toString()); ->prints date and time with IST

Why same calendar object prints two different time zones ?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first one prints the ID of the Calendar's timezone. You have set that to a particular timezone.

The second one prints a Date, not a Calendar. And since a Date doesn't have a timezone, what you see there is your default timezone. Presumably that's not the one you used when you set the Calendar's timezone.
 
kri shan
Ranch Hand
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The second one prints a Date, not a Calendar. And since a Date doesn't have a timezone, what you see there is your default timezone. Presumably that's not the one you used when you set the Calendar's timezone.

So this prints default timezone (IST) but this calendar object sets to EST time zone. It is a kind of misleading information.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The documentation for those classes isn't misleading at all. You've highlighted something which beginners frequently stumble over, but if you look at the code and read the documentation for the classes and methods you used, you'll see how it works. You should try that. I strongly recommend it.
 
It sure was nice of your sister to lend us her car. Let's show our appreciation by sharing this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic