import java.util.Calendar;
import java.util.Date;
import org.apache.commons.lang.time.DateUtils; //apache的类,方便日期加减,方法很多,线程安全
import org.apache.commons.lang.time.DateFormatUtils;//apache的类,日期转换,方法很多,线程安全
public class Test {
public static void main(String args[]) {
String date2="20200405";//输入参数
try {
Date date3 = DateUtils.parseDate(date2, new String[] { "yyyyMMdd" });
Calendar calendar = Calendar.getInstance();
calendar.setTime(date3);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
//---如果大于周一
if(dayOfWeek>Calendar.MONDAY){
Date date = DateUtils.addDays(calendar.getTime(), Calendar.MONDAY-dayOfWeek); //----直接日期加减
calendar.setTime(date);
}
//---如果等于周日
else if (dayOfWeek==Calendar.SUNDAY) {
Date date = DateUtils.addDays(calendar.getTime(), -6); //---直接减6天
calendar.setTime(date);
}
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
System.out.println(DateFormatUtils.format(calendar, "yyyyMMdd"));//所得周一日期 转 字符串
System.out.println(DateFormatUtils.format(
DateUtils.addDays(DateUtils.parseDate(DateFormatUtils.format(calendar, "yyyyMMdd"), new String[] { "yyyyMMdd" }),6)
,"yyyyMMdd"))
;//周日 日期 转 字符串
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}