From f9dc97d25e7888a03394cf2a5237eb8fb0a20318 Mon Sep 17 00:00:00 2001 From: Haroon Khan Date: Fri, 13 Aug 2021 18:39:14 +0100 Subject: [PATCH] [JAVA-6221] Fix TimeApi unit test --- .../java/com/baeldung/java9/time/TimeApi.java | 20 +++++++++++++------ .../baeldung/java9/time/TimeApiUnitTest.java | 14 ++++++------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/core-java-modules/core-java-date-operations-1/src/main/java/com/baeldung/java9/time/TimeApi.java b/core-java-modules/core-java-date-operations-1/src/main/java/com/baeldung/java9/time/TimeApi.java index ee4e35a77b..dee3135391 100644 --- a/core-java-modules/core-java-date-operations-1/src/main/java/com/baeldung/java9/time/TimeApi.java +++ b/core-java-modules/core-java-date-operations-1/src/main/java/com/baeldung/java9/time/TimeApi.java @@ -13,12 +13,9 @@ import java.util.stream.IntStream; public class TimeApi { public static List getDatesBetweenUsingJava7(Date startDate, Date endDate) { - List datesInRange = new ArrayList(); - Calendar calendar = new GregorianCalendar(); - calendar.setTime(startDate); - - Calendar endCalendar = new GregorianCalendar(); - endCalendar.setTime(endDate); + List datesInRange = new ArrayList<>(); + Calendar calendar = getCalendarWithoutTime(startDate); + Calendar endCalendar = getCalendarWithoutTime(endDate); while (calendar.before(endCalendar)) { Date result = calendar.getTime(); @@ -40,4 +37,15 @@ public class TimeApi { return startDate.datesUntil(endDate).collect(Collectors.toList()); } + private static Calendar getCalendarWithoutTime(Date date) { + Calendar calendar = new GregorianCalendar(); + calendar.setTime(date); + calendar.set(Calendar.HOUR, 0); + calendar.set(Calendar.HOUR_OF_DAY, 0); + calendar.set(Calendar.MINUTE, 0); + calendar.set(Calendar.SECOND, 0); + calendar.set(Calendar.MILLISECOND, 0); + return calendar; + } + } diff --git a/core-java-modules/core-java-date-operations-1/src/test/java/com/baeldung/java9/time/TimeApiUnitTest.java b/core-java-modules/core-java-date-operations-1/src/test/java/com/baeldung/java9/time/TimeApiUnitTest.java index 8813870c2b..c4e150c757 100644 --- a/core-java-modules/core-java-date-operations-1/src/test/java/com/baeldung/java9/time/TimeApiUnitTest.java +++ b/core-java-modules/core-java-date-operations-1/src/test/java/com/baeldung/java9/time/TimeApiUnitTest.java @@ -18,19 +18,19 @@ public class TimeApiUnitTest { Date endDate = endCalendar.getTime(); List dates = TimeApi.getDatesBetweenUsingJava7(startDate, endDate); - assertEquals(dates.size(), 2); + assertEquals(2, dates.size()); 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()); + assertEquals(date1.getDay(), dates.get(0).getDay()); + assertEquals(date1.getMonth(), dates.get(0).getMonth()); + assertEquals(date1.getYear(), dates.get(0).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()); + assertEquals(date2.getDay(), dates.get(1).getDay()); + assertEquals(date2.getMonth(), dates.get(1).getMonth()); + assertEquals(date2.getYear(), dates.get(1).getYear()); } @Test