All the existing answers are great. This answer provides some additional information which can also solve your problem or at least lead you in the right direction.
Using the modern API:
import java.time.LocalDate;
class Main {
public static void main(String[] args) {
LocalDate startDate = LocalDate.of(2020, 12, 5);// 5th Dec 2020
LocalDate endDate = LocalDate.of(2020, 12, 15);// 15th Dec 2020
for (LocalDate date = startDate; !date.isAfter(endDate); date = date.plusDays(1)) {
System.out.println(date);
}
}
}
Output:
2020-12-05
2020-12-06
2020-12-07
2020-12-08
2020-12-09
2020-12-10
If you have dates as string objects:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
class Main {
public static void main(String[] args) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("d/M/uuuu");
LocalDate startDate = LocalDate.parse("5/12/2020", dtf);// 5th Dec 2020
LocalDate endDate = LocalDate.parse("10/12/2020", dtf);// 10th Dec 2020
for (LocalDate date = startDate; !date.isAfter(endDate); date = date.plusDays(1)) {
System.out.println(date);
}
}
}
Output:
2020-12-05
2020-12-06
2020-12-07
2020-12-08
2020-12-09
2020-12-10
Using the legacy API:
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
class Main {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
calendar.set(2020, 11, 5);// 5th Dec 2020
Date startDate = calendar.getTime();
calendar.set(2020, 11, 10);// 10th Dec 2020
Date endDate = calendar.getTime();
SimpleDateFormat sdfForOutput = new SimpleDateFormat("yyyy-MM-dd");
Date date = startDate;
for (calendar.setTime(startDate); !date.after(endDate); calendar.add(Calendar.DAY_OF_YEAR,
1), date = calendar.getTime()) {
System.out.println(sdfForOutput.format(date));
}
}
}
Output:
2020-12-05
2020-12-06
2020-12-07
2020-12-08
2020-12-09
2020-12-10
If you have dates as string objects:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
class Main {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdfForParsing = new SimpleDateFormat("d/M/yyyy");
Date startDate = sdfForParsing.parse("5/12/2020");// 5th Dec 2020
Date endDate = sdfForParsing.parse("10/12/2020");// 10th Dec 2020
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdfForOutput = new SimpleDateFormat("yyyy-MM-dd");
Date date = startDate;
for (calendar.setTime(startDate); !date.after(endDate); calendar.add(Calendar.DAY_OF_YEAR,
1), date = calendar.getTime()) {
System.out.println(sdfForOutput.format(date));
}
}
}
Output:
2020-12-05
2020-12-06
2020-12-07
2020-12-08
2020-12-09
2020-12-10
Note that the date-time API of java.util
and their formatting API, SimpleDateFormat
are outdated and error-prone. It is recommended to stop using them completely and switch to the modern date-time API. Learn more about the modern date-time API at Trail: Date Time.
Note: For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7.
If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.