如何获取特定日期的最后一周日(星期六)日期。意味着如果我将输入作为06-04-2012
(MM-dd-YYYY)
输出应06-09-2012
如本日历所示。
Calendar cal = Calendar.getInstance();
int currentDay = cal.get(Calendar.DAY_OF_WEEK);
int leftDays= Calendar.SATURDAY - currentDay;
cal.add(Calendar.DATE, leftDays);
看
public Calendar lastDayOfWeek(Calendar calendar){
Calendar cal = (Calendar) calendar.clone();
int day = cal.get(Calendar.DAY_OF_YEAR);
while(cal.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY){
cal.set(Calendar.DAY_OF_YEAR, ++day);
}
return cal;
}
查看 JODA 时间或(如果您无法添加新库)日历类。