发布于 2016-07-18 12:25:03 | 126 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Java函数式编程,程序狗速度看过来!

Java程序设计语言

java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE(j2ee), JavaME(j2me), JavaSE(j2se))的总称。


本文分享一个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)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务