/**
* 获取当前日期所在周的周六日期
* @return String 格式 yyyy-MM-dd
*/
public static String getSaturdayDate() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
if(cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY ){
cal.set(Calendar.DATE,cal.get(Calendar.DATE)-1 );// 设置时间为前一天
return simpleDateFormat.format(cal.getTime());
}
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
cal.set(Calendar.DATE, cal.get(Calendar.DATE) + 5);
return simpleDateFormat.format(cal.getTime());
}
/**
* 获取当前日期所在周的周日日期
* @return String 格式 yyyy-MM-dd
*/
public static String getWeekLastDate() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
if(cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY ){
return simpleDateFormat.format(cal.getTime());
}
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
cal.set(Calendar.DATE, cal.get(Calendar.DATE) + 6);
return simpleDateFormat.format(cal.getTime());
}
public static void main(String[] args) {
System.out.println(getSaturdayDate());
System.out.println(getWeekLastDate());
}