java比较两个日期间隔多长时间

124 阅读1分钟
public static void main(String[] args) throws ParseException {
    String startTime = "2020-12-10 16:22:53";
    compateToTime(startTime);
}

private static void compateToTime(String startTime) throws ParseException {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    Date startDate = simpleDateFormat.parse(startTime);
    Date endDate = new Date();
    long l = endDate.getTime() - startDate.getTime();
    long minutes = l / 1000 /60;
    long day = l / 1000 /60 /60 /24;
    if (day > 30) {

    }
}