比较两个字符串类型日期的相差天数

public static float comparePastDate(String oldDate,String nowDate) throws ParseException{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();

        Date old = sdf.parse(oldDate);
        calendar.setTime(old);
        Long oTime = calendar.getTimeInMillis();

        Date now = sdf.parse(nowDate);
        calendar.setTime(now);
        Long nTime = calendar.getTimeInMillis();
        return (nTime - oTime)/(3600F * 1000 * 24);
    }

你可能感兴趣的:(Java)