欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java计算两个日期相差的天数

程序员文章站 2022-07-12 17:49:36
...
    /**
     * 计算两个日期相差的天数
     * @param oldDate
     * @param newDate
     * @return
     * @author WangLiping
     */
    public static int comparePastDate(String oldDate,String nowDate){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        Date old;
        int day=0;
        try {
            old = sdf.parse(oldDate);
            calendar.setTime(old);
            Long oTime = calendar.getTimeInMillis();

            Date now = sdf.parse(nowDate);
            calendar.setTime(now);
            Long nTime = calendar.getTimeInMillis();

            day=(int) ((nTime - oTime)/(3600F * 1000 * 24));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return day;
    }