1. 程式人生 > >計算一個日期到今天相差的天數

計算一個日期到今天相差的天數

Date enrollDate = inter.getEnrollDate();     //可以自己創個時間節點
Date today = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
if (enrollDate != null) {
    String end = sdf.format(enrollDate);
    String time = sdf.format(today);
    try {
        enrollDate = sdf.parse(end);
        today = sdf.parse(time);
    } catch (ParseException e) {
        e.printStackTrace();
    }
}

long enrollDays = (today.getTime() - enrollDate.getTime()) / (24 * 60 * 60 * 1000);