luodangjia
2025-01-01 2dc478231fd09a88a4d86d44388ae807aca08bc5
medicalWaste-common/src/main/java/com/sinata/common/utils/DateUtils.java
@@ -1,5 +1,7 @@
package com.sinata.common.utils;
import org.apache.commons.lang3.time.DateFormatUtils;
import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
@@ -8,8 +10,10 @@
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import org.apache.commons.lang3.time.DateFormatUtils;
import java.util.List;
/**
 * 时间工具类
@@ -171,6 +175,35 @@
    }
    /**
     * 计算小时差
     *
     * @param endDate
     * @param startTime
     * @return
     */
    public static long timeDistanceHour(Date endDate, Date startTime) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long diff = endDate.getTime() - startTime.getTime();
        long hour = diff % nd / nh;
        return hour;
    }
    /**
     * 计算天数差
     *
     * @param endDate
     * @param startTime
     * @return
     */
    public static long timeDistanceDay(Date endDate, Date startTime) {
        long nd = 1000 * 24 * 60 * 60;
        long diff = endDate.getTime() - startTime.getTime();
        long day = diff / nd;
        return day;
    }
    /**
     * 增加 LocalDateTime ==> Date
     */
    public static Date toDate(LocalDateTime temporalAccessor)
@@ -188,4 +221,62 @@
        ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
        return Date.from(zdt.toInstant());
    }
    /**
     * 获取两个时间节点之间的月份列表
     *
     * @param minDate
     * @param maxDate
     * @param type    1 = 日 2 = 月 3 = 年
     * @return
     */
    public static List<String> getDayBetween(Date minDate, Date maxDate, Integer type) {
        List<String> result = new ArrayList<String>();
        if (type == 3) {//年
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy");//格式化为年
            Calendar min = Calendar.getInstance();
            Calendar max = Calendar.getInstance();
            min.setTime(minDate);
            max.setTime(maxDate);
            Calendar curr = min;
            max.add(Calendar.YEAR, 1);
            while (curr.before(max)) {
                result.add(sdf.format(curr.getTime()));
                curr.add(Calendar.YEAR, 1);
            }
        } else if (type == 2) {//月
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月
            Calendar min = Calendar.getInstance();
            Calendar max = Calendar.getInstance();
            min.setTime(minDate);
            max.setTime(maxDate);
            Calendar curr = min;
            max.add(Calendar.MONTH, 1);
            while (curr.before(max)) {
                result.add(sdf.format(curr.getTime()));
                curr.add(Calendar.MONTH, 1);
            }
        } else {//日
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//格式化为年月日
            Calendar min = Calendar.getInstance();
            Calendar max = Calendar.getInstance();
            min.setTime(minDate);
            max.setTime(maxDate);
            Calendar curr = min;
            max.add(Calendar.DATE, 1);
            while (curr.before(max)) {
                result.add(sdf.format(curr.getTime()));
                curr.add(Calendar.DATE, 1);
            }
        }
        return result;
    }
}