前两天遇到一个问题,,就是客户要求程序在一定周期月数里,提前几天给用户提示周期到了。比如说买了台笔记本,如果笔记本需要每三个月检查维护一次,那么在买笔记本这天后的每三个月里天前几天给销售人员提示,应该和客户联系维护电脑了。
由于每次维修的日期并不能判断得出,所以只能拿笔记本销售日期去比。
想了很久写出一下算法,程序里首先确定销售日期在加上n(n是自然数)个周期月数后的年份和月份与当天年份月份相同,然后判断是否符合提前的天数。
现把代码公开,希望大家指正,并提出更优算法。
/**
* 判断过去的某天(date)和当天是否间隔给定的月数(period)的给定天数(day)内
* @param date 过去的某天
* @param period 周期月数
* @param day 给定天数内
* @return
*/
@SuppressWarnings("deprecation")
public boolean date(Date date, int period, int day) {
boolean sameMonth = false;
Calendar oldCal = Calendar.getInstance();
oldCal.setTime(date);
// 当天日期信息
Date today = new Date();
Calendar newCal = Calendar.getInstance();
newCal.setTime(today);
int oldYear = oldCal.get(Calendar.YEAR);
int newYear = newCal.get(Calendar.YEAR);
//如果年份不一样
if(oldYear != newYear){
while (oldYear != newYear) {
oldCal.add(Calendar.MONTH, period);
// 如果年份不等就把交货日期月数循环加上周期月数
oldYear = oldCal.get(Calendar.YEAR);
int oldMonth = oldCal.get(Calendar.MONTH);
int newMonth = newCal.get(Calendar.MONTH);
if (oldYear == newYear && oldMonth < newMonth) {
// 如果年份相等并且交货月份小于当前月份
while (oldMonth < newMonth) {
oldCal.add(Calendar.MONTH, period);
oldMonth = oldCal.get(Calendar.MONTH);
// 如果月份相等,置true,跳出循环
if (oldMonth == newMonth) {
sameMonth = true;
break;
}
// 如果月份差小于周期月数,跳出循环
if (Math.abs(oldMonth - newMonth) < period) {
break;
}
}
}
}
}else{
//如果是同一年份
int oldMonth = oldCal.get(Calendar.MONTH);
int newMonth = newCal.get(Calendar.MONTH);
if (oldYear == newYear && oldMonth < newMonth) {
// 如果年份相等并且交货月份小于当前月份
while (oldMonth < newMonth) {
oldCal.add(Calendar.MONTH, period);
oldMonth = oldCal.get(Calendar.MONTH);
// 如果月份相等,置true,跳出循环
if (oldMonth == newMonth) {
sameMonth = true;
break;
}
// 如果月份差小于周期月数,跳出循环
if (Math.abs(oldMonth - newMonth) < period) {
break;
}
}
}
}
// 如果年月份都相等
if (sameMonth) {
// 判断两个日期间相差的天数是否小于要求的天数
Date oldDate = oldCal.getTime();
oldDate.setHours(23);
oldDate.setMinutes(59);
oldDate.setSeconds(59);
//判断时间间隔
long diff = oldDate.getTime() - today.getTime();
//System.out.println(diff);
long days = 0;
if (diff < 0){
days = diff;
diff = 0;
}else{
days = diff / (1000 * 60 * 60 * 24);
}
//如果间隔天数小于等于给定天数返回true。
if (days >= 0 && days <= day)
return true;
}
//否则返回false。
return false;
}
由于每次维修的日期并不能判断得出,所以只能拿笔记本销售日期去比。
想了很久写出一下算法,程序里首先确定销售日期在加上n(n是自然数)个周期月数后的年份和月份与当天年份月份相同,然后判断是否符合提前的天数。
现把代码公开,希望大家指正,并提出更优算法。
/**
* 判断过去的某天(date)和当天是否间隔给定的月数(period)的给定天数(day)内
* @param date 过去的某天
* @param period 周期月数
* @param day 给定天数内
* @return
*/
@SuppressWarnings("deprecation")
public boolean date(Date date, int period, int day) {
boolean sameMonth = false;
Calendar oldCal = Calendar.getInstance();
oldCal.setTime(date);
// 当天日期信息
Date today = new Date();
Calendar newCal = Calendar.getInstance();
newCal.setTime(today);
int oldYear = oldCal.get(Calendar.YEAR);
int newYear = newCal.get(Calendar.YEAR);
//如果年份不一样
if(oldYear != newYear){
while (oldYear != newYear) {
oldCal.add(Calendar.MONTH, period);
// 如果年份不等就把交货日期月数循环加上周期月数
oldYear = oldCal.get(Calendar.YEAR);
int oldMonth = oldCal.get(Calendar.MONTH);
int newMonth = newCal.get(Calendar.MONTH);
if (oldYear == newYear && oldMonth < newMonth) {
// 如果年份相等并且交货月份小于当前月份
while (oldMonth < newMonth) {
oldCal.add(Calendar.MONTH, period);
oldMonth = oldCal.get(Calendar.MONTH);
// 如果月份相等,置true,跳出循环
if (oldMonth == newMonth) {
sameMonth = true;
break;
}
// 如果月份差小于周期月数,跳出循环
if (Math.abs(oldMonth - newMonth) < period) {
break;
}
}
}
}
}else{
//如果是同一年份
int oldMonth = oldCal.get(Calendar.MONTH);
int newMonth = newCal.get(Calendar.MONTH);
if (oldYear == newYear && oldMonth < newMonth) {
// 如果年份相等并且交货月份小于当前月份
while (oldMonth < newMonth) {
oldCal.add(Calendar.MONTH, period);
oldMonth = oldCal.get(Calendar.MONTH);
// 如果月份相等,置true,跳出循环
if (oldMonth == newMonth) {
sameMonth = true;
break;
}
// 如果月份差小于周期月数,跳出循环
if (Math.abs(oldMonth - newMonth) < period) {
break;
}
}
}
}
// 如果年月份都相等
if (sameMonth) {
// 判断两个日期间相差的天数是否小于要求的天数
Date oldDate = oldCal.getTime();
oldDate.setHours(23);
oldDate.setMinutes(59);
oldDate.setSeconds(59);
//判断时间间隔
long diff = oldDate.getTime() - today.getTime();
//System.out.println(diff);
long days = 0;
if (diff < 0){
days = diff;
diff = 0;
}else{
days = diff / (1000 * 60 * 60 * 24);
}
//如果间隔天数小于等于给定天数返回true。
if (days >= 0 && days <= day)
return true;
}
//否则返回false。
return false;
}