日期处理

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONArray;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.*;
import java.util.stream.Collectors;


/**
 * 日期处理
 *
 * @description: 日期处理
 * @author: xux
 * @date: 2021-01-15 00:00:00
 **/
public class DateUtils {
    /**
     * 时间格式(yyyy-MM)
     */
    public final static String DATE_DAY = "MM-dd";
    /**
     * 时间格式(yyyy-MM)
     */
    public final static String DATE_MONTH = "yyyy-MM";
    /**
     * 时间格式(yyyy-MM-dd)
     */
    public final static String DATE_PATTERN = "yyyy-MM-dd";
    /**
     * 时间格式(yyyy-MM-dd HH:mm:ss)
     */
    public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    /**
     * 时间格式(yyyyMMddHHmmss)
     */
    public final static String DATA_TIME_SFN = "yyyyMMddHHmmss";
    /**
     * 时间格式(yyyyMMddHHmmssSSS)
     */
    public final static String DATA_TIME_MILLISECOND = "yyyyMMddHHmmssSSS";
    /**
     * 校验的时间格式
     */
    private static String[] parsePatterns = {"yyyy-MM-dd", "yyyy年MM月dd日", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyyMMdd", "yyyy.MM.dd"};


    /**
     * 获取当前日期(格式:yyyyMMddHHmmss)
     * <p>
     * DATE_TIME_PATTERN
     */
    public static String getSfnDate() {
        DateFormat fmt = new SimpleDateFormat(DATA_TIME_SFN);
        return fmt.format(new Date());
    }


    /**
     * 日期格式化
     *
     * @param date    日期
     * @param pattern 格式,如:DateUtils.DATE_TIME_PATTERN
     * @return 返回yyyy-MM-dd格式日期
     */
    public static String format(Date date, String pattern) {
        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            return df.format(date);
        }
        return null;
    }

    /**
     * 获取当前日期(格式:yyyy-MM-dd HH:mm:ss)
     * <p>
     * DATE_TIME_PATTERN
     */
    public static String getCurrentDate() {
        DateFormat fmt = new SimpleDateFormat(DATE_TIME_PATTERN);
        return fmt.format(new Date());
    }

    /**
     * 获取当前日期(格式:MM-dd)
     * <p>
     * DATE_TIME_PATTERN
     */
    public static String getDayTime() {
        DateFormat fmt = new SimpleDateFormat(DATE_DAY);
        return fmt.format(new Date());
    }

    /**
     * 获取当前日期(格式:yyyy-MM-dd)
     * <p>
     * DATE_TIME_PATTERN
     */
    public static String getCurrentTime() {
        DateFormat fmt = new SimpleDateFormat(DATE_PATTERN);
        return fmt.format(new Date());
    }

