Festivals like Father's Day and Mother's Day can be calculated, or they can be defined as the best Friday of each month to facilitate the arrangement of meetings.
The code copy is as follows:
/**
*
* @param strdate
* Start date, format yyyy-MM-dd HH:mm:ss
* @param cyclePriod
* Repeat interval
* @param loopPriod
* Repeat type, m=month, d=day, y=year, w=week, h=hour, f=minute, s=second
* mn=What day is the positive day of the month, mb=What day is the last day of the month, if mb2 is the second day of the last
* w1..7=What day of each week, mn1w2=The first week of the month, mb2w4=The second Thursday of the month
* The value after w can be multi-values, w135 represents week 1, week 3, and Friday
* @param isLunar
* Whether it is a lunar calendar, the value passed in must be a solar calendar, and the returned Gregorian calendar is still the solar calendar after calculation according to the lunar calendar. Currently, the lunar calendar has different calculations for months and years. Other duplication types are added as needed.
* @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)) // Process the month
{
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)) // Processing day
{
returnValue = DateUtils.calDate(strdate, cyclePriod, 5);
} else if ("y".equalsIgnoreCase(loopPriod)) // Process year
{
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)) // Processing week
{
returnValue = DateUtils.calDate(strdate, cyclePriod, 3);
} else if ("h".equalsIgnoreCase(loopPriod)) // Processing hours
{
returnValue = TimeUtils.addTime(strdate, 0, cyclePriod);
} else if ("f".equalsIgnoreCase(loopPriod)) // Processing minutes
{
returnValue = TimeUtils.addTime(strdate, 1, cyclePriod);
} else if ("s".equalsIgnoreCase(loopPriod)) // Process seconds
{
returnValue = TimeUtils.addTime(strdate, 2, cyclePriod);
} else // Handle unconventional cycles
{
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;
}