From 23855599412c4d61b38d78f0f3abd3430a48b5b1 Mon Sep 17 00:00:00 2001 From: zhangherong <571457620@qq.com> Date: 星期三, 25 六月 2025 11:51:38 +0800 Subject: [PATCH] Merge branch 'mdc_hyjs_master' --- lxzn-module-mdc-common/src/main/java/org/jeecg/modules/mdc/util/DateUtils.java | 1115 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 1,115 insertions(+), 0 deletions(-) diff --git a/lxzn-module-mdc-common/src/main/java/org/jeecg/modules/mdc/util/DateUtils.java b/lxzn-module-mdc-common/src/main/java/org/jeecg/modules/mdc/util/DateUtils.java new file mode 100644 index 0000000..34592cb --- /dev/null +++ b/lxzn-module-mdc-common/src/main/java/org/jeecg/modules/mdc/util/DateUtils.java @@ -0,0 +1,1115 @@ +package org.jeecg.modules.mdc.util; + + +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.text.DecimalFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.*; +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 { + + private final static long DAYTIMESUNSET = 86400; + + /** + * @return 寰楀埌鏄庡ぉ + */ + 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; + return new Date(d2); + } + + /** + * @return 杩斿洖鏃堕棿宸殑璇█鎻忚堪 濡�1澶�2灏忔椂5鍒�6绉� + */ + public static String different(Date d1, Date d2) { + if (d1 == null || d2 == null) { + return ""; + } + StringBuilder strB = new StringBuilder(); + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + try { + //姣ms + long diff = d2.getTime() - d1.getTime(); + long diffSeconds = diff / 1000 % 60; + long diffMinutes = diff / (60 * 1000) % 60; + long diffHours = diff / (60 * 60 * 1000) % 24; + long diffDays = diff / (24 * 60 * 60 * 1000); + if (diffDays > 0) { + strB.append(diffDays + " 澶� "); + strB.append(diffHours + " 灏忔椂 "); + strB.append(diffMinutes + " 鍒嗛挓 "); + strB.append(diffSeconds + " 绉� "); + } else if (diffHours > 0) { + strB.append(diffHours + " 灏忔椂 "); + strB.append(diffMinutes + " 鍒嗛挓 "); + strB.append(diffSeconds + " 绉� "); + } else if (diffMinutes > 0) { + strB.append(diffMinutes + " 鍒嗛挓 "); + strB.append(diffSeconds + " 绉� "); + } else { + strB.append(diffSeconds + " 绉� "); + } + } catch (Exception e) { + e.printStackTrace(); + } + return strB.toString(); + } + + /** + * @return 鑾峰彇涓や釜date鐨勬椂闂村樊锛岀粨鏋滀负绉� 闄� + */ + public static long differentSecond(Date startDate, Date endDate) { + return new BigDecimal(endDate.getTime() - startDate.getTime()).divide(new BigDecimal("1000"), 0, BigDecimal.ROUND_HALF_UP).longValue(); +// return (endDate.getTime() - startDate.getTime()) / 1000; + } + + /** + * @return 鑾峰彇涓や釜date鐨勬椂闂村樊锛岀粨鏋滀负鍒嗛挓 + */ + public static Integer differentMinutes(Date startDate, Date endDate) { + return new BigDecimal(endDate.getTime() - startDate.getTime()).divide(new BigDecimal("60000"), 0, RoundingMode.HALF_UP).intValue(); + } + + /** + * @return 杩斿洖浼犲叆鏃堕棿鐨�0鐐�0鍒�0绉� + */ + public static Date getTodayZero(Date d) { + Calendar cal = Calendar.getInstance(); + cal.setTime(d); + cal.set(Calendar.HOUR_OF_DAY, 0); + cal.set(Calendar.MINUTE, 0); + cal.set(Calendar.SECOND, 0); + return cal.getTime(); + } + + /** + * @return 杩斿洖浼犲叆鏃堕棿鐨�0鐐�0鍒�0绉� + */ + public static Date getTomorrowZero(Date d) { + Calendar cal = Calendar.getInstance(); + cal.setTime(d); + cal.set(Calendar.HOUR_OF_DAY, 0); + cal.set(Calendar.MINUTE, 0); + cal.set(Calendar.SECOND, 0); + cal.add(Calendar.DAY_OF_MONTH, 1); + return cal.getTime(); + } + + /** + * @return 鍒ゆ柇鏃堕棿鏄惁鏄綋澶� + */ + public static boolean isTaday(Date date) { + Date now = new Date(); + SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd"); + String nowDay = sf.format(now); + String day = sf.format(date); + return day.equals(nowDay); + } + + /** + * @return 鍒ゆ柇涓や釜鏃堕棿鏄惁涓哄悓涓�澶� + */ + public static boolean isOneDay(Date d1, Date d2) { + SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd"); + String d1Str = sf.format(d1); + String d2Str = sf.format(d2); + return d1Str.equals(d2Str); + } + + /** + * @return 鍒ゆ柇涓や釜鏃堕棿鏄惁涓哄悓涓�骞� + */ + public static boolean isOneYear(Date d1, Date d2) { + SimpleDateFormat sf = new SimpleDateFormat("yyyy"); + String d1Str = sf.format(d1); + String d2Str = sf.format(d2); + return d1Str.equals(d2Str); + } + + public static String dateProportion(Date start, Date end) { + float differentSecond = DateUtils.differentSecond(start, end); + float f = differentSecond / DAYTIMESUNSET * 100; + return String.format("%.2f", f) + "%"; + } + + public static Date strToDate(String dateStr, String format) { + SimpleDateFormat sf = new SimpleDateFormat(format); + Date result = null; + try { + result = sf.parse(dateStr); + } catch (ParseException e) { + e.printStackTrace(); + } + return result; + } + + public static final String STR_DATE = "yyyy-MM-dd"; + public static final String STRDATE = "yyyyMMdd"; + public static final String STR_YEAR_MONTH = "yyyy-MM"; + public static final String STRYEARMONTH = "yyyyMM"; + public static final String STR_DATE_TIME = "yyyy-MM-dd HH:mm:ss.SSS"; + public static final String STR_DATE_TIME_SMALL = "yyyy-MM-dd HH:mm:ss"; + public static final String STR_DATE_TIME_MIN = "yyyy-MM-dd HH:mm"; + public static final String STR_DATE_TIME_HOUR = "yyyy-MM-dd HH"; + 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: 瀹為檯鎶曟爣鏈堜唤 + * </p> + * + * @param startDate + * @param endDate + * @return obj [0] 瀹為檯鎶曟爣鏈堜唤 渚嬪瀹為檯鎶曟爣鏈堜唤锛�3.86锛夋剰涔夋槸3涓湀鍙�26澶� obj[1] 瀹為檯鍙栨暣鏈堜唤 锛�3锛� + */ + public static Integer getRealMonth(Date startDate, Date endDate) { + Integer month = 0; + Calendar start = Calendar.getInstance(); + start.setTime(startDate); + + Calendar end = Calendar.getInstance(); + end.setTime(endDate); + int year = start.get(Calendar.YEAR); + int year2 = end.get(Calendar.YEAR); + int month2 = start.get(Calendar.MONTH); + int month3 = end.get(Calendar.MONTH); + try { + while (start.before(end)) { + start.add(Calendar.MONTH, 1); + month++; + } + } catch (Exception e) { + e.printStackTrace(); + } + int day2 = start.get(Calendar.DAY_OF_MONTH); + int day3 = end.get(Calendar.DAY_OF_MONTH); + int tmpYear = year2 - year; + if (day2 == day3) { + return (tmpYear * 12) + (month3 - month2); + } + return month == 0 ? month : (month - 1); + } + + public static Date getNow() { + return new Date(System.currentTimeMillis()); + } + + public static int getDayOfMonth() { + Calendar aCalendar = Calendar.getInstance(Locale.CHINA); + int day = aCalendar.getActualMaximum(Calendar.DATE); + return day; + } + + /** + * <p> + * Description: 淇敼鏃堕棿涓烘寚瀹氭椂闂村綋澶╃殑23:59:59.000 + * </p> + * + * @param date 闇�瑕佷慨鏀圭殑鏃堕棿 + * @return 淇敼鍚庣殑鏃堕棿 + */ + public static Date fillTime(Date date) { + Date result = removeTime(date); + result.setTime(result.getTime() + 24 * 60 * 60 * 1000 - 1000); // 鍔犱竴澶� + return result; + } + + /** + * @param date 褰撳墠鏃堕棿 + * @param i 寰�鍓嶅嚑澶� + * @return + */ + public static Date fillBeforeTime(Date date, Integer i) { + Date result = removeTime(date); + Calendar calendar = Calendar.getInstance(); // 寰楀埌鏃ュ巻 + calendar.setTime(result);// 鎶婂綋鍓嶆椂闂磋祴缁欐棩鍘� + calendar.add(Calendar.DAY_OF_MONTH, i); // 璁剧疆涓哄墠涓�澶� + result.setTime(calendar.getTime().getTime() + 24 * 60 * 60 * 1000 + - 1000); // 鍔犱竴澶� + return result; + } + + /** + * <p> + * Description: 淇敼鏃堕棿涓烘寚瀹氭椂闂村墠涓�澶╃殑00:00:00 + * </p> + * + * @param date 闇�瑕佷慨鏀圭殑鏃堕棿 + * @return 淇敼鍚庣殑鏃堕棿 + */ + public static Date plusTime(Date date, Integer i) { + Date result = removeTime(date); + Calendar calendar = Calendar.getInstance(); // 寰楀埌鏃ュ巻 + calendar.setTime(result);// 鎶婂綋鍓嶆椂闂磋祴缁欐棩鍘� + calendar.add(Calendar.DAY_OF_MONTH, i); // 璁剧疆涓哄墠涓�澶� + return calendar.getTime(); + } + + /** + * <p> + * Description: 鍘绘帀鏃ユ湡鏃堕棿涓殑鏃堕棿閮ㄥ垎 + * </p> + * 濡�: 2013-11-11 18:56:33 ---> 2013-11-11 00:00:00 + * + * @param date 闇�瑕佷慨鏀圭殑鏃堕棿 + * @return 淇敼鍚庣殑鏃堕棿 + */ + public static Date removeTime(Date date) { + Date result = null; + try { + SimpleDateFormat df = new SimpleDateFormat(STR_DATE); + String dateStr = df.format(date); + result = df.parse(dateStr); + } catch (ParseException e) { + e.printStackTrace(); + } + return result; + } + + /** + * <p> + * Description: 鎸夐粯璁ゆ牸寮�(yyyy-MM-dd HH:mm:ss.SSS)鑾峰彇鏃堕棿瀛楃涓� + * </p> + * + * @param date 瑕佽浆鎹㈢殑鏃ユ湡 + * @return 杞崲鍚庣殑鏃堕棿瀛楃涓� + */ + public static String format(Date date) { + SimpleDateFormat df = new SimpleDateFormat(STR_DATE_TIME); + return df.format(date); + } + + public static Date parseDate(Date date, String format) { + SimpleDateFormat df = new SimpleDateFormat(format); + try { + date = df.parse(df.format(date)); + } catch (ParseException e) { + e.printStackTrace(); + } + return date; + } + + public static Date getDelayedYear() { + Calendar curr = Calendar.getInstance(); + 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 + * @return 杞崲鍚庣殑鏃堕棿瀛楃涓� + */ + public static String format(Date date, String format) { + SimpleDateFormat df = new SimpleDateFormat(format); + return df.format(date); + } + + /** + * <p> + * Description: 鍔犳湀鍑芥暟 + * </p> + * + * @param month 鏈堜唤鏁� + * @return + */ + public static Date addMonth(Integer month, Date time) { + Calendar c = Calendar.getInstance(); + c.setTime(time); + c.add(Calendar.MONTH, month); + return c.getTime(); + } + + public static Boolean greater(Date startTime, Date endTime) { + return startTime.getTime() > endTime.getTime(); + } + + public static Boolean less(Date startTime, Date endTime) { + return startTime.getTime() < endTime.getTime(); + } + + public static Boolean equals(Date startTime, Date endTime) { + return startTime.getTime() == endTime.getTime(); + } + + public static Integer getDiffMonth(Calendar c, Calendar c1) { + return (c.get(Calendar.YEAR) - c1.get(Calendar.YEAR)) * 12 + + (c.get(Calendar.MONTH) - c1.get(Calendar.MONTH)); + } + + /** + * 鏄惁涓哄悓涓�澶� + */ + public static boolean equalsDay(Date a, Date b) { + return removeTime(a).getTime() == removeTime(b).getTime(); + } + + public static Integer getDays(Date startTime, Date endTime) { + Calendar c = Calendar.getInstance(); + c.setTime(startTime); + Calendar c1 = Calendar.getInstance(); + c1.setTime(endTime); + long t1 = c.getTimeInMillis(); + long t2 = c1.getTimeInMillis(); + // 璁$畻澶╂暟 + Long days = (t2 - t1) / (24 * 60 * 60 * 1000); + return days.intValue(); + } + + public static Integer getSeconds(Date startTime, Date endTime) { + try { + Calendar c = Calendar.getInstance(); + c.setTime(startTime); + Calendar c1 = Calendar.getInstance(); + c1.setTime(endTime); + long t1 = c.getTimeInMillis(); + long t2 = c1.getTimeInMillis(); + // 璁$畻绉掓暟 + Long days = (t2 - t1) / (1000); + return days.intValue(); + } catch (Exception e) { + return 0; + } + + } + + //鑾峰彇灏忔椂宸� + public static double subHours(Date startTime, Date endTime) { + Calendar c = Calendar.getInstance(); + c.setTime(startTime); + Calendar c1 = Calendar.getInstance(); + c1.setTime(endTime); + double t1 = c.getTimeInMillis(); + double t2 = c1.getTimeInMillis(); + // 璁$畻澶╂暟 + double hours = (t2 - t1) / (1000 * 60 * 60); + return Double.valueOf(new DecimalFormat("#.00").format(hours)); + } + + //鏍规嵁褰撳墠鏃堕棿鍜屽嚭鐢熸棩鏈熻幏鍙栧勾榫� + public static int getAge(Date birthDay) { + Calendar cal = Calendar.getInstance(); + // 鍙栧嚭绯荤粺褰撳墠鏃堕棿鐨勫勾銆佹湀銆佹棩閮ㄥ垎 + int yearNow = cal.get(Calendar.YEAR); + int monthNow = cal.get(Calendar.MONTH); + int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH); + cal.setTime(birthDay); + // 鍙栧嚭鍑虹敓鏃ユ湡鐨勫勾銆佹湀銆佹棩閮ㄥ垎 + int yearBirth = cal.get(Calendar.YEAR); + int monthBirth = cal.get(Calendar.MONTH); + int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH); + // 褰撳墠骞翠唤涓庡嚭鐢熷勾浠界浉鍑忥紝鍒濇璁$畻骞撮緞 + int age = yearNow - yearBirth; + // 褰撳墠鏈堜唤涓庡嚭鐢熸棩鏈熺殑鏈堜唤鐩告瘮锛屽鏋滄湀浠藉皬浜庡嚭鐢熸湀浠斤紝鍒欏勾榫勪笂鍑�1锛岃〃绀轰笉婊″灏戝懆宀� + if (monthNow <= monthBirth) { + // 濡傛灉鏈堜唤鐩哥瓑锛屽湪姣旇緝鏃ユ湡锛屽鏋滃綋鍓嶆棩锛屽皬浜庡嚭鐢熸棩锛屼篃鍑�1锛岃〃绀轰笉婊″灏戝懆宀� + if (monthNow == monthBirth) { + if (dayOfMonthNow < dayOfMonthBirth) { + age--; + } + } else { + age--; + } + + } + return age; + } + + public static Date subDays(Date startTime, Integer day) { + Calendar c = Calendar.getInstance(); + c.setTime(startTime); + c.add(Calendar.DATE, day * -1); + return c.getTime(); + } + + public static Date addDays(Date startTime, Integer day) { + Calendar c = Calendar.getInstance(); + c.setTime(startTime); + c.add(Calendar.DATE, day); + return c.getTime(); + } + /*public static Date addHours(Date startTime, Integer hours) { + Calendar c = Calendar.getInstance(); + c.setTime(startTime); + c.add(Calendar.HOUR, hours ); + return c.getTime(); + }*/ + + public static Date toDate(String date, String format) { + SimpleDateFormat df = new SimpleDateFormat(format); + try { + return df.parse(date); + } catch (ParseException e) { + e.printStackTrace(); + } + return null; + } + + public static Date toDateFull(String date) { + SimpleDateFormat df = new SimpleDateFormat(STR_DATE_TIME); + try { + return df.parse(date); + } catch (ParseException e) { + e.printStackTrace(); + } + return null; + } + + public static Date toDateMedium(String date) { + SimpleDateFormat df = new SimpleDateFormat(STR_DATE_TIME_SMALL); + try { + Date dd = df.parse(date); + return dd; + } catch (ParseException e) { + return null; + } + } + + public static Integer getDate(Date date) { + if (date != null) { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(date); + int day = calendar.get(Calendar.DATE); + return day; + } + return null; + } + + public static Integer getYear() { + return getYear(new Date()); + } + + public static Integer getYear(Date date) { + if (date != null) { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(date); + int year = calendar.get(Calendar.YEAR); + return year; + } + 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 null; + } + + public static Integer getDay() { + return getDay(new Date()); + } + + public static Integer getDay(Date date) { + if (date != null) { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(date); + int day = calendar.get(Calendar.DAY_OF_MONTH); + return day; + } + return null; + } + + + public static Date[] getMonthStartTimeAndEndTime(Integer month) { + Calendar calendar = Calendar.getInstance(); + Date[] dates = new Date[2]; + Date startTime = null; + Date endsTime = null; + if (month != null) { + calendar.set(Calendar.MONTH, month); + //鑾峰緱鍒版湰鏈堢殑绗竴澶� + calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH)); + startTime = calendar.getTime(); + startTime = DateUtils.removeTime(startTime); + //鑾峰緱鍒版湰鏈堢殑鏈�鍚庝竴澶� + calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); + endsTime = calendar.getTime(); + endsTime = DateUtils.fillTime(endsTime); + } + dates[0] = startTime; + dates[1] = endsTime; + return dates; + } + + 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) { + calendar.set(Calendar.MONTH, month); + //鑾峰緱鍒版湰鏈堢殑绗竴澶� + calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH)); + startTime = calendar.getTime(); + startTime = DateUtils.removeTime(startTime); + //鑾峰緱鍒版湰鏈堢殑鏈�鍚庝竴澶� + calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); + endsTime = calendar.getTime(); + endsTime = DateUtils.fillTime(endsTime); + } + dates[0] = startTime; + dates[1] = endsTime; + return dates; + } + + /** + * 鑾峰彇days澶╃殑鏃堕棿鍖洪棿锛宔ndTime 涓哄綋澶╃殑鍚庝竴澶�0鐐癸紝 + * startTime 涓篹ndTime鍓峝ays澶� + * + * @param days + * @return + */ + public static Date[] getDaysStartTimeAndEndTime(Integer days) { + Date[] dates = new Date[2]; + Date endDate = plusTime(removeTime(getNow()), 1); + Date startDate = subDays(endDate, days); + dates[0] = startDate; + dates[1] = endDate; + return dates; + } + + /** + * 鏍规嵁鏌愪竴骞磋幏鍙栨渶鍚庝竴澶╃殑鏃堕棿 + * 2013 ---> 2013-12-31 23:59:59.000 + * + * @param year + * @return + */ + public static Date getYearEndDay(Integer year) { + Calendar calendar = Calendar.getInstance(); + calendar.clear(); + calendar.set(Calendar.YEAR, year); + calendar.roll(Calendar.DAY_OF_YEAR, -1); + Date yearLast = calendar.getTime(); + Date lastYear = DateUtils.fillTime(yearLast); + return lastYear; + } + + /** + * 鑾峰彇start/end鐨勬墍鏈夋棩鏈熷瓧绗︿覆 鏍煎紡yyyy-MM-dd + * + * @param start + * @param end + * @return + */ + 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) { + list.add(format(start, strDate)); + } else { + list.add(format(plusTime(start, j), strDate)); + } + } + return list; + } + + /** + * 鑾峰彇start/end鐨勬墍鏈夋棩鏈熷瓧绗︿覆 鏍煎紡yyyy-MM-dd + * + * @param start + * @param end + * @return + */ + 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) { + list.add(format(start, STR_DATE)); + } else { + list.add(format(plusTime(start, j), STR_DATE)); + } + } + return list; + } + + /** + * 鑾峰彇start/end鐨勬墍鏈夋棩鏈熷瓧绗︿覆 鏍煎紡yyyyMMdd + * + * @param start + * @param end + * @return + */ + 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) { + list.add(format(start, STRDATE)); + } else { + list.add(format(plusTime(start, j), STRDATE)); + } + } + return list; + } + + /** + * 鑾峰彇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(); + min.setTime(start); + min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1); + 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); + } + return list; + } + + /** + * 鑾峰彇dateStr鐨勬棩鏈熸牸寮弝yyy-MM-dd + * + * @param dateStr + * @return + */ + public static Date getShortDate(String dateStr) { + SimpleDateFormat sdf = new SimpleDateFormat(STR_DATE); + Date startTime = null; + try { + startTime = sdf.parse(dateStr); + } catch (ParseException e) { + } + return startTime == null ? removeTime(new Date()) : startTime; + } + + /** + * 鑾峰彇dateStr鐨勬棩鏈熸牸寮弝yyyMMdd + * + * @param dateStr + * @return + */ + public static Date getShortDate2(String dateStr) { + SimpleDateFormat sdf = new SimpleDateFormat(STRDATE); + Date startTime = null; + try { + startTime = sdf.parse(dateStr); + } catch (ParseException e) { + } + return startTime == null ? removeTime(new Date()) : startTime; + } + + public static Date getFormatDate(String dateStr, String format) { + SimpleDateFormat sdf = new SimpleDateFormat(format); + Date startTime = null; + try { + startTime = sdf.parse(dateStr); + } catch (ParseException e) { + } + return startTime == null ? removeTime(new Date()) : startTime; + } + + /** + * @param time + * @param n + * @return 鍦ㄤ竴涓椂闂翠笂鍔犵 + */ + public static Date addSecond(Date time, Integer n) { + Calendar c = Calendar.getInstance(); + c.setTime(time); + c.add(Calendar.SECOND, n); + return c.getTime(); + } + + /** + * @param time + * @param n + * @return 鍦ㄤ竴涓椂闂翠笂鍔犲垎閽� + */ + public static Date addMinute(Date time, Integer n) { + Calendar c = Calendar.getInstance(); + c.setTime(time); + c.add(Calendar.MINUTE, n); + return c.getTime(); + } + + /** + * @param time + * @param n + * @return 鍦ㄤ竴涓椂闂翠笂鍔犲皬鏃� + */ + public static Date addHour(Date time, Integer n) { + Calendar c = Calendar.getInstance(); + c.setTime(time); + c.add(Calendar.HOUR_OF_DAY, n); + return c.getTime(); + } + + /** + * 鑾峰彇start/end鐨勬墍鏈夋棩鏈熷瓧绗︿覆 鏍煎紡yyyy-MM-dd + * + * @param start + * @param end + * @return + */ + public static List<String> getDateMonth(String start, String end) { + List<String> list = new ArrayList<>(); + SimpleDateFormat yyyyMM = new SimpleDateFormat("yyyy-MM"); + Date startyear = null; + try { + startyear = yyyyMM.parse(start); + Date endyear = yyyyMM.parse(end); + Calendar dd = Calendar.getInstance();//瀹氫箟鏃ユ湡瀹炰緥 + dd.setTime(startyear);//璁剧疆鏃ユ湡璧峰鏃堕棿 + while (dd.getTime().before(endyear)) {//鍒ゆ柇鏄惁鍒扮粨鏉熸棩鏈� + String str = yyyyMM.format(dd.getTime()); + dd.add(Calendar.MONTH, 1);//杩涜褰撳墠鏃ユ湡鏈堜唤鍔�1 + list.add(str); + } + list.add(yyyyMM.format(endyear)); + } catch (ParseException e) { + e.printStackTrace(); + } + return list; + } + + /** + * 鑾峰彇start/end鐨勬墍鏈夋棩鏈熷瓧绗︿覆 鏍煎紡yyyy + * + * @param start + * @param end + * @return + */ + public static List<String> getDateYear(String start, String end) { + List<String> list = new ArrayList<>(); + SimpleDateFormat yyyy = new SimpleDateFormat("yyyy"); + Date startyear = null; + try { + startyear = yyyy.parse(start); + Date endyear = yyyy.parse(end); + Calendar dd = Calendar.getInstance();//瀹氫箟鏃ユ湡瀹炰緥 + dd.setTime(startyear);//璁剧疆鏃ユ湡璧峰鏃堕棿 + while (dd.getTime().before(endyear)) {//鍒ゆ柇鏄惁鍒扮粨鏉熸棩鏈� + String str = yyyy.format(dd.getTime()); + dd.add(Calendar.YEAR, 1);//杩涜褰撳墠鏃ユ湡鏈堜唤鍔�1 + list.add(str); + } + list.add(yyyy.format(endyear)); + } catch (ParseException e) { + e.printStackTrace(); + } + return list; + } + + /** + * 鑾峰彇褰撳墠鏃堕棿 瀹氫綅鍒板皬鏃� + * 鏍煎紡涓� yyyy-MM-dd hh:mm + * 渚嬶細2018-02-27 11:00 + * + * @return + */ + public static Date getNowHourDate() { + Date now = getNow(); + Date result; + Calendar calendar = Calendar.getInstance(); + calendar.setTime(now); + int hour = calendar.get(Calendar.HOUR_OF_DAY); + String dateStr = format(now, STR_DATE); + dateStr = dateStr + " " + (hour < 10 ? "0" + hour : hour) + ":00"; + result = toDate(dateStr, STR_DATE_TIME_MIN); + return result; + } + + /** + * 鑾峰彇姣忔棩8:11鎴�11:11鐨勬椂闂寸偣 + * + * @return + */ + public static Date getNowHourDateTo() { + Date now = getNow(); + Date result; + Calendar calendar = Calendar.getInstance(); + calendar.setTime(now); + int hour = calendar.get(Calendar.HOUR_OF_DAY); + if (hour != 8 && hour != 11) { + return null; + } + int minute = calendar.get(Calendar.MINUTE); + if (minute < 11) { + return null; + } + String dateStr = format(now, STR_DATE); + dateStr = dateStr + " " + (hour < 10 ? "0" + hour : hour) + ":11"; + result = toDate(dateStr, STR_DATE_TIME_MIN); + return result; + } + + /** + * 鑾峰彇姣忔棩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)); + return result; + } + + /** + * 鑾峰彇涓婃湀鐨勬渶鍚庝竴澶� + * + * @return + */ + public static Date getPreviousMonthLastDay() { + Calendar calendar = Calendar.getInstance(); + calendar.set(Calendar.DAY_OF_MONTH, 0); + return DateUtils.removeTime(calendar.getTime()); + } + + /** + * 鑾峰彇end鏃堕棿鏈堢殑绗竴澶� + * + * @param end 鏈堜唤鏈�鍚庝竴澶� + * @return + */ + public static Date getTheMonthFirstDay(Date end) { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(end); + calendar.set(Calendar.DAY_OF_MONTH, 1); + return calendar.getTime(); + } + + public static String secToTimeHour(int time) { + String timeStr = null; + int hour = 0; + if (time <= 0) { + return "0灏忔椂"; + } 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); + timeStr = hourBigDecimal + "灏忔椂"; + } + return timeStr; + } + + public static String secToTime(int time) { + String timeStr = null; + int hour = 0; + int minute = 0; + int second = 0; + if (time <= 0) + return " "; + else { + minute = time / 60; + if (minute < 60) { + second = time % 60; + timeStr = unitFormat(minute) + "鍒�" + unitFormat(second) + " 绉�"; + } else { + hour = minute / 60; + minute = minute % 60; + second = time - hour * 3600 - minute * 60; + timeStr = unitFormat(hour) + "灏忔椂" + unitFormat(minute) + "鍒�" + unitFormat(second) + "绉�"; + } + } + return timeStr; + } + + public static String unitFormat(int i) { + String retStr = null; + if (i >= 0 && i < 10) + retStr = "0" + Integer.toString(i); + else + retStr = "" + i; + return retStr; + } + + /** + * 楠岃瘉鏃堕棿 + * + * @param time 鏃堕棿瀛楃涓� HH:mm + * @return 楠岃瘉鎴愬姛杩斿洖true锛岄獙璇佸け璐ヨ繑鍥瀎alse + */ + public static boolean checkTime(String time) { + String regex = "([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$"; + return Pattern.matches(regex, time); + } + + /** + * 鑾峰彇姣忔棩07:30:00鐨勬椂闂寸偣 + * 鏍煎紡涓� yyyy-MM-dd HH:mm:ss + * 渚嬶細2018-05-28 07:30:00 + * + * @return + */ + public static Date getHourDateTo(Date now, String time) { + String dateStr = format(now, STR_DATE); + dateStr = dateStr + " " + time; + Date result = toDate(dateStr, STR_DATE_TIME_SMALL); + return result; + } + + public static Date setTimeForDay(Date theDate, String planTime) { + 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])); + return cal.getTime(); + } + + public static long getTimeWithOutDay(Date d) { + return d.getTime() / 1000 % 86400; + } + + /** + * @param lo 姣鏁� + * @return String yyyy-MM-dd HH:mm:ss + * @Description: long绫诲瀷杞崲鎴愭棩鏈� + */ + 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) { + 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; + } + + /** + * Date涓嶭ocalDateTime浜掔浉杞崲鐨勫伐鍏锋柟娉� + * + * @param date + * @return + */ + public static LocalDateTime convertToLocalDateTime(Date date) { + if (date == null) { + return null; + } + return Instant.ofEpochMilli(date.getTime()) + .atZone(ZoneId.systemDefault()) + .toLocalDateTime(); + } + + /** + * Date涓嶭ocalDateTime浜掔浉杞崲鐨勫伐鍏锋柟娉� + * + * @param localDateTime + * @return + */ + public static Date convertToDate(LocalDateTime localDateTime) { + if (localDateTime == null) { + return null; + } + return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); + } +} -- Gitblit v1.9.3