|
|
@@ -5,7 +5,14 @@ import org.apache.commons.lang3.time.DateFormatUtils; |
|
|
|
import java.lang.management.ManagementFactory; |
|
|
|
import java.text.ParseException; |
|
|
|
import java.text.SimpleDateFormat; |
|
|
|
import java.time.Instant; |
|
|
|
import java.time.LocalDate; |
|
|
|
import java.time.LocalTime; |
|
|
|
import java.time.ZoneId; |
|
|
|
import java.time.format.DateTimeFormatter; |
|
|
|
import java.util.Calendar; |
|
|
|
import java.util.Date; |
|
|
|
import java.util.Locale; |
|
|
|
|
|
|
|
/** |
|
|
|
* 时间工具类 |
|
|
@@ -133,4 +140,107 @@ public final class DateUtils extends org.apache.commons.lang3.time.DateUtils { |
|
|
|
return day + "天" + hour + "小时" + min + "分钟"; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 指定日期字符串加/减指定月数 |
|
|
|
* @param dataTime |
|
|
|
* @param months |
|
|
|
* @return |
|
|
|
* @throws ParseException |
|
|
|
*/ |
|
|
|
public static Date getLastMonthDate(String dataTime,int months) throws ParseException { |
|
|
|
//时间字符串转 LocalDate 类型 |
|
|
|
LocalDate today = LocalDate.parse(dataTime, DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MM_SS)); |
|
|
|
//当前月份+(-1) |
|
|
|
today = today.plusMonths(months); |
|
|
|
Instant instant = today.atTime(LocalTime.MIDNIGHT).atZone(ZoneId.systemDefault()).toInstant(); |
|
|
|
Date date = Date.from(instant); |
|
|
|
return date; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 计算下一个循环周期 |
|
|
|
* |
|
|
|
* @param beginDate 开始日期:格式yyyy-MM-dd HH:mm:ss |
|
|
|
* @param cycleType 周期类型0-每周 1-每月 |
|
|
|
* @param cycleDate 循环日期:如果周期类型是每周,那么循环日期日期值范围为:1-7,如果周期类型为每月,那么周期日期值范围为:1-31 |
|
|
|
* @return |
|
|
|
* @throws ParseException |
|
|
|
*/ |
|
|
|
public static String getNextCycleDay(String beginDate, String cycleType, String cycleDate) throws ParseException { |
|
|
|
if (StringUtils.isNotEmpty(cycleType) && "0".equals(cycleType)) { |
|
|
|
if (StringUtils.isNotEmpty(cycleDate) && (Integer.valueOf(cycleDate) < 0 || Integer.valueOf(cycleDate) > 7)) { |
|
|
|
throw new RuntimeException("循环周期类型为周,循环日期只能再1~7范围内!"); |
|
|
|
} |
|
|
|
} else if (StringUtils.isNotEmpty(cycleType) && "1".equals(cycleType)) { |
|
|
|
if (StringUtils.isNotEmpty(cycleDate) && (Integer.valueOf(cycleDate) < 0 || Integer.valueOf(cycleDate) > 31)) { |
|
|
|
throw new RuntimeException("循环周期类型为月,循环日期只能再1~31范围内!"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
Date dateBegin = DateUtils.parseDate(beginDate, YYYY_MM_DD_HH_MM_SS); |
|
|
|
String beginDateTime = DateUtils.parseDateToStr("HH:mm:ss", dateBegin);//开始时间点 |
|
|
|
String nextDate = "";//下一个日期 |
|
|
|
Date date = null; |
|
|
|
|
|
|
|
String today = DateUtils.getDate();//获取今天的日期 |
|
|
|
//开始日期大于当前,取开始日期进行比较,否则取当天进行比较 |
|
|
|
if (beginDate.compareTo(today) > 0) { |
|
|
|
date = DateUtils.parseDate(beginDate, YYYY_MM_DD_HH_MM_SS); |
|
|
|
} else { |
|
|
|
date = new Date(); |
|
|
|
} |
|
|
|
if ("0".equals(cycleType)) { |
|
|
|
//每周 |
|
|
|
//如果指定周几大于今天所属的周几,则取本周的周几 |
|
|
|
//如果指定周几小于今天所属的周几,则取下个周的周几 |
|
|
|
Calendar cal = Calendar.getInstance(Locale.CHINA); |
|
|
|
cal.setTime(date); |
|
|
|
cal.setFirstDayOfWeek(Calendar.MONDAY);//将每周第一天设为星期一,默认是星期天 |
|
|
|
int week = cal.get(Calendar.DAY_OF_WEEK) - 1;//获取指定日期的周几 |
|
|
|
if (week == 0) week = 7; |
|
|
|
if (Integer.valueOf(cycleDate) >= week) {//如果指定的周几大于等于当前周几,去本周的周几 |
|
|
|
cal.set(Calendar.DAY_OF_WEEK, Integer.valueOf(cycleDate) + 1);//星期 |
|
|
|
} else { |
|
|
|
cal.add(Calendar.WEEK_OF_MONTH, 1);//周数加1,即下周 |
|
|
|
cal.set(Calendar.DAY_OF_WEEK, Integer.valueOf(cycleDate) + 1);//星期 |
|
|
|
} |
|
|
|
nextDate = DateUtils.parseDateToStr(YYYY_MM_DD, cal.getTime()); |
|
|
|
} else if ("1".equals(cycleType)) { |
|
|
|
//每月 |
|
|
|
//如果指定每月几号大于等于今天所属几号,则取本月的几号 |
|
|
|
//如果指定每月几号小于今天所属几号,则取下个月的几号 |
|
|
|
if (cycleDate.length() == 1) { |
|
|
|
cycleDate = "0" + cycleDate; |
|
|
|
} |
|
|
|
if (Integer.valueOf(cycleDate) >= Integer.valueOf(DateUtils.parseDateToStr("dd", date))) { |
|
|
|
while (true) { |
|
|
|
Calendar cal = Calendar.getInstance(); |
|
|
|
cal.setTime(date); |
|
|
|
//如果当月最大天数小于指定的每月几号,则取下一个月的几号,直到找到对应月数有对应的几号为止 |
|
|
|
if (cal.getActualMaximum(Calendar.DATE) < Integer.valueOf(cycleDate)) { |
|
|
|
date = DateUtils.getLastMonthDate(DateUtils.parseDateToStr(YYYY_MM_DD_HH_MM_SS, date), 1); |
|
|
|
} else { |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
nextDate = DateUtils.parseDateToStr("yyyy-MM", date) + "-" + cycleDate; |
|
|
|
} else { |
|
|
|
Date lastMonthDate = DateUtils.getLastMonthDate(DateUtils.parseDateToStr(YYYY_MM_DD_HH_MM_SS, date), 1); |
|
|
|
nextDate = DateUtils.parseDateToStr("yyyy-MM", lastMonthDate) + "-" + cycleDate; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return nextDate + " " + beginDateTime; |
|
|
|
} |
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
String beginDate = "2023-05-04 12:12:12"; |
|
|
|
try { |
|
|
|
System.out.println("最近下一个周:" + getNextCycleDay(beginDate,"0","7")); |
|
|
|
System.out.println("最近下一个月:" + getNextCycleDay(beginDate,"1","31")); |
|
|
|
} catch (ParseException e) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
} |
|
|
|
} |