你是怎么打印出来的Calendar
?
我想你得到的是格林威治标准时间,比午夜晚一小时,因此晚一天。
考虑:
public static void main(String[] args) throws Exception {
int year = 2012;
int month = 2; // eg. for march
int day = 31;
int hrs = 0;
int min = 18;
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Berlin")); // GMT+1
cal.set(year, month, day, hrs, min);
long time = cal.getTimeInMillis();
System.out.println(time);
cal.setTimeInMillis(time);
final DateFormat sdf = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.FULL, SimpleDateFormat.FULL);
System.out.println(sdf.format(cal.getTime()));
}
输出:
1333145915825
Friday, 30 March 2012 23:18:35 o'clock BST
现在,如果我添加TimeZone
到DateFormat
:
final DateFormat sdf = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.FULL, SimpleDateFormat.FULL);
sdf.setTimeZone(TimeZone.getTimeZone("Europe/Berlin"));
System.out.println(sdf.format(cal.getTime()));
我得到:
1333145887761
Saturday, 31 March 2012 00:18:07 o'clock CEST
因此,在格式化时SimpleDateFormat
使用您的默认值,而不是(如您所说的)。TimeZone
TimeZone
Calendar
Calendar.getTime()