java实现计算周期性提醒的示例
本文导语: 可以计算父亲节、母亲节这样的节日,也可以定义如每月最好一个周五,以方便安排会议。 代码如下:/*** * @param strdate* 开始日期,格式为yyyy-MM-dd HH:mm:ss* @param cyclePriod* 重复间隔* @param loopPriod*...
可以计算父亲节、母亲节这样的节日,也可以定义如每月最好一个周五,以方便安排会议。
/**
*
* @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;
}