Haha, it’s a date problem again, then I definitely recommend you to use the new time API of Java 8, and your time string is still in this formatyyyy-MM-dd,直接LocalDate.parseThe method can convert the string into a LocalDate object
Furthermore, if this involves a series of regular time collections, you must consider Stream. It is very convenient and fast to use Stream to construct your collection. The following is the sample code:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date begin = sdf.parse("2016-12-11");
Date end = sdf.parse("2016-12-20");
List<String> l = new ArrayList<String>();
Calendar cal = Calendar.getInstance();
while (begin.before(end)) {
l.add(sdf.format(begin));
cal.setTime(begin);
cal.add(Calendar.DAY_OF_MONTH, 1);
begin = cal.getTime();
}
for(String s:l){
System.out.println(s);
}
} catch (ParseException e) {
e.printStackTrace();
}
You need Apache’s lang package, which has the following API
static Date addDays(Date date, int amount) Returns a new Date object after adding amount days to a date time object static Date addHours(Date date, int amount) Returns a new Date object after adding amount h to a date time object static Date addMilliseconds(Date date, int amount) Returns a new Date object after adding amount milliseconds to a date time object static Date addMinutes(Date date, int amount) Returns a new Date object after adding amount minutes to a date time object static Date addMonths(Date date, int amount) Returns a new Date object after adding amount months to a date time object static Date addSeconds(Date date, int amount) Returns a new Date object after adding amount seconds to a date time object static Date addWeeks(Date date, int amount) Returns a new Date object after adding amount weeks to a date time object static Date addYears(Date date, int amount) Returns a new Date object after adding amount years to a date time object
Haha, it’s a date problem again, then I definitely recommend you to use the new time API of Java 8, and your time string is still in this format
yyyy-MM-dd
,直接LocalDate.parse
The method can convert the string into a LocalDate objectFurthermore, if this involves a series of regular time collections, you must consider Stream. It is very convenient and fast to use Stream to construct your collection. The following is the sample code:
Then the test code:
The following is the print result:
Easy to use~~Perfectly elegant and easy to understand Java8~Haha
You calculate the number of days difference between two dates and add 1 in the loop. If the added date is equal to the later date, it will be fine
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
You need Apache’s lang package, which has the following API
static Date addDays(Date date, int amount) Returns a new Date object after adding amount days to a date time object
static Date addHours(Date date, int amount) Returns a new Date object after adding amount h to a date time object
static Date addMilliseconds(Date date, int amount) Returns a new Date object after adding amount milliseconds to a date time object
static Date addMinutes(Date date, int amount) Returns a new Date object after adding amount minutes to a date time object
static Date addMonths(Date date, int amount) Returns a new Date object after adding amount months to a date time object
static Date addSeconds(Date date, int amount) Returns a new Date object after adding amount seconds to a date time object
static Date addWeeks(Date date, int amount) Returns a new Date object after adding amount weeks to a date time object
static Date addYears(Date date, int amount) Returns a new Date object after adding amount years to a date time object
Article address