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