java周期_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;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值