java計算兩個日期之間有多少天

/**
 * 根據第一個
 *
 * @return 第一個胎檢時間到當前時間之間的日期
 */
@SuppressLint("SimpleDateFormat")
private List<Date> calculatorHorizontalData(String dateStr) {
    String lastMus = babyDetailInfo.data.birthday;
    try {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date start = sdf.parse(lastMus);
        Date end = sdf.parse(dateStr);
        return dateSplit(start, end);
    } catch (Exception e) {
        return null;
    }
}
/**
 * 兩個日期之間的日期
 *
 * @param startDate 開始日期
 * @param endDate   結束日期
 * @return 兩個日期之間的日期的所有日期
 * @throws Exception 異常捕獲
 */
private static List<Date> dateSplit(Date startDate, Date endDate)
        throws Exception {
    Long spi = endDate.getTime() - startDate.getTime();
    Long step = spi / (24 * 60 * 60 * 1000);// 相隔天數
    List<Date> dateList = new ArrayList<>();
    dateList.add(startDate);
    for (int i = 1; i <= step; i++) {
        dateList.add(new Date(dateList.get(i - 1).getTime()
                + (24 * 60 * 60 * 1000)));// 比上一天減一
    }
    return dateList;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章