java獲取兩個日期之間的所有日期


java獲取兩個日期之間的所有日期

 

 

 

解決方法:

1.核心方法

private List<String> getBetweenDates(String start, String end) {

List<String> result = new ArrayList<String>();

try {

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    Date start_date = sdf.parse(start);

    Date end_date = sdf.parse(end);

   Calendar tempStart = Calendar.getInstance();

   tempStart.setTime(start_date);

   Calendar tempEnd = Calendar.getInstance();

   tempEnd.setTime(end_date);

   while (tempStart.before(tempEnd)||tempStart.equals(tempEnd)) {

       result.add(sdf.format(tempStart.getTime()));

       tempStart.add(Calendar.DAY_OF_YEAR, 1);

   }

} catch (ParseException e) {

e.printStackTrace();

}

Collections.reverse(result);

   return result;

}

 

2.使用方法

getBetweenDates("2018-12-26","2018-01-02")


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM