From 650f5b86d2af51a0a3c7334703bdc2916a82a749 Mon Sep 17 00:00:00 2001 From: lius <Lius2225@163.com> Date: 星期三, 12 七月 2023 14:59:58 +0800 Subject: [PATCH] 假期管理自动生成双休日任务 --- lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/util/DateUtils.java | 190 +++++++++++++++++++++++++++-------------------- 1 files changed, 109 insertions(+), 81 deletions(-) diff --git a/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/util/DateUtils.java b/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/util/DateUtils.java index 18aca87..5aa4da8 100644 --- a/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/util/DateUtils.java +++ b/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/util/DateUtils.java @@ -5,9 +5,15 @@ import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.time.DayOfWeek; import java.time.LocalDate; +import java.time.ZoneId; +import java.time.temporal.ChronoField; +import java.time.temporal.ChronoUnit; import java.util.*; import java.util.regex.Pattern; +import java.util.stream.Collectors; +import java.util.stream.Stream; public class DateUtils { @@ -16,18 +22,19 @@ /** * @return 寰楀埌鏄庡ぉ */ - public static Date getNextDay(Date d1){ - long d2 = d1.getTime() + 86400*1000; + public static Date getNextDay(Date d1) { + long d2 = d1.getTime() + 86400 * 1000; return new Date(d2); } /** * @return 寰楀埌鏄ㄥぉ */ - public static Date getPreviousDay(Date d1){ - long d2 = d1.getTime() - 86400*1000; + public static Date getPreviousDay(Date d1) { + long d2 = d1.getTime() - 86400 * 1000; return new Date(d2); } + /** * @return 杩斿洖鏃堕棿宸殑璇█鎻忚堪 濡�1澶�2灏忔椂5鍒�6绉� */ @@ -130,11 +137,11 @@ public static String dateProportion(Date start, Date end) { float differentSecond = DateUtils.differentSecond(start, end); - float f = differentSecond / DAYTIMESUNSET *100; - return String.format("%.2f", f)+"%"; + float f = differentSecond / DAYTIMESUNSET * 100; + return String.format("%.2f", f) + "%"; } - public static Date strToDate(String dateStr,String format) { + public static Date strToDate(String dateStr, String format) { SimpleDateFormat sf = new SimpleDateFormat(format); Date result = null; try { @@ -155,6 +162,7 @@ public static final String STR_DATE_TIME_FULL = "yyyyMMddHHmmssSSS"; public static final String STR_HHMMSS = "HH:mm:ss"; public static final String STR_HHMM = "HH:mm"; + /** * <p> * Description: 瀹為檯鎶曟爣鏈堜唤 @@ -207,8 +215,7 @@ * Description: 淇敼鏃堕棿涓烘寚瀹氭椂闂村綋澶╃殑23:59:59.000 * </p> * - * @param date - * 闇�瑕佷慨鏀圭殑鏃堕棿 + * @param date 闇�瑕佷慨鏀圭殑鏃堕棿 * @return 淇敼鍚庣殑鏃堕棿 */ public static Date fillTime(Date date) { @@ -218,10 +225,8 @@ } /** - * @param date - * 褰撳墠鏃堕棿 - * @param i - * 寰�鍓嶅嚑澶� + * @param date 褰撳墠鏃堕棿 + * @param i 寰�鍓嶅嚑澶� * @return */ public static Date fillBeforeTime(Date date, Integer i) { @@ -239,8 +244,7 @@ * Description: 淇敼鏃堕棿涓烘寚瀹氭椂闂村墠涓�澶╃殑00:00:00 * </p> * - * @param date - * 闇�瑕佷慨鏀圭殑鏃堕棿 + * @param date 闇�瑕佷慨鏀圭殑鏃堕棿 * @return 淇敼鍚庣殑鏃堕棿 */ public static Date plusTime(Date date, Integer i) { @@ -257,8 +261,7 @@ * </p> * 濡�: 2013-11-11 18:56:33 ---> 2013-11-11 00:00:00 * - * @param date - * 闇�瑕佷慨鏀圭殑鏃堕棿 + * @param date 闇�瑕佷慨鏀圭殑鏃堕棿 * @return 淇敼鍚庣殑鏃堕棿 */ public static Date removeTime(Date date) { @@ -278,8 +281,7 @@ * Description: 鎸夐粯璁ゆ牸寮�(yyyy-MM-dd HH:mm:ss.SSS)鑾峰彇鏃堕棿瀛楃涓� * </p> * - * @param date - * 瑕佽浆鎹㈢殑鏃ユ湡 + * @param date 瑕佽浆鎹㈢殑鏃ユ湡 * @return 杞崲鍚庣殑鏃堕棿瀛楃涓� */ public static String format(Date date) { @@ -287,7 +289,7 @@ return df.format(date); } - public static Date parseDate(Date date,String format){ + public static Date parseDate(Date date, String format) { SimpleDateFormat df = new SimpleDateFormat(format); try { date = df.parse(df.format(date)); @@ -297,21 +299,20 @@ return date; } - public static Date getDelayedYear(){ + public static Date getDelayedYear() { Calendar curr = Calendar.getInstance(); - curr.set(Calendar.YEAR,curr.get(Calendar.YEAR)+1); - Date date=curr.getTime(); + curr.set(Calendar.YEAR, curr.get(Calendar.YEAR) + 1); + Date date = curr.getTime(); return date; } + /** * <p> * Description: 鎸夋寚瀹氭牸寮忚幏鍙栨椂闂村瓧绗︿覆 * </p> * - * @param date - * 瑕佽浆鎹㈢殑鏃ユ湡 - * @param format - * 鏍煎紡,渚嬪:yyyy-MM-dd HH:mm:ss.SSS + * @param date 瑕佽浆鎹㈢殑鏃ユ湡 + * @param format 鏍煎紡,渚嬪:yyyy-MM-dd HH:mm:ss.SSS * @return 杞崲鍚庣殑鏃堕棿瀛楃涓� */ public static String format(Date date, String format) { @@ -324,8 +325,7 @@ * Description: 鍔犳湀鍑芥暟 * </p> * - * @param month - * 鏈堜唤鏁� + * @param month 鏈堜唤鏁� * @return */ public static Date addMonth(Integer month, Date time) { @@ -382,7 +382,7 @@ // 璁$畻绉掓暟 Long days = (t2 - t1) / (1000); return days.intValue(); - }catch (Exception e) { + } catch (Exception e) { return 0; } @@ -400,8 +400,9 @@ double hours = (t2 - t1) / (1000 * 60 * 60); return Double.valueOf(new DecimalFormat("#.00").format(hours)); } + //鏍规嵁褰撳墠鏃堕棿鍜屽嚭鐢熸棩鏈熻幏鍙栧勾榫� - public static int getAge(Date birthDay){ + public static int getAge(Date birthDay) { Calendar cal = Calendar.getInstance(); // 鍙栧嚭绯荤粺褰撳墠鏃堕棿鐨勫勾銆佹湀銆佹棩閮ㄥ垎 int yearNow = cal.get(Calendar.YEAR); @@ -415,13 +416,13 @@ // 褰撳墠骞翠唤涓庡嚭鐢熷勾浠界浉鍑忥紝鍒濇璁$畻骞撮緞 int age = yearNow - yearBirth; // 褰撳墠鏈堜唤涓庡嚭鐢熸棩鏈熺殑鏈堜唤鐩告瘮锛屽鏋滄湀浠藉皬浜庡嚭鐢熸湀浠斤紝鍒欏勾榫勪笂鍑�1锛岃〃绀轰笉婊″灏戝懆宀� - if (monthNow <= monthBirth){ + if (monthNow <= monthBirth) { // 濡傛灉鏈堜唤鐩哥瓑锛屽湪姣旇緝鏃ユ湡锛屽鏋滃綋鍓嶆棩锛屽皬浜庡嚭鐢熸棩锛屼篃鍑�1锛岃〃绀轰笉婊″灏戝懆宀� - if (monthNow == monthBirth){ + if (monthNow == monthBirth) { if (dayOfMonthNow < dayOfMonthBirth) { age--; } - }else { + } else { age--; } @@ -439,7 +440,7 @@ public static Date addDays(Date startTime, Integer day) { Calendar c = Calendar.getInstance(); c.setTime(startTime); - c.add(Calendar.DATE, day ); + c.add(Calendar.DATE, day); return c.getTime(); } /*public static Date addHours(Date startTime, Integer hours) { @@ -449,7 +450,7 @@ return c.getTime(); }*/ - public static Date toDate(String date, String format){ + public static Date toDate(String date, String format) { SimpleDateFormat df = new SimpleDateFormat(format); try { return df.parse(date); @@ -459,7 +460,7 @@ return null; } - public static Date toDateFull(String date){ + public static Date toDateFull(String date) { SimpleDateFormat df = new SimpleDateFormat(STR_DATE_TIME); try { return df.parse(date); @@ -468,7 +469,8 @@ } return null; } - public static Date toDateMedium(String date){ + + public static Date toDateMedium(String date) { SimpleDateFormat df = new SimpleDateFormat(STR_DATE_TIME_SMALL); try { Date dd = df.parse(date); @@ -477,6 +479,7 @@ return null; } } + public static Integer getDate(Date date) { if (date != null) { Calendar calendar = Calendar.getInstance(); @@ -486,9 +489,11 @@ } return null; } + public static Integer getYear() { return getYear(new Date()); } + public static Integer getYear(Date date) { if (date != null) { Calendar calendar = Calendar.getInstance(); @@ -498,21 +503,25 @@ } return null; } + public static Integer getMonth() { return getMonth(new Date()); } + public static Integer getMonth(Date date) { if (date != null) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int month = calendar.get(Calendar.MONTH); - return month+1; + return month + 1; } return null; } + public static Integer getDay() { return getDay(new Date()); } + public static Integer getDay(Date date) { if (date != null) { Calendar calendar = Calendar.getInstance(); @@ -524,12 +533,12 @@ } - public static Date[] getMonthStartTimeAndEndTime(Integer month){ + public static Date[] getMonthStartTimeAndEndTime(Integer month) { Calendar calendar = Calendar.getInstance(); Date[] dates = new Date[2]; Date startTime = null; Date endsTime = null; - if(month != null){ + if (month != null) { calendar.set(Calendar.MONTH, month); //鑾峰緱鍒版湰鏈堢殑绗竴澶� calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH)); @@ -544,14 +553,15 @@ dates[1] = endsTime; return dates; } - public static Date[] getMonthStartTimeAndEndTime(Date date){ + + public static Date[] getMonthStartTimeAndEndTime(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); Integer month = calendar.get(Calendar.MONTH); Date[] dates = new Date[2]; Date startTime = null; Date endsTime = null; - if(month != null){ + if (month != null) { calendar.set(Calendar.MONTH, month); //鑾峰緱鍒版湰鏈堢殑绗竴澶� calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH)); @@ -570,6 +580,7 @@ /** * 鑾峰彇days澶╃殑鏃堕棿鍖洪棿锛宔ndTime 涓哄綋澶╃殑鍚庝竴澶�0鐐癸紝 * startTime 涓篹ndTime鍓峝ays澶� + * * @param days * @return */ @@ -585,6 +596,7 @@ /** * 鏍规嵁鏌愪竴骞磋幏鍙栨渶鍚庝竴澶╃殑鏃堕棿 * 2013 ---> 2013-12-31 23:59:59.000 + * * @param year * @return */ @@ -600,15 +612,16 @@ /** * 鑾峰彇start/end鐨勬墍鏈夋棩鏈熷瓧绗︿覆 鏍煎紡yyyy-MM-dd + * * @param start * @param end * @return */ - public static List<String> getDatesStringList(Date start, Date end,String strDate) { + public static List<String> getDatesStringList(Date start, Date end, String strDate) { List<String> list = new ArrayList<>(); int i = getDays(start, end); - for(int j = 0; j <= i; j++) { - if(j == 0) { + for (int j = 0; j <= i; j++) { + if (j == 0) { list.add(format(start, strDate)); } else { list.add(format(plusTime(start, j), strDate)); @@ -619,6 +632,7 @@ /** * 鑾峰彇start/end鐨勬墍鏈夋棩鏈熷瓧绗︿覆 鏍煎紡yyyy-MM-dd + * * @param start * @param end * @return @@ -626,8 +640,8 @@ public static List<String> getDatesStringList(Date start, Date end) { List<String> list = new ArrayList<>(); int i = getDays(start, end); - for(int j = 0; j <= i; j++) { - if(j == 0) { + for (int j = 0; j <= i; j++) { + if (j == 0) { list.add(format(start, STR_DATE)); } else { list.add(format(plusTime(start, j), STR_DATE)); @@ -638,6 +652,7 @@ /** * 鑾峰彇start/end鐨勬墍鏈夋棩鏈熷瓧绗︿覆 鏍煎紡yyyyMMdd + * * @param start * @param end * @return @@ -645,8 +660,8 @@ public static List<String> getDatesStringList2(Date start, Date end) { List<String> list = new ArrayList<>(); int i = getDays(start, end); - for(int j = 0; j <= i; j++) { - if(j == 0) { + for (int j = 0; j <= i; j++) { + if (j == 0) { list.add(format(start, STRDATE)); } else { list.add(format(plusTime(start, j), STRDATE)); @@ -655,7 +670,7 @@ return list; } - public static List<String> getMonthBetween(Date start, Date end){ + public static List<String> getMonthBetween(Date start, Date end) { List<String> list = new ArrayList<>(); Calendar min = Calendar.getInstance(); Calendar max = Calendar.getInstance(); @@ -664,15 +679,16 @@ max.setTime(end); max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2); Calendar curr = min; - while (curr.before(max)){ - list.add(format(curr.getTime(),STR_YEAR_MONTH)); - curr.add(Calendar.MONTH,1); + while (curr.before(max)) { + list.add(format(curr.getTime(), STR_YEAR_MONTH)); + curr.add(Calendar.MONTH, 1); } return list; } /** * 鑾峰彇dateStr鐨勬棩鏈熸牸寮弝yyy-MM-dd + * * @param dateStr * @return */ @@ -688,6 +704,7 @@ /** * 鑾峰彇dateStr鐨勬棩鏈熸牸寮弝yyyMMdd + * * @param dateStr * @return */ @@ -701,7 +718,7 @@ return startTime == null ? removeTime(new Date()) : startTime; } - public static Date getFormatDate(String dateStr,String format) { + public static Date getFormatDate(String dateStr, String format) { SimpleDateFormat sdf = new SimpleDateFormat(format); Date startTime = null; try { @@ -712,12 +729,11 @@ } /** - * * @param time * @param n - * @return 鍦ㄤ竴涓椂闂翠笂鍔犵 + * @return 鍦ㄤ竴涓椂闂翠笂鍔犵 */ - public static Date addSecond(Date time,Integer n) { + public static Date addSecond(Date time, Integer n) { Calendar c = Calendar.getInstance(); c.setTime(time); c.add(Calendar.SECOND, n); @@ -725,12 +741,11 @@ } /** - * * @param time * @param n - * @return 鍦ㄤ竴涓椂闂翠笂鍔犲垎閽� + * @return 鍦ㄤ竴涓椂闂翠笂鍔犲垎閽� */ - public static Date addMinute(Date time,Integer n) { + public static Date addMinute(Date time, Integer n) { Calendar c = Calendar.getInstance(); c.setTime(time); c.add(Calendar.MINUTE, n); @@ -738,12 +753,11 @@ } /** - * * @param time * @param n - * @return 鍦ㄤ竴涓椂闂翠笂鍔犲皬鏃� + * @return 鍦ㄤ竴涓椂闂翠笂鍔犲皬鏃� */ - public static Date addHour(Date time,Integer n) { + public static Date addHour(Date time, Integer n) { Calendar c = Calendar.getInstance(); c.setTime(time); c.add(Calendar.HOUR_OF_DAY, n); @@ -752,6 +766,7 @@ /** * 鑾峰彇start/end鐨勬墍鏈夋棩鏈熷瓧绗︿覆 鏍煎紡yyyy-MM-dd + * * @param start * @param end * @return @@ -776,8 +791,10 @@ } return list; } + /** * 鑾峰彇start/end鐨勬墍鏈夋棩鏈熷瓧绗︿覆 鏍煎紡yyyy + * * @param start * @param end * @return @@ -802,10 +819,12 @@ } return list; } + /** * 鑾峰彇褰撳墠鏃堕棿 瀹氫綅鍒板皬鏃� * 鏍煎紡涓� yyyy-MM-dd hh:mm * 渚嬶細2018-02-27 11:00 + * * @return */ public static Date getNowHourDate() { @@ -822,6 +841,7 @@ /** * 鑾峰彇姣忔棩8:11鎴�11:11鐨勬椂闂寸偣 + * * @return */ public static Date getNowHourDateTo() { @@ -830,11 +850,11 @@ Calendar calendar = Calendar.getInstance(); calendar.setTime(now); int hour = calendar.get(Calendar.HOUR_OF_DAY); - if(hour != 8 && hour != 11) { + if (hour != 8 && hour != 11) { return null; } int minute = calendar.get(Calendar.MINUTE); - if(minute < 11) { + if (minute < 11) { return null; } String dateStr = format(now, STR_DATE); @@ -845,18 +865,20 @@ /** * 鑾峰彇姣忔棩8:00銆�13:00銆�20:00鐨勬椂闂寸偣 + * * @return */ public static List<Date> getHourDateList(Date time) { List<Date> result = new ArrayList<>(); - result.add(addHour(time,8)); - result.add(addHour(time,13)); - result.add(addHour(time,20)); + result.add(addHour(time, 8)); + result.add(addHour(time, 13)); + result.add(addHour(time, 20)); return result; } /** * 鑾峰彇涓婃湀鐨勬渶鍚庝竴澶� + * * @return */ public static Date getPreviousMonthLastDay() { @@ -867,6 +889,7 @@ /** * 鑾峰彇end鏃堕棿鏈堢殑绗竴澶� + * * @param end 鏈堜唤鏈�鍚庝竴澶� * @return */ @@ -882,13 +905,12 @@ int hour = 0; if (time <= 0) { return "0灏忔椂"; - } - else { + } else { BigDecimal bigDecimal = new BigDecimal(time); BigDecimal bigDecimal1 = new BigDecimal(3600); BigDecimal hourBigDecimal = new BigDecimal(0); hourBigDecimal = new BigDecimal(String.valueOf(bigDecimal)). - divide(new BigDecimal(String.valueOf(bigDecimal1)),2, BigDecimal.ROUND_HALF_UP); + divide(new BigDecimal(String.valueOf(bigDecimal1)), 2, BigDecimal.ROUND_HALF_UP); timeStr = hourBigDecimal + "灏忔椂"; } return timeStr; @@ -924,6 +946,7 @@ retStr = "" + i; return retStr; } + /** * 楠岃瘉鏃堕棿 * @@ -953,34 +976,39 @@ Calendar cal = Calendar.getInstance(); cal.setTime(theDate); String[] times = planTime.split(":"); - cal.set(Calendar.HOUR_OF_DAY,Integer.parseInt(times[0])); - cal.set(Calendar.MINUTE,Integer.parseInt(times[1])); - cal.set(Calendar.SECOND,Integer.parseInt(times[2])); + cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(times[0])); + cal.set(Calendar.MINUTE, Integer.parseInt(times[1])); + cal.set(Calendar.SECOND, Integer.parseInt(times[2])); return cal.getTime(); } public static long getTimeWithOutDay(Date d) { - return d.getTime()/1000%86400; + return d.getTime() / 1000 % 86400; } + /** - * @Description: long绫诲瀷杞崲鎴愭棩鏈� - * * @param lo 姣鏁� * @return String yyyy-MM-dd HH:mm:ss + * @Description: long绫诲瀷杞崲鎴愭棩鏈� */ - public static String longToDate(long lo){ + public static String longToDate(long lo) { Date date = new Date(lo); SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sd.format(date); } /** - * * @param startDate * @param endDate * @return */ public static List<Date> getWeekDays(LocalDate startDate, LocalDate endDate) { - return null; + List<Date> result = new ArrayList<>(); + List<LocalDate> dateList = Stream.iterate(startDate, localDate -> localDate.plusDays(1)) + .limit(ChronoUnit.DAYS.between(startDate, endDate) + 1) + .filter(localDate -> DayOfWeek.SATURDAY.equals(DayOfWeek.of(localDate.get(ChronoField.DAY_OF_WEEK))) || DayOfWeek.SUNDAY.equals(DayOfWeek.of(localDate.get(ChronoField.DAY_OF_WEEK)))) + .collect(Collectors.toList()); + dateList.forEach(localDate -> result.add(Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()))); + return result; } } -- Gitblit v1.9.3