private long dateCompare(Date firstDt,Date secentDt){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String firstDtStr = sdf.format(firstDt); String secentDtStr = sdf.format(secentDt); LocalDate localDate1 = LocalDate.parse(firstDtStr); LocalDate localDate2 = LocalDate.parse(secentDtStr); return localDate2.until(localDate1, ChronoUnit.DAYS); }
ChronoUnit参数可以指定天,月,年。
时间差计算很方便