0
为什么如果我使用String.format(Date)
它会在默认时区打印日期,但是当我使用String.format(Calendar)
时,它会在日历的时区中打印。其实,后者是我需要的,但我能确定这种行为会持续吗?对日历与日期使用String.format()
为什么如果我使用String.format(Date)
它会在默认时区打印日期,但是当我使用String.format(Calendar)
时,它会在日历的时区中打印。其实,后者是我需要的,但我能确定这种行为会持续吗?对日历与日期使用String.format()
正如我从实施的String.format(发现至少为JDK 1.5,它具有printDateTime(Object, Locale)
它包含这样的代码:
} 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 {
所以如果论据String.format
是日期,使用Calendar.getInstance(Locale)
,这产生日历在默认时区,如果参数为Calendar
则使用日历的时区。此外,我没有发现任何明确的描述,并且该方法是私人的,所以不可能假定此行为不会改变。
日期永远不会有时区,日历总是会这样做我们的疑问? –
检查此帖:-http://stackoverflow.com/questions/2891361/java-how-to-set-timezone-of-a-java-util-date知道如何为你的日期对象设置'TimeZone'。 –
@RohitJain这是不可能的,日期不包含时区信息。 – dhblah