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"; /** *
* Description: 实际投标月份 *
* * @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; } /** ** Description: 修改时间为指定时间当天的23:59:59.000 *
* * @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; } /** ** Description: 修改时间为指定时间前一天的00:00:00 *
* * @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(); } /** ** Description: 去掉日期时间中的时间部分 *
* 如: 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; } /** ** Description: 按默认格式(yyyy-MM-dd HH:mm:ss.SSS)获取时间字符串 *
* * @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; } /** ** Description: 按指定格式获取时间字符串 *
* * @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); } /** ** Description: 加月函数 *
* * @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天的时间区间,endTime 为当天的后一天0点, * startTime 为endTime前days天 * * @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