时间参数处理工具类

/**
 * @Author 蔡迪兴
 * @Description 时间工具类
 * @Date 2021/8/13 22:32
 */
public class DateUtil {
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final SimpleDateFormat sdfy = new SimpleDateFormat("yyyy-MM-dd");
    private static final String sdfS = "yyyy/MM/dd";

    /**
     * 获取字符串格式当前时间
     *
     * @return
     */
    public static String now() {
        Date date = new Date();
        return sdf.format(date);
    }

    /**
     * 比较两个时间大小(结束时间大于开始时间返回true,等于也返回true)
     *
     * @param beginTime 开始时间
     * @param endTime   结束时间
     * @param format    比较格式(可以比较到分,也可以比较到秒)
     * @return 返回 boolean
     * @author : 蔡迪兴
     */
    public static boolean compareToTime(String beginTime, String endTime, String format) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date bt = sdf.parse(beginTime);
        Date et = sdf.parse(endTime);
        if (bt.before(et)) {
            return true;
        } else {
            if (sdf.format(bt).equals(sdf.format(et))) {
                return true;
            } else {
                return false;
            }
        }
    }

    /**
     * 时间格式校验
     *
     * @param format 格式:例如yyyy-MM-dd HH:mm:ss或yyyy-MM-dd或HH:mm,按照自己的需求来
     * @param str    时间
     * @return 返回
     */
    public static boolean isValidDate(String format, String str) {
        //这里的时间格式根据自己需求更改(注意:格式区分大小写)
        DateFormat formatter = new SimpleDateFormat(format);
        try {
            Date date = formatter.parse(str);
            return str.equals(formatter.format(date));
        } catch (Exception e) {
            return false;
        }
    }


    @Data
    @Builder
    public static class DayCompare {
        private int year;
        private int month;
        private int day;
    }

    /**
     * 计算2个日期相差多少年
     * 列:2015-02-02  ~  2021-03-02 大约相差 6.1 年
     *
     * @param startTime
     * @param endTime
     * @return
     * @Author 蔡迪兴
     */
    public static String yearCompare(String startTime, String endTime) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(sdfS);
        Date fromDate = null;
        Date toDate = null;
        try {
            fromDate = simpleDateFormat.parse(startTime);
            toDate = simpleDateFormat.parse(endTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        DayCompare result = dayComparePrecise(fromDate, toDate);
        double month = result.getMonth();
        double year = result.getYear();
        //返回2位小数,并且四舍五入
        DecimalFormat df = new DecimalFormat("######0.0");
        return df.format(year + month / 12);
    }

    /**
     * 计算2个日期之间相差的  相差多少年月日
     * 比如:2015-02-02 到  2021-03-02 相差 6年,1个月,0天
     *
     * @param fromDate
     * @param toDate
     * @return
     */
    public static DayCompare 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;
        return DayCompare.builder().day(day).month(month).year(year).build();
    }

    /**
     * 日期转换成字符串
     *
     * @param format 格式:例如yyyy-MM-dd HH:mm:ss或yyyy-MM-dd,按照自己的需求来
     * @param date   日期
     * @return 返回String
     * @author : 蔡迪兴
     */
    public static String dateToStr(String format, Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(date);
    }

    /**
     * 字符串转换成日期
     *
     * @param format 格式:例如yyyy-MM-dd HH:mm:ss或yyyy-MM-dd,按照自己的需求来
     * @param str    日期
     * @return 返回Date
     * @author : 蔡迪兴
     */
    public static Date strToDate(String format, String str) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Date date = null;
        try {
            date = simpleDateFormat.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 获取一个时间段内一共有几天
     *
     * @param date1 开始时间
     * @param date2 结束时间
     * @return 返回
     * @author : 蔡迪兴
     */
    public static int differentDays(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);
        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if (year1 != year2)   //同一年
        {
            int timeDistance = 0;
            for (int i = year1; i < year2; i++) {
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)    //闰年
                {
                    timeDistance += 366;
                } else    //不是闰年
                {
                    timeDistance += 365;
                }
            }
            return timeDistance + (day2 - day1);
        } else    //不同年
        {
            return day2 - day1;
        }
    }

    /**
     * 获取指定时间的毫秒数
     *
     * @param time 时间
     * @return 结果
     * @throws Exception 异常
     */
    public static Long getAppointTimeMillisecond(String time) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = format.parse(time);
        return date.getTime();
    }

    /**
     * @Author: 蔡迪兴
     * @Description: 判断当前时间在时间区间内
     * @DateTime: 15:05 2021/8/20
     * @Params: [nowTime, startTime, endTime]
     * @Return boolean
     */
    public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }
/**
* @Author: 蔡迪兴
* @Description: 取给定时间的前一天
* @DateTime: 10:47 2021/8/3
* @Params: [date, days]
* @Return java.util.Date
*/
public static Date subtractDays(Date date, int days) {
    Date dateTime = new Date();
    long dif = date.getTime() - 86400 * 1000 * days;//减一天
    dateTime.setTime(dif);
    return dateTime;
} 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值