java实现计算周期性提醒的示例
可以计算父亲节、母亲节这样的节日,也可以定义如每月最好一个周五,以方便安排会议。
/**
*
* @param strdate
* 开始日期,格式为yyyy-mm-dd hh:mm:ss
* @param cyclepriod
* 重复间隔
* @param looppriod
* 重复类型,m=月,d=日,y=年,w=周,h=小时,f=分钟,s=秒
* mn=月正数第几天,mb=月倒数第几天,如mb2为倒数第2天
* w1..7=每周几,mn1w2=月第一个周2,mb2w4=月倒数第2个周四
* w后的值可以是多值,w135代表周1、周3、周五
* @param islunar
* 是否为阴历,传入的值必须为阳历,按阴历计算后返回的依然是阳历。目前阴历只有月和年的计算不同 其他重复类型根据需要再添加
* @return
*/
public static string nexttime(string strdate, int cyclepriod,
string looppriod, boolean islunar) {
string returnvalue = "";
int[] dates = dateutils.gendate(strdate);
chinesecalendar ccalendar = new chinesecalendar();
ccalendar.setgregorianyear(dates[0]);
ccalendar.setgregorianmonth(dates[1]);
ccalendar.setgregoriandate(dates[2]);
if ("m".equalsignorecase(looppriod)) // 处理月
{
if (islunar) {
for (int i = 0; i < cyclepriod; i++) {
ccalendar.nextchinesemonth();
}
returnvalue = dateutils.gendate(ccalendar.getgregorianyear(),
ccalendar.getgregorianmonth(),
ccalendar.getgregoriandate());
} else {
returnvalue = dateutils.caldate(strdate, cyclepriod, 2);
}
} else if ("d".equalsignorecase(looppriod)) // 处理日
{
returnvalue = dateutils.caldate(strdate, cyclepriod, 5);
} else if ("y".equalsignorecase(looppriod)) // 处理年
{
if (islunar) {
ccalendar.addchineseyear(cyclepriod);
returnvalue = dateutils.gendate(ccalendar.getgregorianyear(),
ccalendar.getgregorianmonth(),
ccalendar.getgregoriandate());
} else {
returnvalue = dateutils.caldate(strdate, cyclepriod, 1);
}
} else if ("w".equalsignorecase(looppriod)) // 处理周
{
returnvalue = dateutils.caldate(strdate, cyclepriod, 3);
} else if ("h".equalsignorecase(looppriod)) // 处理小时
{
returnvalue = timeutils.addtime(strdate, 0, cyclepriod);
} else if ("f".equalsignorecase(looppriod)) // 处理分钟
{
returnvalue = timeutils.addtime(strdate, 1, cyclepriod);
} else if ("s".equalsignorecase(looppriod)) // 处理秒
{
returnvalue = timeutils.addtime(strdate, 2, cyclepriod);
} else // 处理非常规周期
{
if ("m".equalsignorecase(stringutils.left(looppriod, 1))) {
string mnb = looppriod.substring(1, 2);
string wnb = "";
int mnbvalue = 0;
int wnbvalue = 0;
if (looppriod.indexof("w") > 1) {
wnb = looppriod.substring(looppriod.indexof("w") + 1,
looppriod.indexof("w") + 2);
mnbvalue = integer.parseint(looppriod.substring(2,
looppriod.indexof("w")));
wnbvalue = integer.parseint(looppriod.substring(
looppriod.indexof("w") + 1, looppriod.length()));
if ("n".equalsignorecase(mnb)) {
returnvalue = getbeforeweekday(strdate, mnbvalue,
wnbvalue);
} else if ("b".equalsignorecase(mnb)) {
returnvalue = getbackweekday(strdate, mnbvalue,
wnbvalue);
}
} else {
mnbvalue = integer.parseint(looppriod.substring(2,
looppriod.length())) - 1;
if ("n".equalsignorecase(mnb)) {
returnvalue = caldate(givemonthfirst(strdate),
mnbvalue, 5);
} else if ("b".equalsignorecase(mnb)) {
returnvalue = caldate(givemonthlast(strdate),
-mnbvalue, 5);
}
}
} else if ("w".equalsignorecase(stringutils.left(looppriod, 1))) {
string week = stringutils.right(looppriod,
looppriod.length() - 1);
strdate = caldate(strdate, cyclepriod - 1, 3);
while (true) {
strdate = caldate(strdate, 1, 5);
if (week.indexof(string.valueof(getweekday(strdate))) >= 0) {
returnvalue = strdate;
break;
}
}
}
}
return returnvalue;
}