我在尝试根据指定日期获取上周的日期列表时遇到了一些问题。这是我的代码: 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 02Mon Aug 03Tue Aug 04Wed Aug 05Thu Aug 06Fri Aug 07Sat Aug 08日期都错了。应该是从7月29日到8月4日。有任何想法吗?谢谢!
添加回答
举报
0/150
提交
取消