Reputation: 879
What is the difference between SimpleDateFormat.setTimeZone() and Calendar.setTimeZone()?
Upvotes: 4
Views: 4682
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
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
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