From dced873b34d793ac6ffc76edee26d7a5b0d74f9f Mon Sep 17 00:00:00 2001 From: Vasiliy Sadokhin Date: Sun, 30 Sep 2018 19:56:48 +0800 Subject: [PATCH] Fixed the utility to put week date to previous (instead of next) Monday when current day is Sunday --- .../vsadokhin/iot/data/utility/DateUtility.java | 3 ++- .../iot/data/utility/DateUtilityTest.java | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/data/src/main/java/me/vsadokhin/iot/data/utility/DateUtility.java b/data/src/main/java/me/vsadokhin/iot/data/utility/DateUtility.java index 608919a..b7ce8fe 100644 --- a/data/src/main/java/me/vsadokhin/iot/data/utility/DateUtility.java +++ b/data/src/main/java/me/vsadokhin/iot/data/utility/DateUtility.java @@ -3,6 +3,7 @@ import java.time.Instant; import java.time.temporal.ChronoUnit; import java.util.Calendar; +import java.util.Locale; import java.util.TimeZone; public final class DateUtility { @@ -14,7 +15,7 @@ public static long getDayStart(long milliseconds) { } public static long getWeekStart(long milliseconds) { - Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); + Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.UK); calendar.setTimeInMillis(milliseconds); calendar.set(Calendar.MILLISECOND, 0); calendar.set(Calendar.SECOND, 0); diff --git a/data/src/test/java/me/vsadokhin/iot/data/utility/DateUtilityTest.java b/data/src/test/java/me/vsadokhin/iot/data/utility/DateUtilityTest.java index 0a6b233..a631d41 100644 --- a/data/src/test/java/me/vsadokhin/iot/data/utility/DateUtilityTest.java +++ b/data/src/test/java/me/vsadokhin/iot/data/utility/DateUtilityTest.java @@ -9,6 +9,7 @@ import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.util.Calendar; +import java.util.Date; import java.util.TimeZone; import org.junit.Test; @@ -34,7 +35,7 @@ public void getDayStart() { } @Test - public void getWeekStart() { + public void getWeekStart_checkResultIsMondayMidnight() { // setup Instant instant = LocalDate.of(2018, 9, 29).atStartOfDay().toInstant(ZoneOffset.UTC); @@ -44,4 +45,17 @@ public void getWeekStart() { // verify assertThat(result, is(ZonedDateTime.of(2018,9,24,0,0,0,0,ZoneId.of("UTC")).toInstant().toEpochMilli())); } + + @Test + public void getWeekStart_sundayCornerCase_checkWeekStartIsStillMondayMidnight() { + // setup + Instant instant = LocalDate.of(2018, 9, 30).atStartOfDay().toInstant(ZoneOffset.UTC); + + // act + long result = DateUtility.getWeekStart(instant.toEpochMilli()); + + // verify + System.out.println(new Date(result)); + assertThat(result, is(ZonedDateTime.of(2018,9,24,0,0,0,0,ZoneId.of("UTC")).toInstant().toEpochMilli())); + } } \ No newline at end of file