Bael 924 Updated code based on review from editor (#2020)

* BAEL-924: How to get all LocalDates between two dates?

* BAEL-924: Updated code based on review from editor

* BAEL-924: Updated code based on review from editor
This commit is contained in:
Syed Ali Raza 2017-06-09 01:06:04 +05:00 committed by Grzegorz Piwowarek
parent 41c35cea8b
commit 5b785b3ad2
2 changed files with 11 additions and 20 deletions

View File

@ -8,7 +8,7 @@ import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.IntStream;
public class TimeApi {
@ -29,12 +29,11 @@ public class TimeApi {
}
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;
return IntStream.iterate(0, i -> i + 1)
.limit(numOfDaysBetween)
.mapToObj(i -> startDate.plusDays(i))
.collect(Collectors.toList());
}
public static List<LocalDate> getDatesBetweenUsingJava9(LocalDate startDate, LocalDate endDate) {

View File

@ -14,11 +14,11 @@ public class TimeApiTest {
public void givenGetDatesBetweenWithUsingJava7_WhenStartEndDate_thenDatesList() {
Date startDate = Calendar.getInstance().getTime();
Calendar endCalendar = Calendar.getInstance();
endCalendar.add(Calendar.DATE, 3);
endCalendar.add(Calendar.DATE, 2);
Date endDate = endCalendar.getTime();
List<Date> dates = TimeApi.getDatesBetweenUsingJava7(startDate, endDate);
assertEquals(dates.size(), 3);
assertEquals(dates.size(), 2);
Calendar calendar = Calendar.getInstance();
Date date1 = calendar.getTime();
@ -31,36 +31,28 @@ public class TimeApiTest {
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);
LocalDate endDate = LocalDate.now().plusDays(2);
List<LocalDate> dates = TimeApi.getDatesBetweenUsingJava8(startDate, endDate);
assertEquals(dates.size(), 3);
assertEquals(dates.size(), 2);
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);
LocalDate endDate = LocalDate.now().plusDays(2);
List<LocalDate> dates = TimeApi.getDatesBetweenUsingJava9(startDate, endDate);
assertEquals(dates.size(), 3);
assertEquals(dates.size(), 2);
assertEquals(dates.get(0), LocalDate.now());
assertEquals(dates.get(1), LocalDate.now().plusDays(1));
assertEquals(dates.get(2), LocalDate.now().plusDays(2));
}
}