fwq
2023-02-03 9134911f3185df2ed3668225a21aa1056abbcbf4
src/main/java/com/hx/util/DateUtil.java
@@ -572,8 +572,7 @@
    /**
     * 获取当前月的结束时间
     *
     * @param date 0拿取当月,正代表后,负代表前,值为几个(月)
     * @param date 点前时间
     * @return 返回时间 格式yyyy-MM-dd 23:59:59
     */
    public static Date getMonthEnd(Date date) {
@@ -588,7 +587,6 @@
    /**
     * 获取当前月的结束时间
     *
     * @param num 0拿取当月,正代表后,负代表前,值为几个(月)
     * @return 返回时间 格式yyyy-MM-dd 23:59:59
     */
@@ -603,31 +601,87 @@
        return sdf.format(calendar.getTime());
    }
    /**
     * 获取当前年的开始时间
     * @param time 时间
     * @return 返回时间 格式yyyy-MM-dd 00:00:00
     */
    public static Date getYearStart(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.set(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }
    /**
     * 获取当前年的结束时间
     * @param time 时间
     * @return 返回时间 格式yyyy-MM-dd 00:00:00
     */
    public static Date getYearEnd(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.set(Calendar.MONTH, 11);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DATE));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }
    /**这天的开始时间
     * 日期2000-01-01变2000-01-01 00:00:00
     */
    public static String dayToStart(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        return sdf.format(calendar.getTime());
    }
    /**
    /**这天的最后时间
     * 日期2000-01-01变2000-01-01 23:59:59
     */
    public static String dayToEnd(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR, 23);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        return sdf.format(calendar.getTime());
    }
    /**这天的开始时间
     * 日期2000-01-01变2000-01-01 00:00:00
     */
    public static Date dayToStartDate(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);
        return calendar.getTime();
    }
    /**这天的最后时间
     * 日期2000-01-01变2000-01-01 23:59:59
     */
    public static Date dayToEndDate(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);
        return calendar.getTime();
    }
    /**获取月份的天数
@@ -671,4 +725,59 @@
        calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }
    /**当天多少点前
     * @param date 时间
     * @param num 多少点前,不含当点前,24小时制
     * @return 月份的天数
     */
    public static boolean getFrontMinute(Date date,int num) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, num-1);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return date.compareTo(cal.getTime()) < 1;
    }
    /**
     * 获得某个日期的当天某点
     * 例如:2022-12-26 11:20:00 -> 2022-12-26 13:00:00
     * @param num 24小时
     */
    public static String getHourDayTime(Date date,int num) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, num);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        return sdf.format(calendar.getTime());
    }
    /**
     * 获取时间当月剩余天数
     * */
    public static Integer getMonthSurplus(Date date) {
        Calendar month = Calendar.getInstance();
        month.setTime(new Date());
        month.set(Calendar.DATE, 1);
        month.roll(Calendar.DATE, -1);
        return month.getActualMaximum(Calendar.DATE) - Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    }
    /**获取当前时间所在周的周一00:00:00*/
    public static Date getMonday(Date date) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setTime(date);
        //以周一为首日
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        //周一
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return calendar.getTime();
    }
}