我在尝试根据指定日期获取上周的日期列表时遇到了一些问题。这是我的代码:
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd");
Date refDate = null;
try {
refDate = formatter.parse("Sun Aug 05");
} catch (ParseException e) {
e.printStackTrace();
}
Date[] days = reservationViewModel.getDaysOfWeek(refDate, Calendar.getInstance().getFirstDayOfWeek());
for (Date day : days) {
// display
}
以及获取日期列表的代码:
public static Date[] getDaysOfWeek(Date refDate, int firstDayOfWeek) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(refDate);
calendar.set(Calendar.DAY_OF_WEEK, firstDayOfWeek);
Date[] daysOfWeek = new Date[7];
for (int i = 0; i < 7; i++) {
daysOfWeek[i] = calendar.getTime();
calendar.add(Calendar.DAY_OF_MONTH, 1);
}
return daysOfWeek;
}
但是,我得到的结果是:
Sun Aug 02
Mon Aug 03
Tue Aug 04
Wed Aug 05
Thu Aug 06
Fri Aug 07
Sat Aug 08
日期都错了。应该是从7月29日到8月4日。有任何想法吗?谢谢!
相关分类