import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class test {
public static Date geLastWeekMonday(Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(getThisWeekMonday(date));
cal.add(Calendar.DATE,-7);return cal.getTime();}
public static Date getThisWeekMonday(Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);// 获得当前日期是一个星期的第几天int dayWeek = cal.get(Calendar.DAY_OF_WEEK);if(1== dayWeek){
cal.add(Calendar.DAY_OF_MONTH,-1);}// 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
cal.setFirstDayOfWeek(Calendar.MONDAY);// 获得当前日期是一个星期的第几天int day = cal.get(Calendar.DAY_OF_WEEK);// 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
cal.add(Calendar.DATE, cal.getFirstDayOfWeek()- day);return cal.getTime();}
public static Date getNextWeekMonday(Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(getThisWeekMonday(date));
cal.add(Calendar.DATE,7);return cal.getTime();}
public staticvoidmain(String[] args){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = sdf.parse("2024-05-12");
System.out.println("今天是"+ sdf.format(date));
System.out.println("上周一"+ sdf.format(geLastWeekMonday(date)));
System.out.println("本周一"+ sdf.format(getThisWeekMonday(date)));
System.out.println("下周一"+ sdf.format(getNextWeekMonday(date)));}catch(Exception e){
e.printStackTrace();}}}