java實現(xiàn)計算周期性提醒的示例
可以計算父親節(jié)、母親節(jié)這樣的節(jié)日,也可以定義如每月最好一個周五,以方便安排會議。
/**
*
* @param strdate
* 開始日期,格式為yyyy-MM-dd HH:mm:ss
* @param cyclePriod
* 重復(fù)間隔
* @param loopPriod
* 重復(fù)類型,m=月,d=日,y=年,w=周,h=小時,f=分鐘,s=秒
* mn=月正數(shù)第幾天,mb=月倒數(shù)第幾天,如mb2為倒數(shù)第2天
* w1..7=每周幾,mn1w2=月第一個周2,mb2w4=月倒數(shù)第2個周四
* w后的值可以是多值,w135代表周1、周3、周五
* @param isLunar
* 是否為陰歷,傳入的值必須為陽歷,按陰歷計算后返回的依然是陽歷。目前陰歷只有月和年的計算不同 其他重復(fù)類型根據(jù)需要再添加
* @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 // 處理非常規(guī)周期
{
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;
}
相關(guān)文章
使用Java判定一個數(shù)值是否在指定的開閉區(qū)間范圍內(nèi)
這篇文章主要給大家介紹了關(guān)于使用Java判定一個數(shù)值是否在指定的開閉區(qū)間范圍內(nèi)的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2022-09-09解決SecureRandom.getInstanceStrong()引發(fā)的線程阻塞問題
這篇文章主要介紹了解決SecureRandom.getInstanceStrong()引發(fā)的線程阻塞問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12Spring實戰(zhàn)之Bean定義中的SpEL表達式語言支持操作示例
這篇文章主要介紹了Spring實戰(zhàn)之Bean定義中的SpEL表達式語言支持操作,結(jié)合實例形式分析了Bean定義中的SpEL表達式語言操作步驟與實現(xiàn)技巧,需要的朋友可以參考下2019-12-12