zenglf
2023-09-28 f84d9e69907cb678150eaa6393fd74cf042fcca4
lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/util/DateUtils.java
@@ -1,12 +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 {
@@ -15,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秒
     */
@@ -65,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;
    }
    /**
@@ -129,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 {
@@ -154,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: 实际投标月份
@@ -206,8 +219,7 @@
     * Description: 修改时间为指定时间当天的23:59:59.000
     * </p>
     *
     * @param date
     *            需要修改的时间
     * @param date 需要修改的时间
     * @return 修改后的时间
     */
    public static Date fillTime(Date date) {
@@ -217,10 +229,8 @@
    }
    /**
     * @param date
     *            当前时间
     * @param i
     *            往前几天
     * @param date 当前时间
     * @param i    往前几天
     * @return
     */
    public static Date fillBeforeTime(Date date, Integer i) {
@@ -238,8 +248,7 @@
     * Description: 修改时间为指定时间前一天的00:00:00
     * </p>
     *
     * @param date
     *            需要修改的时间
     * @param date 需要修改的时间
     * @return 修改后的时间
     */
    public static Date plusTime(Date date, Integer i) {
@@ -256,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) {
@@ -277,8 +285,7 @@
     * Description: 按默认格式(yyyy-MM-dd HH:mm:ss.SSS)获取时间字符串
     * </p>
     *
     * @param date
     *            要转换的日期
     * @param date 要转换的日期
     * @return 转换后的时间字符串
     */
    public static String format(Date date) {
@@ -286,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));
@@ -296,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) {
@@ -323,8 +329,7 @@
     * Description: 加月函数
     * </p>
     *
     * @param month
     *            月份数
     * @param month 月份数
     * @return
     */
    public static Date addMonth(Integer month, Date time) {
@@ -381,7 +386,7 @@
            // 计算秒数
            Long days = (t2 - t1) / (1000);
            return days.intValue();
        }catch (Exception e) {
        } catch (Exception e) {
            return 0;
        }
@@ -399,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);
@@ -414,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--;
            }
@@ -438,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) {
@@ -448,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);
@@ -458,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);
@@ -467,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);
@@ -476,6 +483,7 @@
            return null;
        }
    }
    public static Integer getDate(Date date) {
        if (date != null) {
            Calendar calendar = Calendar.getInstance();
@@ -485,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();
@@ -497,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();
@@ -523,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));
@@ -543,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));
@@ -569,6 +584,7 @@
    /**
     * 获取days天的时间区间,endTime 为当天的后一天0点,
     * startTime 为endTime前days天
     *
     * @param days
     * @return
     */
@@ -584,6 +600,7 @@
    /**
     * 根据某一年获取最后一天的时间
     * 2013 ---> 2013-12-31 23:59:59.000
     *
     * @param year
     * @return
     */
@@ -599,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));
@@ -618,6 +636,7 @@
    /**
     * 获取start/end的所有日期字符串 格式yyyy-MM-dd
     *
     * @param start
     * @param end
     * @return
@@ -625,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));
@@ -637,6 +656,7 @@
    /**
     * 获取start/end的所有日期字符串 格式yyyyMMdd
     *
     * @param start
     * @param end
     * @return
@@ -644,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));
@@ -654,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();
@@ -663,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的日期格式yyyy-MM-dd
     *
     * @param dateStr
     * @return
     */
@@ -687,6 +728,7 @@
    /**
     * 获取dateStr的日期格式yyyyMMdd
     *
     * @param dateStr
     * @return
     */
@@ -700,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 {
@@ -711,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);
@@ -724,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);
@@ -737,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);
@@ -751,6 +790,7 @@
    /**
     * 获取start/end的所有日期字符串 格式yyyy-MM-dd
     *
     * @param start
     * @param end
     * @return
@@ -775,8 +815,10 @@
        }
        return list;
    }
    /**
     * 获取start/end的所有日期字符串 格式yyyy
     *
     * @param start
     * @param end
     * @return
@@ -801,10 +843,12 @@
        }
        return list;
    }
    /**
     * 获取当前时间 定位到小时
     * 格式为 yyyy-MM-dd hh:mm
     * 例:2018-02-27 11:00
     *
     * @return
     */
    public static Date getNowHourDate() {
@@ -821,6 +865,7 @@
    /**
     * 获取每日8:11或11:11的时间点
     *
     * @return
     */
    public static Date getNowHourDateTo() {
@@ -829,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);
@@ -844,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() {
@@ -866,6 +913,7 @@
    /**
     * 获取end时间月的第一天
     *
     * @param end 月份最后一天
     * @return
     */
@@ -881,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;
@@ -923,6 +970,7 @@
            retStr = "" + i;
        return retStr;
    }
    /**
     * 验证时间
     *
@@ -952,24 +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) {
        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转int 存在溢出情况  根据业务情况编辑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;
    }
}