java自定义工具类编写

java自定义工具类编写

得到一天的最后时刻

public static Date getDateOneDayEnd(Date date) throws ParseException {
   
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        Date date1 = calendar.getTime();
        return date1;
    }

得到一天的开始时刻

    public static Date getOneDatefirst(Date date) throws ParseException {
   
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date date1 = calendar.getTime();
        return date1;
    }

得到明天

    public static Date addOnyDay(Date date) throws ParseException {
   
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + 1);
        Date date1 = calendar.getTime();
        return date1;
    }

得到昨天

 public static Date reduceOnyDay(Date date) throws ParseException {
   
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) - 1);
        Date date1 = calendar.getTime();
        return date1;
    }

得到两个时间的间隔天数

 public static long betweenTwoDays(Date date1, Date date2) {
   
        long a1 = date1.getTime();
        long a2 = date2.getTime();
        long a3 = a2 - a1;
        a3 = a3 / (3600 * 24 * 1000);
        return a3;
    }

解析日期字符串

	public static final SimpleDateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss");
    public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
    public static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HHmmss");
 /**
     * @param: String 
     * @return: java.util.Date
     */
    public static Date parseDateTime(String dateTime) {
   
        try {
   
            return DATE_TIME_FORMAT.parse(dateTime);
        } catch (ParseException e) {
   
            e.printStackTrace();
        }
        return null;
    }


用java8封装的新类实现

 /**
     *
     * @param localDateTime
     * @
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

菜鸟程序员李老板专业码代码三十年

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

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

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

打赏作者

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

抵扣说明:

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

余额充值