I need to get dates on Monday and Friday last week. To do this, I get the Monday date for this week and subtract 7 days. This gives me a date on Monday last week.
To get the date on Friday, I have to add 4. This confused me a bit, because for some reason the first day of the week is Sunday, not Monday in the UK.
Anyway, this is how I get the dates.
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
cal.add(Calendar.DAY_OF_WEEK, -7);
cal.set(Calendar.HOUR_OF_DAY,0);
cal.set(Calendar.MINUTE,0);
cal.set(Calendar.SECOND,0);
cal.set(Calendar.MILLISECOND,0);
cal.add(Calendar.DAY_OF_WEEK, 4);
cal.set(Calendar.HOUR_OF_DAY,23);
cal.set(Calendar.MINUTE,59);
cal.set(Calendar.SECOND,59);
cal.set(Calendar.MILLISECOND,0);
The above works, but I'm interested in something wrong with the logic. That is, will it work in February, leap years, etc.
Feel free to suggest a better solution / approach.
thank
ziggy source
share