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 NIO三大組件與ByteBuffer深入理解及使用
這篇文章主要介紹了Java NIO三大組件與ByteBuffer,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-01-01
數(shù)組與List之間相互轉(zhuǎn)換的方法詳解
本文是對數(shù)組與List之間相互轉(zhuǎn)換的方法進行了詳細的分析介紹,需要的朋友可以過來參考下。希望對大家有所幫助2013-10-10
Spring Cloud Gateway重試機制的實現(xiàn)
這篇文章主要介紹了Spring Cloud Gateway重試機制的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03
Java圖片與二進制相互轉(zhuǎn)換實現(xiàn)示例講解
這篇文章主要介紹了Java圖片與二進制相互轉(zhuǎn)換實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-03-03

