我正在尝试将时间设置为 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