Skip to content

Commit

Permalink
Fixed the utility to put week date to previous (instead of next) Mond…
Browse files Browse the repository at this point in the history
…ay when current day is Sunday
  • Loading branch information
vsadokhin committed Sep 30, 2018
1 parent aa29923 commit dced873
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand All @@ -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()));
}
}

0 comments on commit dced873

Please sign in to comment.