    /**
     * 字符串转换成自定义格式日期
     *
     * @param strDate 日期字符串
     * @param pattern 日期的格式,如:DateUtils.DATE_TIME_PATTERN
     */
    public static Date stringToDate(String strDate, String pattern) {
        if (StringUtils.isBlank(strDate)) {
            return null;
        }
        DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern);
        return fmt.parseLocalDateTime(strDate).toDate();
    }

    /**
     * 返回上一个月的日期
     *
     * @param date 日期
     * @return 返回yyyy-MM格式日期
     */
    public static String lastMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, -1);
        return format(cal.getTime(), DATE_MONTH);
    }

    /**
     * 返回下一个月的日期
     *
     * @param date 日期
     * @return 返回yyyy-MM格式日期
     */
    public static String nextMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, +1);
        return format(cal.getTime(), DATE_MONTH);
    }

    /**
     * 获取当前年份
     */
    public static String getYear() {
        Calendar date = Calendar.getInstance();
        String year = String.valueOf(date.get(Calendar.YEAR));
        return year;
    }

    /**
     * 获取当前月份
     * <p>
     * DATE_TIME_PATTERN
     */
    public static String getMonth() {
        Date date = new Date();
        Calendar from = Calendar.getInstance();
        from.setTime(date);
        int fromMonth = from.get(Calendar.MONTH) + 1;
        String month = DateUtils.padDatazero(Integer.parseInt(fromMonth + ""));
        return month;
    }

    /**
     * 获取n年前后之间年份
     */
    public static List<String> getNumYear(int num) {
        List<String> list = new ArrayList<String>();
        for (int i = 0; i >= num; i--) {
            Calendar calendar = Calendar.getInstance();
            //过去num年
            calendar.setTime(new Date());
            calendar.add(Calendar.YEAR, i);
            String year = String.valueOf(calendar.get(Calendar.YEAR));
            list.add(year);
        }
        return list;
    }

    /**
     * 根据周数,获取开始日期、结束日期
     *
     * @param week 周期  0本周,-1上周,-2上上周,1下周,2下下周
     * @return 返回date[0]开始日期、date[1]结束日期
     */
    public static Date[] getWeekStartAndEnd(int week) {
        DateTime dateTime = new DateTime();
        LocalDate date = new LocalDate(dateTime.plusWeeks(week));
        date = date.dayOfWeek().withMinimumValue();
        Date beginDate = date.toDate();
        Date endDate = date.plusDays(6).toDate();
        return new Date[]{beginDate, endDate};
    }

    /**
     * 对日期的【秒】进行加/减
     *
     * @param date    日期
     * @param seconds 秒数,负数为减
     * @return 加/减几秒后的日期
     */
    public static Date addDateSeconds(Date date, int seconds) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusSeconds(seconds).toDate();
    }

    /**
     * 对日期的【分钟】进行加/减
     *
     * @param date    日期
     * @param minutes 分钟数,负数为减
     * @return 加/减几分钟后的日期
     */
    public static Date addDateMinutes(Date date, int minutes) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusMinutes(minutes).toDate();
    }

    /**
     * 对日期的【小时】进行加/减
     *
     * @param date  日期
     * @param hours 小时数,负数为减
     * @return 加/减几小时后的日期
     */
    public static Date addDateHours(Date date, int hours) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusHours(hours).toDate();
    }

    /**
     * 对日期的【天】进行加/减
     *
     * @param date 日期
     * @param days 天数,负数为减
     * @return 加/减几天后的日期
     */
    public static Date addDateDays(Date date, int days) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusDays(days).toDate();
    }

    /**
     * 对日期的【周】进行加/减
     *
     * @param date  日期
     * @param weeks 周数,负数为减
     * @return 加/减几周后的日期
     */
    public static Date addDateWeeks(Date date, int weeks) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusWeeks(weeks).toDate();
    }

    /**
     * 对日期的【月】进行加/减
     *
     * @param date   日期
     * @param months 月数,负数为减
     * @return 加/减几月后的日期
     */
    public static Date addDateMonths(Date date, int months) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusMonths(months).toDate();
    }

    /**
     * 对日期的【年】进行加/减
     *
     * @param date  日期
     * @param years 年数,负数为减
     * @return 加/减几年后的日期
     */
    public static Date addDateYears(Date date, int years) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusYears(years).toDate();
    }

    /**
     * 获取某时间段内的某个时间
     *
     * @param start
     * @param end
     * @return
     */
    public static List<String> getBetweenDates(String start, String end) {
        List<String> result = new ArrayList<String>();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_PATTERN);
            SimpleDateFormat rsdf = new SimpleDateFormat(DATE_PATTERN);
            Date start_date = sdf.parse(start);
            Date end_date = sdf.parse(end);
            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start_date);
            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end_date);
            while (tempStart.before(tempEnd) || tempStart.equals(tempEnd)) {
                result.add(rsdf.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 获取某时间段内的某个时间
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    public static String randomDate(String beginDate, String endDate, boolean isDate, String pattern) {
        // TODO Auto-generated method stub
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        try {
            Date start = format.parse(beginDate);
            Date end = format.parse(endDate);
            if (start.getTime() >= end.getTime()) {
                return null;
            }
            long date = random(start.getTime(), end.getTime());
            Date sr = new Date(date);
            String d = new SimpleDateFormat(pattern).format(new Date(date));
            if (isDate) {
                return d.substring(0, 10);
            } else {
                return d.substring(10);
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取两数之间随机数
     *
     * @param begin
     * @param end
     * @return
     */
    public static long random(long begin, long end) {
        // TODO Auto-generated method stub
        long rtn = begin + (long) (Math.random() * (end - begin));
        if (rtn == begin || rtn == end) {
            return random(begin, end);
        }
        return rtn;
    }

    /**
     * 毫秒值转日期
     *
     * @param time
     * @return
     */
    public static String getDateByTime(String time) {
        Long t = Long.parseLong(time);
        Date d = new Date(t);
        return format(d, DATE_TIME_PATTERN);
    }

    /**
     * 数据补零
     *
     * @param s
     * @return
     */
    public static String padDatazero(int s) {
        return s < 10 ? "0" + s : "" + s;
    }

    /**
     * 根据开始时间和结束时间返回时间段内的时间集合
     *
     * @param beginDate
     * @param endDate
     * @return List<Date>
     * @throws ParseException
     */
    public static List<String> getDatesBetweenTwoDate(String beginDate, String endDate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN);
        List<String> lDate = new ArrayList<String>();
        lDate.add(beginDate);//把开始时间加入集合
        Calendar cal = Calendar.getInstance();
        //使用给定的 Date 设置此 Calendar 的时间
        cal.setTime(sdf.parse(beginDate));
        boolean bContinue = true;
        while (bContinue) {
            //根据日历的规则,为给定的日历字段添加或减去指定的时间量
            cal.add(Calendar.DAY_OF_MONTH, 1);
            // 测试此日期是否在指定日期之后
            if (sdf.parse(endDate).after(cal.getTime())) {
                lDate.add(sdf.format(cal.getTime()));
            } else {
                break;
            }
        }
        lDate.add(endDate);//把结束时间加入集合
        return lDate;
    }

    /**
     * 判断时间是否在范围内
     *
     * @param createTime
     * @param beginTime
     * @param endTime
     * @return
     * @throws Exception
     */
    public static Boolean isInTime(String createTime, String beginTime, String endTime, String pattern) {
        return DateUtil.isIn(DateUtil.parse(createTime, pattern)
                , DateUtil.parse(beginTime, pattern)
                , DateUtil.parse(endTime, pattern)
        );
    }

    /**
     * 日期转换成字符串
     *
     * @param date
     * @return str
     */
    public static String DateToStr(Date date, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        String str = format.format(date);
        return str;
    }

    /**
     * 字符串转换成日期
     *
     * @param str
     * @return date
     */
    public static Date StrToDate(String str, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            if (!StringUtils.isNotBlank(str) || str.equals("无")) {
                date = new Date();
            } else {
                date = format.parse(str);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 获取两个日期之间的毫秒值
     *
     * @param date1
     * @param date2
     * @return date
     */
    public static Long daysDifference(Date date1, Date date2) {
        // 获取相差的天数
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date1);
        long timeInMillis1 = calendar.getTimeInMillis();
        calendar.setTime(date2);
        long timeInMillis2 = calendar.getTimeInMillis();
        long betweenDays = (timeInMillis2 - timeInMillis1) / (1000L * 3600L * 24L);
        return betweenDays;
    }

    /**
     * 获取当前年份1月至今(不包括当前月)的所有月份
     */
    public static List<String> getcurrentMonthList() {
        List<String> reList = new ArrayList<String>();
        Calendar canlendar = Calendar.getInstance();
        int curmonth = canlendar.get(Calendar.MONTH) + 1;
        for (int i = 0; i < curmonth - 1; i++) {
            canlendar.set(Calendar.DAY_OF_MONTH, 0);
            reList.add(format(canlendar.getTime(), DATE_MONTH));
        }
        return reList;
    }

    /**
     * 截取日期
     *
     * @param workShiftList
     * @return
     */
    public static String subDate(JSONArray workShiftList) {
        String workShifTime = (String) workShiftList.get(1);// 2020-05-01
        String creationTime = workShifTime.substring(0, 7);// 截取年月
        return creationTime;
    }

    /**
     * 截取日期
     *
     * @param dateTime (yyyy-MM-dd)
     * @return
     */
    public static String subDate(String dateTime) {
        String creationTime = dateTime.substring(0, 7);// 截取年月
        return creationTime;
    }

    /**
     * 获取某一年的首月
     *
     * @param dateTime (yyyy-MM)
     * @return
     */
    public static String getFirstMonth(String dateTime) {
        String creationTime = dateTime.substring(0, 4) + "-01";
        return creationTime;
    }

    /**
     * 获取下一月的当前日期
     *
     * @param dataTime
     * @return
     */
    public static String getLastDateMonth(String dataTime) {
        LocalDate today = LocalDate.parse(dataTime);
        //当前月份+(-1)
        today = today.minusMonths(-1);
        //LocalDate日期格式是"YYYY-MM-DD",只需要用toString()就可以转化成字符串类型
        return today.toString();
    }

    /**
     * 获取当前日期半年后的日期
     *
     * @param dataTime
     * @return
     */
    public static String getPastHalfYear(String dataTime, Integer month) {
        Calendar c = Calendar.getInstance();//获得一bai个日历的实例
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_PATTERN);
        Date date = null;
        try {
            date = sdf.parse(dataTime);//初始日期
        } catch (Exception e) {
        }
        c.setTime(date);//设置du日历时间
        c.add(Calendar.MONTH, month);//在日历的月份zhi上增加6个月
        return sdf.format(c.getTime());

    }

    /**
     * 获取当前月的第一天与最后一天
     *
     * @return
     */
    public static Map<String, String> getFirstdayAndlastday() {
        Map<String, String> map = new HashMap<>();
        // 获取当前年份、月份、日期
        Calendar cale = null;
        // 获取当月第一天和最后一天
        SimpleDateFormat format = new SimpleDateFormat(DATE_TIME_PATTERN);
        String firstday, lastday;
        // 获取当前月的第一天
        cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        firstday = format.format(cale.getTime());
        // 获取当前月的最后一天
        cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 1);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        lastday = format.format(cale.getTime());
        map.put("firstday", firstday);
        map.put("lastday", lastday);
        return map;
    }

    /**
     * 获取n分钟后的时间
     *
     * @param minute n分钟
     * @return
     */
    public static String getTimeByMinute(int minute) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, minute);
        return new SimpleDateFormat(DATE_TIME_PATTERN).format(calendar.getTime());
    }

    /**
     * 毫秒值转时分秒
     *
     * @param time 毫秒值
     * @return
     */
    public static String getGapTime(long time) {
        long hours = time / (1000 * 60 * 60);
        long minutes = (time - hours * (1000 * 60 * 60)) / (1000 * 60);
        String diffTime = "";
        if (minutes < 10) {
            diffTime = hours + ":0" + minutes;
        } else {
            diffTime = hours + ":" + minutes;
        }
        return diffTime;
    }

    /**
     * 拼接当天开始结束时间
     *
     * @param startStr
     * @param endStr
     * @return
     */
    public static Map<String, String> splicingTime(String startStr, String endStr) {
        Map<String, String> map = new HashMap<String, String>();
        String[] startTimeSplit = startStr.split(" ");
        String[] endTimeSplit = endStr.split(" ");
        // 获取当天时间
        String currentTime = getCurrentTime();
        // 拼接开始时间
        String startTime = currentTime + " " + startTimeSplit[1];
        // 拼接结束时间
        String endTime = currentTime + " " + endTimeSplit[1];
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        return map;
    }

    /**
     * 计算2个日期之间相差的  相差多少年月日
     * 比如:2011-02-02 到  2017-03-02 相差 6年,1个月,0天
     *
     * @param fromDate
     * @param toDate
     * @return
     */
    public static Map<String, Integer> dayComparePrecise(Date fromDate, Date toDate) {
        Calendar from = Calendar.getInstance();
        from.setTime(fromDate);
        Calendar to = Calendar.getInstance();
        to.setTime(toDate);
        int fromYear = from.get(Calendar.YEAR);
        int fromMonth = from.get(Calendar.MONTH);
        int fromDay = from.get(Calendar.DAY_OF_MONTH);
        int toYear = to.get(Calendar.YEAR);
        int toMonth = to.get(Calendar.MONTH);
        int toDay = to.get(Calendar.DAY_OF_MONTH);
        int year = toYear - fromYear;
        int month = toMonth - fromMonth;
        int day = toDay - fromDay;
        Map<String, Integer> map = new HashMap<>();
        map.put("year", year);
        map.put("month", month);
        map.put("day", day);
        return map;
    }

    /**
     * 计算2个日期之间相差的  以年、月、日为单位,各自计算结果是多少
     * 比如:2011-02-02 到  2017-03-02
     * 以年为单位相差为:6年
     * 以月为单位相差为:73个月
     * 以日为单位相差为:2220天
     *
     * @param fromDate
     * @param toDate
     * @return
     */
    public static Map<String, Integer> dayCompare(Date fromDate, Date toDate) {
        Calendar from = Calendar.getInstance();
        from.setTime(fromDate);
        Calendar to = Calendar.getInstance();
        to.setTime(toDate);
        //只要年月
        int fromYear = from.get(Calendar.YEAR);
        int fromMonth = from.get(Calendar.MONTH);
        int toYear = to.get(Calendar.YEAR);
        int toMonth = to.get(Calendar.MONTH);
        int year = toYear - fromYear;
        int month = toYear * 12 + toMonth - (fromYear * 12 + fromMonth);
        int day = (int) ((to.getTimeInMillis() - from.getTimeInMillis()) / (24 * 3600 * 1000));
        Map<String, Integer> map = new HashMap<>();
        map.put("year", year);
        map.put("month", month);
        map.put("day", day);
        return map;
    }

    /**
     * 计算2个日期相差多少年
     * 列:2011-02-02  ~  2017-03-02 大约相差 6.1 年
     *
     * @param fromDate
     * @param toDate
     * @return
     */
    public static Map<String, Object> yearCompare(String fromDate, String toDate) {
        Date date1 = StrToDate(fromDate, DATE_PATTERN);
        Date date2 = StrToDate(toDate, DATE_PATTERN);
        Map<String, Integer> timeMap = dayComparePrecise(date1, date2);
        double month = timeMap.get("month");
        double year = timeMap.get("year");
        // 返回2位小数,并且四舍五入
        DecimalFormat df = new DecimalFormat("######0.0");
        int useYear = (int) (year + month / 12);
        String useYears = df.format(year + month / 12);
        Map<String, Object> map = new HashMap<>();
        map.put("useYear", useYear);
        map.put("useYears", useYears);
        return map;
    }

    /**
     * 比较日期大小
     *
     * @param beginTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    public static boolean compareDateSizeBefore(Date beginTime, Date endTime) {
        boolean before = beginTime.after(endTime);
        return before;
    }

    public static boolean compareDateSizeAfter(Date beginTime, Date endTime) {
        boolean after = beginTime.before(endTime);
        return after;
    }

    /**
     * 比较日期大小
     *
     * @param beginTime 开始时间
     * @param endTime   结束时间
     *                  date1小于date2返回-1,date1大于date2返回1,相等返回0
     * @return
     */
    public static int compareDate(Date beginTime, Date endTime) {
        int compare = beginTime.compareTo(endTime);
        return compare;
    }

    /**
     * 获取当天开始时间
     *
     * @return
     */
    public static Date getFirstTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date time = calendar.getTime();
        return calendar.getTime();
    }

    /**
     * 获取当天结束时间
     *
     * @return
     */
    public static Date getEndTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        Date time = calendar.getTime();
        return calendar.getTime();
    }

    /**
     * 获取当前周开始时间
     *
     * @return
     */
    public static Date getWeekFirstTime() {
        Calendar currentDate = new GregorianCalendar();
        currentDate.setFirstDayOfWeek(Calendar.MONDAY);
        currentDate.set(Calendar.HOUR_OF_DAY, 0);
        currentDate.set(Calendar.MINUTE, 0);
        currentDate.set(Calendar.SECOND, 0);
        currentDate.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return currentDate.getTime();
    }

    /**
     * 获取当前周结束时间
     *
     * @return
     */
    public static Date getWeekEndTime() {
        Calendar currentDate = new GregorianCalendar();
        currentDate.setFirstDayOfWeek(Calendar.MONDAY);
        currentDate.set(Calendar.HOUR_OF_DAY, 23);
        currentDate.set(Calendar.MINUTE, 59);
        currentDate.set(Calendar.SECOND, 59);
        currentDate.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        return currentDate.getTime();
    }

    /**
     * 获取某一月份的第一天的开始时间
     *
     * @param day yyyy-MM-dd
     * @return
     */
    public static Date getMonthFirstTime(Date day) {
        String timeData = format(day, DATE_TIME_PATTERN);
        int year = Integer.parseInt(timeData.substring(0, 4));
        int month = Integer.parseInt(timeData.substring(5, 7));
        YearMonth yearMonth = YearMonth.of(year, month);
        java.time.LocalDate localDate = yearMonth.atDay(1);
        LocalDateTime startOfDay = localDate.atStartOfDay();
        ZonedDateTime zonedDateTime = startOfDay.atZone(ZoneId.of("Asia/Shanghai"));
        return Date.from(zonedDateTime.toInstant());

    }

    /**
     * 获取某一月份的最后一天的结束时间
     *
     * @param day
     * @return
     */
    public static Date getMonthEndTime(Date day) {
        String timeData = format(day, DATE_TIME_PATTERN);
        int year = Integer.parseInt(timeData.substring(0, 4));
        int month = Integer.parseInt(timeData.substring(5, 7));
        YearMonth yearMonth = YearMonth.of(year, month);
        java.time.LocalDate endOfMonth = yearMonth.atEndOfMonth();
        LocalDateTime localDateTime = endOfMonth.atTime(23, 59, 59, 999);
        ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of("Asia/Shanghai"));
        return Date.from(zonedDateTime.toInstant());

    }

    /**
     * 获取上一年的年份
     *
     * @param timeData yyyy-MM-dd
     * @return
     */
    public static String getLastYear(String timeData) {
        int year = Integer.parseInt(timeData.substring(0, 4));
        return String.valueOf(year - 1);

    }

    /**
     * 获取上个月时间
     *
     * @return
     */
    public static String getLastMonth() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date); // 设置为当前时间
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); // 设置为上一个月
        date = calendar.getTime();
        String accDate = format.format(date);
        return accDate;
    }

    /**
     * 获取时间数据
     *
     * @return
     */
    public static Map<String, Integer> getTimeData(Date date) {
        Map<String, Integer> map = new HashMap<>();
        String year = String.format("%tY", date);
        String month = String.format("%tm", date);
        String day = String.format("%td", date);
        String hours = String.format("%tH", date);
        String minytes = String.format("%tM", date);
        String seconds = String.format("%tS", date);
        map.put("year", Integer.parseInt(year));
        map.put("month", Integer.parseInt(month));
        map.put("day", Integer.parseInt(day));
        map.put("hours", Integer.parseInt(hours));
        map.put("minytes", Integer.parseInt(minytes));
        map.put("seconds", Integer.parseInt(seconds));
        return map;
    }

    /**
     * 检查日期格式是否合法
     *
     * @param
     * @return boolean
     */
    public static String parseDate(String string, String format1) {
        if (string == null) {
            return null;
        }
        try {
            String format = DateUtils.format(org.apache.commons.lang3.time.DateUtils.parseDate(string, parsePatterns), format1);
            return format;
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 时间字符串正序排序
     *
     * @param
     * @return
     */
    public static List<String> timeListSort(List<String> timeList, String format) {
        List<String> collect = timeList.stream().sorted(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                try {
                    Date d1 = DateUtils.stringToDate(o1, format);
                    Date d2 = DateUtils.stringToDate(o2, format);
                    return d1.compareTo(d2);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return 0;
            }
        }).collect(Collectors.toList());
        return collect;
    }

}

  

posted @ 2021-08-02 14:56  Lz_LittleMonster  阅读(123)  评论(0)    收藏  举报