Home >Java >javaTutorial >How to Retrieve a Date Range in Java?
Retrieving a Date Range in Java
One may encounter a situation where a list of dates between two specified dates is required. This range can include both the start and end dates.
Java 8 java.time Package
If using Java 8, the Java Time package provides an elegant solution based on the Joda-Time API:
Example:
String startDate = "2014-05-01"; String endDate = "2014-05-10"; LocalDate start = LocalDate.parse(startDate); LocalDate end = LocalDate.parse(endDate); List<LocalDate> totalDates = new ArrayList<>(); while (!start.isAfter(end)) { totalDates.add(start); start = start.plusDays(1); }
The above is the detailed content of How to Retrieve a Date Range in Java?. For more information, please follow other related articles on the PHP Chinese website!