DateUtil.java 7.8 KB
package cn.com.glsx.entmicro.util;

import com.glsx.plat.common.utils.DateUtils;
import com.glsx.plat.common.utils.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import scala.util.parsing.combinator.testing.Str;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtil extends org.apache.commons.lang.time.DateUtils {

    public static final String NORMAL_DATE_PATTERN;

    public static final String reg = "(\\d{4}[/-]?\\d{1,2}[/-]?\\d{1,2})?";

    public static final String reg1 = "(\\d{4}[/-]?\\d{1,2}[/-]?\\d{1,2} \\d{1,2}:\\d{1,2}[:]?\\d{1,2})?";

    public static final String reg2 = "(\\d{14})?";

    public static final String reg3 = "(\\d{4}[/-]?\\d{1,2}[/-]?\\d{1,2} \\d{1,2}:\\d{1,2}[:]?\\d{1,2})?";

    public static final String reg4 = "(\\d{14})?";

    static {
        NORMAL_DATE_PATTERN = DateFormatUtils.ISO_DATE_FORMAT.getPattern();
    }

    //日期比较方法
    public static int compareDate(Date first, Date second) {
        return first.compareTo(second);
    }

    //获取近datNum天日期,从今日
    public static List<String> getDates(int datNum, boolean fromNow) {
        int index = 1;
        int end = datNum + 1;
        if (fromNow) {
            index = 0;
            end = datNum;
        }
        List<String> dateList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        for (int i = index; i < end; i++) {
            Date date = DateUtils.addDays(new Date(), -i);
            String formatDate = sdf.format(date);
            dateList.add(formatDate);
        }
        return dateList;
    }

    //获取近datNum天日期,从今日
    public static List<String> getDates(Date dateEnd, int datNum) {
        List<String> dateList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        for (int i = 0; i < datNum; i++) {
            Date date = DateUtils.addDays(dateEnd, -i);
            String formatDate = sdf.format(date);
            dateList.add(formatDate);
        }
        return dateList;
    }

    public static Date parse(String strDate) {
        Date date = null;
        try {
            if (StringUtils.isNotEmpty(strDate)) {
                if (strDate.matches(reg3)) {
                    date = parseDate(strDate, new String[]{NORMAL_DATE_PATTERN, "yyyy-MM-dd HH:mm:ss"});
                } else {
                    date = parseDate(strDate, new String[]{NORMAL_DATE_PATTERN, "yyyy/MM/dd"});
                }
            }
        } catch (ParseException var3) {
            var3.printStackTrace();
        }
        return date;
    }

    public static Set<String> getMonths(int datNum) {
        Set<String> dateList = new HashSet<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM");
        for (int i = 0; i < datNum; i++) {
            Date date = DateUtils.addDays(new Date(), -i);
            String formatDate = sdf.format(date);
            dateList.add(formatDate);
        }
        return dateList;
    }

    public static Integer getMonthDays(String date) {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM");
        try {
            cal.setTime(sdf.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
       /* cal.set(Calendar.YEAR, 2002 );
        cal.set(Calendar.MONTH, 6 ); // 7月*/
        int maxDate = cal.getActualMaximum(Calendar.DATE);
        return maxDate;
    }

    public static List<String> getAllDates(String minDay) {
        List<String> dateList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        int i = 0;
        while (true) {
            Date date = DateUtils.addDays(new Date(), -i);
            String formatDate = sdf.format(date);
            System.out.println("最小日期" + minDay);
            System.out.println("当前日期" + formatDate);
            dateList.add(formatDate);
            if (formatDate.equals(minDay)) {
                break;
            }
            i++;
        }
        return dateList;
    }

    /**
     * 合并日期
     *
     * @return
     */
    public static Date mergeDate(Date date, String datetime) {
        SimpleDateFormat ds = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        SimpleDateFormat dsp = new SimpleDateFormat("yyyy/MM/dd");
        try {
            String format = dsp.format(date.getTime());
            Date endTime = ds.parse(format + " " + datetime);
            return endTime;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static List<String> getDateTimeList() {
        List<String> timelist = new ArrayList<>();
        for (int i = 0; i < 25; i++) {
            if (i < 10) {
                timelist.add("0" + i + ":00");
            } else {
                timelist.add(i + ":00");
            }
        }
        return timelist;
    }

    /**
     * 获取时间段内的时间集合
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    public static List<String> getDateList(String startTime, String endTime) {
        List<String> dateList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = DateUtils.stringToDate(startTime, "yyyy-MM-dd");
        int i = 0;
        while (true) {
            Date date = DateUtils.addDays(startDate, +i);
            String formatDate = sdf.format(date);
            dateList.add(formatDate);
            if (formatDate.equals(endTime)) {
                break;
            }
            i++;
        }
        return dateList;
    }

    /**
     * 获取时间段内的时间集合
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    public static List<String> getDateList(Date startTime, Date endTime) {
        List<String> dateList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String endDate = sdf.format(endTime);
        int i = 0;
        while (true) {
            Date date = DateUtils.addDays(startTime, +i);
            String formatDate = sdf.format(date);
            dateList.add(formatDate);
            if (formatDate.equals(endDate)) {
                break;
            }
            i++;
        }
        return dateList;
    }

    public static boolean isDate(String str) {
        String reg = "(\\d{4}[/-]?\\d{1,2}[/-]?\\d{1,2})?";
        String reg1 = "(\\d{4}[/-]?\\d{1,2}[/-]?\\d{1,2} \\d{1,2}:\\d{1,2}[:]?\\d{1,2})?";
        String reg2 = "(\\d{14})?";
        boolean matches = str.matches(reg);
        if (matches) {
            return true;
        }
        boolean matches1 = str.matches(reg1);
        if (matches1) {
            return true;
        }
        return str.matches(reg2);
    }

    /**
     * 计算日期相差分钟数
     * @param start
     * @param end
     * @return
     */
    public static long diffMinute(Date start, Date end) {
        if (start == null || end == null) {
            return 0;
        }
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(start);
        c2.setTime(end);
        if (c1.after(c2)) {
            return 0;
        } else {
            long diff = c2.getTimeInMillis() - c1.getTimeInMillis();
            return diff / (60 * 1000);
        }
    }

    public static String timeStampSecToDateStr(String timestampString) {
        try {
            long parseLong = Long.parseLong(timestampString);
            long timestamp = parseLong * 1000L;
            return DateFormatUtils.format(timestamp, "yyyy-MM-dd HH:mm:ss");
        } catch (Exception var6) {
            var6.printStackTrace();
            return "";
        }
    }
}