判断某个给定日期在不在本周内

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;


public class TestMain {

public static void main(String args[]){
System.out.println(checkDate("20161023121110"));
}

public static boolean checkDate(String date) {
boolean flag = true;
Date lastWithdrawTime = null;
try {
lastWithdrawTime = DateUtils.parseDate( date, "yyyyMMddHHmmss");
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
try {
// 判断今天是否是周日,如果是周日,计算时间向前退一周
if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
cal.add(Calendar.WEEK_OF_YEAR, -1);
String startDateStr = DateFormatUtils.format(new Date(cal.getTime().getTime() + 24 * 3600 * 1000), "yyyyMMdd");
Date startDate = DateUtils.parseDate(startDateStr, "yyyyMMdd");
String endDateStr = DateFormatUtils.format(new Date(cal.getTime().getTime() + 8 * 24 * 3600 * 1000), "yyyyMMdd");
Date endDate = DateUtils.parseDate(endDateStr, "yyyyMMdd");
if (lastWithdrawTime.after(startDate) && lastWithdrawTime.before(endDate)) {
flag = false;
}
} else {
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
String startDateStr = DateFormatUtils.format(cal.getTime(), "yyyyMMdd");
Date startDate = DateUtils.parseDate(startDateStr, "yyyyMMdd");
String endDateStr = DateFormatUtils.format(new Date(cal.getTime().getTime() + 7 * 24 * 3600 * 1000), "yyyyMMdd");
Date endDate = DateUtils.parseDate(endDateStr, "yyyyMMdd");
if (lastWithdrawTime.getTime() == startDate.getTime()||(lastWithdrawTime.after(startDate) && lastWithdrawTime.before(endDate))) {
flag = false;
}
}
} catch (Exception e) {
flag = true;
}
return flag;
}
}

猜你喜欢

转载自xiyuhanfei.iteye.com/blog/2332865