1、每周。例如计算每周一的时间。
2、每月。例如计算每月15号的时间。
3、每年。例如计算每年5月20号的时间。
<span style="white-space:pre"></span>String RWFKSJ = ""; //任务反馈时间
String RWKSSJ = ""; //任务开始时间
String SJTZ = "";
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
Calendar cal = Calendar.getInstance(); // java.util包
Date nowdate = null;
if(!"".equals(ZQ)){
//获取当前日期
String StrnowDate = df.format(new Date());
if("每周".equals(ZQ)){
SJTZ = request.getParameter("SJTZWEEK")==null ?"": request.getParameter("SJTZWEEK");
//获取当前 日期所对应的周X。获取星期Y。
int nowday = getWeekday(userName,StrnowDate);
int zqday = getNum(SJTZ);
String Strzqdate = "";
if(zqday>=nowday){ //若x《y。则计算周y的时间M
cal.add(Calendar.DATE, zqday-nowday); // 日期减 如果不够减会将月变动
Strzqdate = df.format(cal.getTime());
}else { //若x》= y。则计算周y+7天的时间M yyyy年-mm月-dd日
cal.add(Calendar.DATE, zqday+7-nowday); // 日期减 如果不够减会将月变动
Strzqdate = df.format(cal.getTime());
}
nowdate = df.parse(StrnowDate);
cal.setTime(nowdate);
//cal.add(Calendar.DATE , FKSJD); // 日期减 如果不够减会将月变动
Date jsDate = df.parse(df.format(cal.getTime()));
Date zqDate = df.parse(Strzqdate);
if(zqDate.after(jsDate)||zqDate.equals(jsDate)){
RWKSSJ = df.format(zqDate);
}else{
RWKSSJ = getDayDate(zqDate, jsDate);
}
Date rwkssjdate = df.parse(RWKSSJ);
//cal.setTime(rwkssjdate);
//cal.add(Calendar.DATE, -XFSJD);
//RWXFSJ = df.format(cal.getTime());
cal.setTime(rwkssjdate);
cal.add(Calendar.DATE, FKSJD);
RWFKSJ = df.format(cal.getTime());
}else if("每月".equals(ZQ)){
SJTZ = request.getParameter("SJTZMONTH")==null ?"": request.getParameter("SJTZMONTH");
//获取到周期 每月 几号 几天后 8号 10天后
//获取当前月份
//则当月的周期时间为当月+几号
//获取当前日期+10天后的时间
//Date jsDate = cal.getTime();
String month = "";
if(cal.get(Calendar.MONTH)+1<10){
month = "0"+(cal.get(Calendar.MONTH)+1);
}else{
month = ""+(cal.get(Calendar.MONTH)+1);
}
String StrDate = cal.get(Calendar.YEAR) +"-" + month +"-" + SJTZ.replaceAll("[^0-9]","") ;
Date zqDate = df.parse(StrDate);
Date jsDate = df.parse(df.format(cal.getTime()));
if(zqDate.after(jsDate)||zqDate.equals(jsDate)){
RWKSSJ = df.format(zqDate);
}else{
RWKSSJ = getMonthDate(zqDate, jsDate);
}
Date rwkssjdate = df.parse(RWKSSJ);
cal.setTime(rwkssjdate);
cal.add(Calendar.DATE, FKSJD); // 日期加 如果不够加会将月变动
RWFKSJ = df.format(cal.getTime());
}else if("每年".equals(ZQ)){
String SJTZ1 = request.getParameter("SJTZYEAR1")==null ?"": request.getParameter("SJTZYEAR1");
String SJTZ2 = request.getParameter("SJTZYEAR2")==null ?"": request.getParameter("SJTZYEAR2");
SJTZ = SJTZ1 +"-"+ SJTZ2;
//将日期拼接成 yyyy年mm月dd日
String hqrqStr = cal.get(Calendar.YEAR) +"-"+ SJTZ1.replaceAll("[^0-9]","") +"-"+ SJTZ2.replaceAll("[^0-9]","") ;
Date zqDate = df.parse(hqrqStr);
Date jsDate = df.parse(df.format(cal.getTime()));
if(zqDate.after(jsDate)||zqDate.equals(jsDate)){
RWKSSJ = df.format(zqDate);
}else{
RWKSSJ = getYearDate(zqDate, jsDate);
}
Date rwkssjdate = df.parse(RWKSSJ);
cal.setTime(rwkssjdate);
cal.add(Calendar.DATE, FKSJD); // 日期加 如果不够加会将月变动
RWFKSJ = df.format(cal.getTime());
} else {
obj.put("success", false);
obj.put("msg", "操作失败!周期类型["+ZQ+"]不存在!");
}
}else{
obj.put("success", false);
obj.put("msg", "操作失败!周期为空!");
}
<pre name="code" class="java">//实现给定某日期,判断是星期几 public static int getWeekday(String userName,String date){//必须yyyy-MM-dd SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat sdw = new SimpleDateFormat("E"); Date d = null; int num = 0; try { d = sd.parse(date); String day = sdw.format(d); num = getNum(day); } catch (ParseException e) { Tools.errorList(e, userName); }return num; }
<pre name="code" class="java"><span style="white-space:pre"></span>/** * 根据周几得出数字 * @param day * @return */public static int getNum(String day){if ("星期一".equalsIgnoreCase(day)) return 1; else if ("星期二".equalsIgnoreCase(day)) return 2; else if ("星期三".equalsIgnoreCase(day)) return 3; else if ("星期四".equalsIgnoreCase(day)) return 4; else if ("星期五".equalsIgnoreCase(day)) return 5; else if ("星期六".equalsIgnoreCase(day)) return 6; else if ("星期日".equalsIgnoreCase(day)) return 7; return 0;}
<pre name="code" class="java"><span style="white-space:pre"></span>/** * 获取下次执行时间 * @param zqDate * @param jsDate * @return */@SuppressWarnings("static-access")public String getDayDate(Date zqDate,Date jsDate){SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式if(zqDate.after(jsDate)){return df.format(zqDate);}else{Calendar cal = Calendar.getInstance(); // java.util包cal.setTime(zqDate);cal.add(cal.DATE, 7);zqDate = cal.getTime();return getDayDate(zqDate, jsDate);}}
<pre name="code" class="java"><span style="white-space:pre"></span>@SuppressWarnings("static-access")public String getMonthDate(Date zqDate,Date jsDate){SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式if(zqDate.after(jsDate)){return df.format(zqDate);}else{Calendar cal = Calendar.getInstance(); // java.util包cal.setTime(zqDate);cal.add(cal.MONTH, 1);zqDate = cal.getTime();return getMonthDate(zqDate, jsDate);}}
<pre name="code" class="java"><span style="white-space:pre"></span>private String getYearDate(Date zqDate, Date jsDate) {SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式if(zqDate.after(jsDate)){return df.format(zqDate);}else{Calendar cal = Calendar.getInstance(); // java.util包cal.setTime(zqDate);cal.add(cal.YEAR, 1);zqDate = cal.getTime();return getYearDate(zqDate, jsDate);}}