Ye Wint
Ye Wint

Reputation: 879

Difference between SimpleDateFormat.setTimeZone() and Calendar.setTimeZone()

What is the difference between SimpleDateFormat.setTimeZone() and Calendar.setTimeZone()?

Upvotes: 4

Views: 4682

Answers (3)

SamIAm
SamIAm

Reputation: 2491

Both of these methods do exactly the same thing.

SimpleDateFormat.setTimeZone(TimeZone zone)

is essentially a method inherited from java.text.DateFormat. When

DateFormat.setTimeZone(TimeZone zone) 

is called, it will run

 getCalendar().setTimeZone(TimeZone zone)

which is the exact same thing as

Calendar.setTimeZone(TimeZone zone)

They are both calling the same method which would set the timezone.

Upvotes: 2

Vishal
Vishal

Reputation: 3279

setTimeZone is method defined in DateFormat class which internally holds a Calendar object. So it modified Calender object and set's it TimeZone, where as Calendar.setTimeZone set time zone of the existing calender...

Both of these methods are instance methods..

Upvotes: 0

Alexander Pavlov
Alexander Pavlov

Reputation: 32296

SimpleDateFormat will use the specified timezone during formatting, while Calendar will just consider the given timezone as that in which the respective date/time are specified. The Calendar's timezone will be converted into the SimpleDateFormat's timezone when formatting the Calendar instance.

Upvotes: 2

Related Questions