DateUtil工具类记录

为支持日常工作需要,记录所用到的一些关于时间的工具类方法。随时进行补充。

/**
    * Description:获取两个日期之间的天数差
    * @Author:hanyq
    * @Date:2023/9/19 11:23
    */
    public static int getDateDifference(Date startDate,Date endDate){
        int days = 0;
        try {
            Calendar startCalendar = Calendar.getInstance();
            startCalendar.setTime(startDate);
            Calendar endCalendar = Calendar.getInstance();
            endCalendar.setTime(endDate);
            //计算两个日期相差的天数
            //startCalendar.getTime().getTime()返回long毫秒数形式,毫秒转为秒所以除以1000
            //1天=24小时,1小时=60分,1分=60秒,所以两个时间的差再除以60 * 60 * 24换算成天的形式
            days = ((int) (startCalendar.getTime().getTime() / 1000) - (int) (endCalendar.getTime().getTime() / 1000)) / (60 * 60 * 24);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return days;
    }

    /**
     * 比较两个时间大小(当时间A大于等于时间B时会返回false;反之时间A小于时间B时会返回true)
     * @param startDate 时间A
     * @param endDate   时间B
     * @return
     */
    public static Boolean compareDate(Date startDate,Date endDate){
        Boolean result = false;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        try {
            result = startDate.after(endDate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 获取当前日期对应的一周的开始日期
     * @param date
     * @return
     */
    public static Calendar getFirstDayOfWeek(Date date){
        Calendar result = null;
        try{
            Calendar calendarIndex = Calendar.getInstance();
            calendarIndex.setTime(date);
            //先判断该日期是否是周日,如果是的话将其减一(西方周六为一周结束,而我们以周日为一周结束)
            LocalDate weekHandle = LocalDate.of(calendarIndex.get(Calendar.YEAR), calendarIndex.get(Calendar.MONTH) + 1, calendarIndex.get(Calendar.DATE));
            int week = weekHandle.getDayOfWeek().getValue();

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            if (week == 7){
                calendar.add(Calendar.DAY_OF_MONTH, -1);
            }
            calendar.set(Calendar.DAY_OF_WEEK, calendar.getActualMinimum(Calendar.DAY_OF_WEEK));
            calendar.add(Calendar.DAY_OF_MONTH, 1);

            result = calendar;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            return result;
        }
    }

    /**
     * 获取当前日期对应的一周的结束日期
     * @param date
     * @return
     */
    public static Calendar getLastDayOfWeek(Date date){
        Calendar result = null;
        try{
            Calendar calendarIndex = Calendar.getInstance();
            calendarIndex.setTime(date);
            //先判断该日期是否是周日,如果是的话将其减一(西方周六为一周结束,而我们以周日为一周结束)
            LocalDate weekHandle = LocalDate.of(calendarIndex.get(Calendar.YEAR), calendarIndex.get(Calendar.MONTH) + 1, calendarIndex.get(Calendar.DATE));
            int week = weekHandle.getDayOfWeek().getValue();

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            if (week == 7){
                calendar.add(Calendar.DAY_OF_MONTH, -1);
            }
            calendar.set(Calendar.DAY_OF_WEEK, calendar.getActualMaximum(Calendar.DAY_OF_WEEK));
            calendar.add(Calendar.DAY_OF_MONTH, 1);

            result = calendar;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            return result;
        }
    }

    /**
    * Description:获取当时间所属月份开始时间
    * @Author:hanyq
    * @Date:2023/10/17 17:39
    */
    public static Calendar getFirstDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return calendar;
    }

    /**
     * Description:获取当时间所属月份结束时间
     * @Author:hanyq
     * @Date:2023/10/17 17:39
     */
    public static Calendar getLastDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return calendar;
    }

    /**
     * Description:获取当时间所属年份开始时间
     * @Author:hanyq
     * @Date:2023/10/17 17:50
     */
    public static Calendar getFirstDayOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_YEAR, 1);
        return calendar;
    }

    /**
     * Description:获取当时间所属年份结束时间
     * @Author:hanyq
     * @Date:2023/10/17 17:50
     */
    public static Calendar getLastDayOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_YEAR, calendar.getActualMaximum(Calendar.DAY_OF_YEAR));
        return calendar;
    }

你可能感兴趣的:(java)