I was having some problem when trying to get the list of dates from previous week based on the specified date. Here is my code:
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
}
And the code to get list of dates:
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;
}
However, the result that I am getting are:
Sun Aug 02
Mon Aug 03
Tue Aug 04
Wed Aug 05
Thu Aug 06
Fri Aug 07
Sat Aug 08
The dates are all wrong. It supposed to be from 29th July to 4th August. Any ideas? Thanks!