BAEL-924: How to get all LocalDates between two dates? (#2012)

This commit is contained in:
Syed Ali Raza 2017-06-08 10:47:10 +05:00 committed by Grzegorz Piwowarek
parent 4453358bfb
commit e59ecbc864
2 changed files with 110 additions and 0 deletions

View File

@ -0,0 +1,44 @@
package com.baeldung.java9.time;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class TimeApi {
public static List<Date> getDatesBetweenUsingJava7(Date startDate, Date endDate) {
List<Date> datesInRange = new ArrayList<Date>();
Calendar calendar = new GregorianCalendar();
calendar.setTime(startDate);
Calendar endCalendar = new GregorianCalendar();
endCalendar.setTime(endDate);
while (calendar.before(endCalendar)) {
Date result = calendar.getTime();
datesInRange.add(result);
calendar.add(Calendar.DATE, 1);
}
return datesInRange;
}
public static List<LocalDate> getDatesBetweenUsingJava8(LocalDate startDate, LocalDate endDate) {
List<LocalDate> datesInRange = new ArrayList<LocalDate>();
long numOfDaysBetween = ChronoUnit.DAYS.between(startDate, endDate);
Stream.iterate(0, i -> i + 1).limit(numOfDaysBetween).forEach(i -> {
datesInRange.add(startDate.plusDays(i));
});
return datesInRange;
}
public static List<LocalDate> getDatesBetweenUsingJava9(LocalDate startDate, LocalDate endDate) {
return startDate.datesUntil(endDate).collect(Collectors.toList());
}
}

View File

@ -0,0 +1,66 @@
package com.baeldung.java9.time;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class TimeApiTest {
@Test
public void givenGetDatesBetweenWithUsingJava7_WhenStartEndDate_thenDatesList() {
Date startDate = Calendar.getInstance().getTime();
Calendar endCalendar = Calendar.getInstance();
endCalendar.add(Calendar.DATE, 3);
Date endDate = endCalendar.getTime();
List<Date> dates = TimeApi.getDatesBetweenUsingJava7(startDate, endDate);
assertEquals(dates.size(), 3);
Calendar calendar = Calendar.getInstance();
Date date1 = calendar.getTime();
assertEquals(dates.get(0).getDay(), date1.getDay());
assertEquals(dates.get(0).getMonth(), date1.getMonth());
assertEquals(dates.get(0).getYear(), date1.getYear());
calendar.add(Calendar.DATE, 1);
Date date2 = calendar.getTime();
assertEquals(dates.get(1).getDay(), date2.getDay());
assertEquals(dates.get(1).getMonth(), date2.getMonth());
assertEquals(dates.get(1).getYear(), date2.getYear());
calendar.add(Calendar.DATE, 1);
Date date3 = calendar.getTime();
assertEquals(dates.get(2).getDay(), date3.getDay());
assertEquals(dates.get(2).getMonth(), date3.getMonth());
assertEquals(dates.get(2).getYear(), date3.getYear());
}
@Test
public void givenGetDatesBetweenWithUsingJava8_WhenStartEndDate_thenDatesList() {
LocalDate startDate = LocalDate.now();
LocalDate endDate = LocalDate.now().plusDays(3);
List<LocalDate> dates = TimeApi.getDatesBetweenUsingJava8(startDate, endDate);
assertEquals(dates.size(), 3);
assertEquals(dates.get(0), LocalDate.now());
assertEquals(dates.get(1), LocalDate.now().plusDays(1));
assertEquals(dates.get(2), LocalDate.now().plusDays(2));
}
@Test
public void givenGetDatesBetweenWithUsingJava9_WhenStartEndDate_thenDatesList() {
LocalDate startDate = LocalDate.now();
LocalDate endDate = LocalDate.now().plusDays(3);
List<LocalDate> dates = TimeApi.getDatesBetweenUsingJava9(startDate, endDate);
assertEquals(dates.size(), 3);
assertEquals(dates.get(0), LocalDate.now());
assertEquals(dates.get(1), LocalDate.now().plusDays(1));
assertEquals(dates.get(2), LocalDate.now().plusDays(2));
}
}