JAVA日期处理

📢博客主页:折戏花

📢欢迎点赞 👍 收藏 ⭐留言 📝 如有错误敬请指正!

📢本文由折戏花编写,首发于CSDN🙉

平时开发时避免不了对日期的各种处理,这里小编就记录一下平时我们比较常用到的日期处理方式
以下工具类中所用java.util包下的Date类,工具类继承org.apache.commons.lang3.time.DateUtils类

1、Date类型转String类型

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

2、String类型转Date类型

 public static final Date dateTime(final String format, final String ts) {
        try {
            if(StringUtil.isNotBlank(ts)) {
                return new SimpleDateFormat(format).parse(ts);
            }else{
                return null;
            }
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

3、获取当前日期

 /**
     * 获取当前Date型日期
     *
     * @return Date() 当前日期
     */
    public static Date getNowDate() {
        return new Date();
    }
    /**
     * 获取当前String型日期
     * format 日期格式
     * @return String 当前日期
     */
 	public static final String dateTimeNow(final String format) {
        return parseDateToStr(format, new Date());
    }
	/**
     * 获取当前日期
     * @return yyyy-mm-dd
     * */
    public static String getCurrentDay() {
        return DateFormatUtils.format(new Date(),"yyyy-MM-dd");
    }

4、获取当月第一天(利用Calendar类)

  /**
     * 获取当前月第一天
     * @return yyyy-mm-dd
     * */
    public static String getCurrentMonthFirstDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return DateFormatUtils.format(calendar.getTime(), "yyyy-MM-dd");
    }

5、获取当前日期前多少天的日期(利用Calendar类)

 /**
     * 获取当前日期过去 pastDay 天
     * @param pastDay 过去多少天
     * @return yyyy-mm-dd
     * */
    public static Date getPastDayDate(int pastDay) {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DAY_OF_YEAR, 1 - pastDay);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }
	/**
     * 获取当前日期过去 pastDay 天
     * @param pastDay 过去多少天
     * @return yyyy-mm-dd
     * */
    public static String getPastDayDateString(int pastDay) {
        return DateFormatUtils.format(getPastDayDate(pastDay), "yyyy-MM-dd");
    }

6、计算2个日期相隔天数(利用Calendar类)

/**
     * 得到两个日期的相隔多少天
     * @param date1     日期1 开始
     * @param date2     日期2 结束
     * @return int      多少天
     */
    public static int dateDiffDay(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        long diffDay = (cal2.getTimeInMillis() - cal1.getTimeInMillis())  / (1000 * 60 * 60 * 24);
        return (int) diffDay + 1;
    }

7、判断日期是否在2个时间范围内

 /**
     * 是否在两个时间之间
     * @param date 比对的时间
     * @param begin
     * @param end
     * @return boolean 在两个时间时间
     * */
    public static boolean between(Date date, Date begin, Date end) {
        if (date == null) {
            return false;
        } else if (begin == null && end == null) {
            return true;
        } else if (begin == null && end != null) {
            return date.compareTo(end) <= 0;
        } else if (begin != null && end == null) {
            return begin.compareTo(date) <= 0;
        }
        return begin.compareTo(date) <= 0 &&  date.compareTo(end) <= 0;
    }

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

折戏花

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

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

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

打赏作者

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

抵扣说明:

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

余额充值