Java获取本周一、上周一、下周一时间 工具类
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;public class DateUtils {private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");public static Date getPreviousWeekMonday(Date date) {Calendar cal = Calendar.getInstance();cal.setTime(getThisWeekMonday(date));cal.add(Calendar.DATE, -7);return cal.getTime();}public static Date getCurrentWeekMonday(Date date) {Calendar cal = Calendar.getInstance();cal.setTime(date);cal.setFirstDayOfWeek(Calendar.MONDAY);cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);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 static void exampleUsage(Date date) {try {System.out.println("今天是" + sdf.format(date));System.out.println("上周一" + sdf.format(getPreviousWeekMonday(date)));System.out.println("本周一" + sdf.format(getCurrentWeekMonday(date)));System.out.println("下周一" + sdf.format(getNextWeekMonday(date)));} catch (Exception e) {e.printStackTrace();}}
}