常用DateUtils工具类

自己工作中常用的 Date 工具类,记录下。

package com.gh.common.utils;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

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

/**
 * 类描述:
 *
 * @ClassName DateUtils
 * @Description TODO
 * @Author zla
 * @Date 2022/9/22 19:38
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {

    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    /**
     * 获取昨天当前时间的整点时间
     * 例如: currentTime="2000-12-12 12:12:12" return "2000-12-11 12:00:00"
     *
     * @param currentTime
     * @return
     */
    public static Date getYestodayOnHour(Date currentTime) {
        long time = currentTime.getTime();
        long yestodayTime = time - 1000 * 60 * 60 * 24;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date yesterday = null;
        try {
            yesterday = sdf.parse(sdf.format(new Date(yestodayTime)));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return yesterday;
    }

    /**
     * 获取本周的第一天的日期(周一是第一天)
     *
     * @return String
     **/
    public static String getWeekStart() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.WEEK_OF_MONTH, 0);
        cal.set(Calendar.DAY_OF_WEEK, 2);
        Date time = cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time) + " 00:00:00";
    }

    public static final String parseDateToStr(final String format, final Date date) {
        return new SimpleDateFormat(format).format(date);
    }

    /**
     * 获取往前n天日期
     *
     * @return
     */
    public static String getBeforeDay(int num) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -num);
        return DateUtil.format(calendar.getTime(), "yyyy-MM-dd");
    }

    /**
     * 比较三个日期时间
     *
     * @return
     * @Date 2022/12/7 11:12
     * @Param [date1, date2, date3]
     **/
    public static String compareDateTime(String date1, String date2, String date3) {
        String lastReportTime = compareDateTime(date1, date2);

        return compareDateTime(lastReportTime, date3);
    }

    /**
     * 比较两个日期时间
     *
     * @return
     * @Date 2022/12/7 11:12
     * @Param
     **/
    public static String compareDateTime(String date1, String date2) {
        String lastReportTime = null;
        if (Objects.nonNull(date1) && !"".equals(date1) && Objects.nonNull(date2) && !"".equals(date2)) {
            lastReportTime = date1;
            int compareTo = lastReportTime.compareTo(date2);
            if (compareTo < 0) {
                lastReportTime = date2;
            }
            return lastReportTime;
        } else if (Objects.isNull(date1) || "".equals(date1)) {
            lastReportTime = date2;
        } else if (Objects.isNull(date2) || "".equals(date2)) {
            lastReportTime = date1;
        }
        return lastReportTime;
    }

    /**
     * 获取该月的天数
     *
     * @return
     * @Date 2022/12/27 15:07
     * @Param [date]
     **/
    public static int getDaysOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    /**
     * 获取上个月的日期时间
     *
     * @return
     * @Date 2022/12/27 15:07
     * @Param []
     **/
    public static Date getBeforeMonth() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.MONTH, -1);
        return c.getTime();
    }

    /**
     * 获取当天0点时间
     *
     * @return
     * @Date 2022/12/27 16:34
     * @Param []
     **/
    public static String getTodayTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String time = sdf.format(new Date());
        return time + " 00:00:00";
    }

    /**
     *  获取今天的时间(字符串格式,yyyy-MM-dd)
     * 
     * @Date 2024/3/14 17:14
     * @Param []
     * @return 
     **/
    public static String getTodayStr(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(new Date());
    }

    /**
     *  查询今日 0 点 (Date格式)
     * 
     * @Date 2024/3/14 17:18
     * @Param []
     * @return 
     **/
    public static Date getTodayDateStart(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(new Date()) + " 00:00:00" ;
        return parseDate(date);
    }

    /**
     *  查询今日 23:59:59 点 (Date格式)
     *
     * @Date 2024/3/14 17:19
     * @Param []
     * @return
     **/
    public static Date getTodayDateEnd(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(new Date()) + " 23:59:59" ;
        return parseDate(date);
    }

    /**
     *  查询日期 00:00:00 点 (Date格式)
     *
     * @Date 2024/3/14 17:18
     * @Param []
     * @return
     **/
    public static String getDateBegin(Date date){
        return new SimpleDateFormat("yyyy-MM-dd").format(date) + " 00:00:00";
    }

    /**
     *  查询日期 23:59:59 点 (Date格式)
     *
     * @Date 2024/3/14 17:19
     * @Param []
     * @return
     **/
    public static String getDateEnd(Date date){
        return new SimpleDateFormat("yyyy-MM-dd").format(date) + " 23:59:59";
    }

    /**
     * 获取昨天开始时间(0点)
     *
     * @return
     * @Date 2022/12/27 16:45
     * @Param []
     **/
    public static String getYesterdayStartTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DATE, -1);
        String time = sdf.format(c.getTime());
        return time + " 00:00:00";
    }

    /**
     * 获取昨天结束时间(23点59分59秒)
     *
     * @return
     * @Date 2022/12/27 16:45
     * @Param []
     **/
    public static String getYesterdayEndTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DATE, -1);
        String time = sdf.format(c.getTime());
        return time + " 23:59:59";
    }

    /**
     * 获取昨天结束时间(23点59分59秒)
     *
     * @return
     * @Date 2022/12/27 16:45
     * @Param []
     **/
    public static Date getYesterdayEndTime(Date currentTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(currentTime);
        c.add(Calendar.DATE, -1);
        String time = sdf.format(c.getTime()) + " 23:59:59";
        return parseDate(time);
    }

    /**
     * 获取当月1日0点时间
     *
     * @return
     * @Date 2022/12/27 16:34
     * @Param []
     **/
    public static String getCurrentMonthTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String time = sdf.format(new Date());
        return time + "-01 00:00:00";
    }

    /**
     * 获取上个月1日开始时间(0点)
     *
     * @return
     * @Date 2022/12/27 16:45
     * @Param []
     **/
    public static String getBeforeMonthStartTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        //将小时至00
        calendar.set(Calendar.HOUR_OF_DAY, 00);
        //将分钟至00
        calendar.set(Calendar.MINUTE, 00);
        //将秒至00
        calendar.set(Calendar.SECOND, 00);

        return sdf.format(calendar.getTime());
    }

    /**
     * 获取上个月最后一日结束时间(23点59分59秒)
     *
     * @return
     * @Date 2022/12/27 16:45
     * @Param []
     **/
    public static String getBeforeMonthEndTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        int month = calendar.get(Calendar.MONTH);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        //将小时至23
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        //将分钟至59
        calendar.set(Calendar.MINUTE, 59);
        //将秒至59
        calendar.set(Calendar.SECOND, 59);
        return sdf.format(calendar.getTime());
    }

    /**
     * 获取当年1月1日0点时间
     *
     * @return
     * @Date 2022/12/27 16:38
     * @Param []
     **/
    public static String getCurrentYearTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        String time = sdf.format(new Date());
        return time + "-01-01 00:00:00";
    }

    public static String getLastYearStartTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.YEAR, -1);
        String time = sdf.format(calendar.getTime());
        return time + "-01-01 00:00:00";
    }

    public static String getLastYearEndTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.YEAR, -1);
        String time = sdf.format(calendar.getTime());
        return time + "-12-31 23:59:59";
    }

    public static boolean isSameDay(String time1, String time2) {
        DateTime begin = DateUtil.parse(time1);
        DateTime end = DateUtil.parse(time2);
        boolean is = DateUtil.isSameDay(begin, end);
        return is;
    }

    /**
     * 获取当前时间(yyyyMMddHHmmss格式)
     *
     * @return
     * @Date 2022/12/27 16:38
     * @Param []
     **/
    public static String getCurrentTimeStr() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String time = sdf.format(new Date());
        return time;
    }

    /**
     * 获取当前日期(yyyyMMdd格式)
     *
     * @return
     * @Date 2022/12/27 16:38
     * @Param []
     **/
    public static String getCurrentDateStr() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String Date = sdf.format(new Date());
        return Date;
    }

    /**
     * 两个日期间间隔列表
     *
     * @param startDate
     * @param endDate
     * @param interval
     * @return
     */
    public static List getDateList(String startDate, String endDate, int interval) {
        List dataList = new ArrayList();

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar start = Calendar.getInstance();
        Calendar end = Calendar.getInstance();
        try {
            start.setTime(format.parse(startDate));
            end.setTime(format.parse(endDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        while (end.after(start)) {
            dataList.add(format.format(start.getTime()));
            start.add(Calendar.DAY_OF_MONTH, interval);
        }
        return dataList;
    }

    /**
     * 获取近30天的日期(返回 String 类型)
     *
     * @return
     * @Date 2023/3/9 13:31
     * @Param []
     **/
    public static List<String> getRecentThreeDayDateList() {
        Calendar c = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<String> lsDate = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            c.setTime(new Date());
            c.add(Calendar.DATE, -i);
            Date d = c.getTime();
            lsDate.add(sdf.format(d));
            Collections.sort(lsDate);
        }
        return lsDate;
    }

    /**
     * 获取几分钟前的时间
     *
     * @param currentTime
     * @param minutes
     * @return
     */
    public static String getBeforeMinuteTime(Date currentTime, int minutes, String format) {
        Long now = currentTime.getTime();
        Long before = now - 1000 * 60 * minutes;
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String beforeTime = sdf.format(new Date(before));
        return beforeTime;
    }

    /**
     * 获取几分钟前的时间
     *
     * @param currentTime
     * @param minutes
     * @return
     */
    public static String getAfterMinuteTime(Date currentTime, int minutes, String format) {
        Long now = currentTime.getTime();
        Long after = now + 1000 * 60 * minutes;
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String beforeTime = sdf.format(new Date(after));
        return beforeTime;
    }

    /**
     * 获取n(小时/天)后的时间(减去一秒)
     *
     * @param currentTime
     * @param interval
     * @param type 1-天,2-小时
     * @param reduceFlag 减少标志(true-需要减去一秒)
     * @return
     */
    public static Date getAfterIntervalTime(Date currentTime, int interval, int type, boolean reduceFlag) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentTime);
        if (type == 1) {
            calendar.add(Calendar.DATE, interval);
        } else {
            calendar.add(Calendar.HOUR, interval);
        }
        if (reduceFlag) {
            calendar.add(Calendar.SECOND, -1);
        }
        return calendar.getTime();
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        }
        try {
            return parseDate(str.toString(), parsePatterns);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 获取最近的n天日期集合,包括今天
     * @return
     */
    public static List<String> getRecentDateList(int n){
        Calendar c=Calendar.getInstance();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        List<String> lsDate=new ArrayList<>();
        for(int i=0;i<n;i++){
            c.setTime(new Date());
            c.add(Calendar.DATE,-i);
            Date d=c.getTime();
            lsDate.add(sdf.format(d));
            Collections.sort(lsDate);
        }
        return lsDate ;
    }


    public static void main(String[] args) throws Exception{
        System.out.println(getAfterMinuteTime(new Date(),120,"yyyy/MM/dd HH:mm:ss"));
        String dateStr = "2024-01-15 08:00:00";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sdf.parse(dateStr);
        System.out.println(sdf.format(getAfterIntervalTime(date,1, 2, true)));
        System.out.println(sdf.format(getAfterIntervalTime(date,1,2, false)));
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我只会发热

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值