我正在尝试将时间设置为Calendar实例,但我遇到了奇怪的行为.我们来看看一些例子:
Calendar c = Calendar.getInstance();
//c.setTime(date);
c.set(date.getYear(), (date.getMonth() - 1), date.getDay());
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
当我用setTime()设置时间时,它设置错误的日期.第二个(和已弃用的)集(年,月,日)正常工作.我之前从未见过这个 – 我认为它在两种情况下都使用默认时区,所以它应该是相同的.有人可以向我解释一下吗?
====================编辑:让我们问另一种方式:
Date date = new Date();
Calendar c1 = Calendar.getInstance();
c1.setTime(date);
Calendar c2 = Calendar.getInstance();
c2.set(date.getYear(), (date.getMonth() - 1), date.getDay());
int day1 = c1.get(Calendar.DAY_OF_WEEK);
int day2 = c2.get(Calendar.DAY_OF_WEEK);
// day1 != day2 ---> I'd like to know - WHY?
所以现在的日期如下:
date: Nov 5, 2013 4:27:02 PM
day1: 3
day2: 1
time: 1383665222638
timezone: Europe/Prague