• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Ranch Hand
Posts: 1489
  • Mark post as helpful
  • send pies
  • 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 ?
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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: 1489
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!