Reputation: 10151
Why if I use String.format(Date)
it prints date in default timezone, but when I use String.format(Calendar)
it prints in Calendar's timezone. Actually, the latter is what I need, but can I be sure that this behavior will persist?
Upvotes: 0
Views: 223
Reputation: 10151
As I found from implementation of String.format (at least for JDK 1.5, it has printDateTime(Object, Locale)
which contains such code:
} else if (arg instanceof Date) {
// Note that the following method uses an instance of the
// default time zone (TimeZone.getDefaultRef().
cal = Calendar.getInstance(l);
cal.setTime((Date)arg);
} else if (arg instanceof Calendar) {
cal = (Calendar) ((Calendar)arg).clone();
cal.setLenient(true);
} else {
so if argument for String.format
is of Date, Calendar.getInstance(Locale)
is used, which creates calendar in default timezone, if argument is Calendar
then calendar's time zone is used. Moreover, i didn't found any explicit description of this and that method is private, so It's impossible to assume that this behavior won't change.
Upvotes: 1