From f84d9e69907cb678150eaa6393fd74cf042fcca4 Mon Sep 17 00:00:00 2001 From: zenglf <18502938215@163.com> Date: 星期四, 28 九月 2023 14:39:03 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/master' into develop --- lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/util/DateUtils.java | 262 +++++++++++++++++++++++++++++++++++---------------- 1 files changed, 179 insertions(+), 83 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..75c7f8e 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 @@ -1,13 +1,21 @@ package org.jeecg.modules.mdc.util; +import io.swagger.models.auth.In; + import java.math.BigDecimal; 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 +24,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绉� */ @@ -66,10 +75,11 @@ } /** - * @return 鑾峰彇涓や釜date鐨勬椂闂村樊锛岀粨鏋滀负绉� + * @return 鑾峰彇涓や釜date鐨勬椂闂村樊锛岀粨鏋滀负绉� 闄� */ public static long differentSecond(Date startDate, Date endDate) { - return (endDate.getTime() - startDate.getTime()) / 1000; + return new BigDecimal(endDate.getTime() - startDate.getTime()).divide(new BigDecimal("1000"), 0, BigDecimal.ROUND_HALF_UP).longValue(); +// return (endDate.getTime() - startDate.getTime()) / 1000; } /** @@ -130,11 +140,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 +165,8 @@ 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"; + public static final String STR_MMDD = "MM-dd"; + /** * <p> * Description: 瀹為檯鎶曟爣鏈堜唤 @@ -207,8 +219,7 @@ * Description: 淇敼鏃堕棿涓烘寚瀹氭椂闂村綋澶╃殑23:59:59.000 * </p> * - * @param date - * 闇�瑕佷慨鏀圭殑鏃堕棿 + * @param date 闇�瑕佷慨鏀圭殑鏃堕棿 * @return 淇敼鍚庣殑鏃堕棿 */ public static Date fillTime(Date date) { @@ -218,10 +229,8 @@ } /** - * @param date - * 褰撳墠鏃堕棿 - * @param i - * 寰�鍓嶅嚑澶� + * @param date 褰撳墠鏃堕棿 + * @param i 寰�鍓嶅嚑澶� * @return */ public static Date fillBeforeTime(Date date, Integer i) { @@ -239,8 +248,7 @@ * Description: 淇敼鏃堕棿涓烘寚瀹氭椂闂村墠涓�澶╃殑00:00:00 * </p> * - * @param date - * 闇�瑕佷慨鏀圭殑鏃堕棿 + * @param date 闇�瑕佷慨鏀圭殑鏃堕棿 * @return 淇敼鍚庣殑鏃堕棿 */ public static Date plusTime(Date date, Integer i) { @@ -257,8 +265,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 +285,7 @@ * Description: 鎸夐粯璁ゆ牸寮�(yyyy-MM-dd HH:mm:ss.SSS)鑾峰彇鏃堕棿瀛楃涓� * </p> * - * @param date - * 瑕佽浆鎹㈢殑鏃ユ湡 + * @param date 瑕佽浆鎹㈢殑鏃ユ湡 * @return 杞崲鍚庣殑鏃堕棿瀛楃涓� */ public static String format(Date date) { @@ -287,7 +293,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 +303,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 +329,7 @@ * Description: 鍔犳湀鍑芥暟 * </p> * - * @param month - * 鏈堜唤鏁� + * @param month 鏈堜唤鏁� * @return */ public static Date addMonth(Integer month, Date time) { @@ -382,7 +386,7 @@ // 璁$畻绉掓暟 Long days = (t2 - t1) / (1000); return days.intValue(); - }catch (Exception e) { + } catch (Exception e) { return 0; } @@ -400,8 +404,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 +420,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 +444,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 +454,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 +464,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 +473,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 +483,7 @@ return null; } } + public static Integer getDate(Date date) { if (date != null) { Calendar calendar = Calendar.getInstance(); @@ -486,9 +493,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 +507,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 +537,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 +557,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 +584,7 @@ /** * 鑾峰彇days澶╃殑鏃堕棿鍖洪棿锛宔ndTime 涓哄綋澶╃殑鍚庝竴澶�0鐐癸紝 * startTime 涓篹ndTime鍓峝ays澶� + * * @param days * @return */ @@ -585,6 +600,7 @@ /** * 鏍规嵁鏌愪竴骞磋幏鍙栨渶鍚庝竴澶╃殑鏃堕棿 * 2013 ---> 2013-12-31 23:59:59.000 + * * @param year * @return */ @@ -600,15 +616,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 +636,7 @@ /** * 鑾峰彇start/end鐨勬墍鏈夋棩鏈熷瓧绗︿覆 鏍煎紡yyyy-MM-dd + * * @param start * @param end * @return @@ -626,8 +644,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 +656,7 @@ /** * 鑾峰彇start/end鐨勬墍鏈夋棩鏈熷瓧绗︿覆 鏍煎紡yyyyMMdd + * * @param start * @param end * @return @@ -645,8 +664,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 +674,27 @@ return list; } - public static List<String> getMonthBetween(Date start, Date end){ + /** + * 鑾峰彇start/end鐨勬墍鏈夋棩鏈熷瓧绗︿覆 鏍煎紡MM-dd + * + * @param start + * @param end + * @return + */ + public static List<String> getDatesStringLists(Date start, Date end) { + List<String> list = new ArrayList<>(); + int i = getDays(start, end); + for (int j = 0; j <= i; j++) { + if (j == 0) { + list.add(format(start, STR_MMDD)); + } else { + list.add(format(plusTime(start, j), STR_MMDD)); + } + } + return list; + } + + public static List<String> getMonthBetween(Date start, Date end) { List<String> list = new ArrayList<>(); Calendar min = Calendar.getInstance(); Calendar max = Calendar.getInstance(); @@ -664,15 +703,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 +728,7 @@ /** * 鑾峰彇dateStr鐨勬棩鏈熸牸寮弝yyyMMdd + * * @param dateStr * @return */ @@ -701,7 +742,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 +753,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 +765,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 +777,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 +790,7 @@ /** * 鑾峰彇start/end鐨勬墍鏈夋棩鏈熷瓧绗︿覆 鏍煎紡yyyy-MM-dd + * * @param start * @param end * @return @@ -776,8 +815,10 @@ } return list; } + /** * 鑾峰彇start/end鐨勬墍鏈夋棩鏈熷瓧绗︿覆 鏍煎紡yyyy + * * @param start * @param end * @return @@ -802,10 +843,12 @@ } return list; } + /** * 鑾峰彇褰撳墠鏃堕棿 瀹氫綅鍒板皬鏃� * 鏍煎紡涓� yyyy-MM-dd hh:mm * 渚嬶細2018-02-27 11:00 + * * @return */ public static Date getNowHourDate() { @@ -822,6 +865,7 @@ /** * 鑾峰彇姣忔棩8:11鎴�11:11鐨勬椂闂寸偣 + * * @return */ public static Date getNowHourDateTo() { @@ -830,11 +874,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 +889,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 +913,7 @@ /** * 鑾峰彇end鏃堕棿鏈堢殑绗竴澶� + * * @param end 鏈堜唤鏈�鍚庝竴澶� * @return */ @@ -882,13 +929,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 +970,7 @@ retStr = "" + i; return retStr; } + /** * 楠岃瘉鏃堕棿 * @@ -953,34 +1000,83 @@ 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; + } + + /** + * @param smallDate + * @param bigDate + * @desc 鑾峰彇涓や釜鏃ユ湡涔嬮棿鐨勫ぉ鏁� + */ + public static Integer getDaysBetween(String smallDate, String bigDate) throws ParseException { + // 鏃ユ湡鏍煎紡 + SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); + // 鑾峰彇涓や釜鏃ユ湡鐨勬椂闂存埑 + Calendar cal = Calendar.getInstance(); + cal.setTime(sdf.parse(smallDate)); + long smallTime = cal.getTimeInMillis(); + cal.setTime(sdf.parse(bigDate)); + long bigTime = cal.getTimeInMillis(); + // 鐩稿樊鐨勬棩鏈� + long days = (bigTime - smallTime) / (1000 * 3600 * 24); + // long杞琲nt 瀛樺湪婧㈠嚭鎯呭喌 鏍规嵁涓氬姟鎯呭喌缂栬緫catch涓繑鍥炲�� + try { + return Integer.parseInt(String.valueOf(days)); + } catch (NumberFormatException e) { + e.printStackTrace(); + return 0; + } + } + + /** + * 鏃ユ湡杞崲锛屽皢鎺ュ彛杩斿洖鐨�20180524杞负2018-05-24 + * + * @param str + * @return + */ + public static String dateConvertion(String str) { + Date parse = null; + String dateString = ""; + try { + parse = new SimpleDateFormat("yyyyMMdd").parse(str); + dateString = new SimpleDateFormat("yyyy-MM-dd").format(parse); + } catch (ParseException e) { + dateString = null; + } + + return dateString; } } -- Gitblit v1.9.3