From 20829a611094df4bea614d9831e1debebc86ad31 Mon Sep 17 00:00:00 2001 From: ciphx Date: Thu, 23 Mar 2023 22:40:40 +0530 Subject: [PATCH] Getiing date of first day of week (#13694) * https://jira.baeldung.com/browse/BAEL-4630 * Revert "https://jira.baeldung.com/browse/BAEL-4630" This reverts commit a81b091678a63ed138a8e8ddb452c1c4dbf0a69b. * https://jira.baeldung.com/browse/BAEL-5874 * https://jira.baeldung.com/browse/BAEL-5874 1. changing test to use constructor instead of lombok plugin 2. reverting lombok plugin addition from pom.xml * https://jira.baeldung.com/browse/BAEL-5874 moving files to core-java-date-operations-3 --------- Co-authored-by: Deboshree --- .../java/com/baeldung/date/DayOfTheWeek.java | 41 +++++++++++ .../baeldung/date/DayOfTheWeekUnitTest.java | 68 +++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 core-java-modules/core-java-date-operations-3/src/main/java/com/baeldung/date/DayOfTheWeek.java create mode 100644 core-java-modules/core-java-date-operations-3/src/test/java/com/baeldung/date/DayOfTheWeekUnitTest.java diff --git a/core-java-modules/core-java-date-operations-3/src/main/java/com/baeldung/date/DayOfTheWeek.java b/core-java-modules/core-java-date-operations-3/src/main/java/com/baeldung/date/DayOfTheWeek.java new file mode 100644 index 0000000000..a5622038ce --- /dev/null +++ b/core-java-modules/core-java-date-operations-3/src/main/java/com/baeldung/date/DayOfTheWeek.java @@ -0,0 +1,41 @@ +package com.baeldung.date; + +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.temporal.TemporalAdjusters; +import java.time.temporal.TemporalField; +import java.time.temporal.WeekFields; +import java.util.Calendar; +import java.util.Date; +import java.util.Locale; + +public class DayOfTheWeek { + + public LocalDate getDateOfFirstDayOfTheWeek_UsingLocale(LocalDate localDate, Locale locale){ + TemporalField fieldISO = WeekFields.of(locale).dayOfWeek(); + return localDate.with(fieldISO, 1); + } + + public LocalDate getDateOfFirstDayOfTheWeek_UsingISODayOfWeek(LocalDate localDate){ + TemporalField dayOfWeek = WeekFields.ISO.dayOfWeek(); + return localDate.with(dayOfWeek, dayOfWeek.range().getMinimum()); + } + + public LocalDate getDateOfFirstDayOfTheWeek_UsingDayOfWeek(LocalDate localDate){ + DayOfWeek weekStart = DayOfWeek.MONDAY; + return localDate.with(TemporalAdjusters.previousOrSame(weekStart)); + } + + public LocalDate getDateOfFirstDayOfTheWeek_UsingCalendar(LocalDate localDate){ + Calendar calendar = Calendar.getInstance(); + ZoneId zoneId = ZoneId.systemDefault(); + Date date = Date.from(localDate.atStartOfDay(zoneId).toInstant()); + calendar.setTime(date); + while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) { + calendar.add(Calendar.DATE, -1); + } + return LocalDateTime.ofInstant(calendar.toInstant(), calendar.getTimeZone().toZoneId()).toLocalDate(); + } +} diff --git a/core-java-modules/core-java-date-operations-3/src/test/java/com/baeldung/date/DayOfTheWeekUnitTest.java b/core-java-modules/core-java-date-operations-3/src/test/java/com/baeldung/date/DayOfTheWeekUnitTest.java new file mode 100644 index 0000000000..cdc0eb1d6b --- /dev/null +++ b/core-java-modules/core-java-date-operations-3/src/test/java/com/baeldung/date/DayOfTheWeekUnitTest.java @@ -0,0 +1,68 @@ +package com.baeldung.date; + +import java.time.LocalDate; +import java.util.Arrays; +import java.util.List; +import java.util.Locale; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +class DayOfTheWeekUnitTest { + + DayOfTheWeek dayOfTheWeek = new DayOfTheWeek(); + + @Test + void whenInputDateIsMonday_ThenFirstDayOfWeekIsSameAsInputDay() { + TestData testData = new TestData(LocalDate.of(2023, 2, 20),LocalDate.of(2023,2,20) ); + Assertions.assertEquals(testData.expectedOutput, dayOfTheWeek.getDateOfFirstDayOfTheWeek_UsingISODayOfWeek(testData.inputDate)); + Assertions.assertEquals(testData.expectedOutput, dayOfTheWeek.getDateOfFirstDayOfTheWeek_UsingCalendar(testData.inputDate)); + Assertions.assertEquals(testData.expectedOutput, dayOfTheWeek.getDateOfFirstDayOfTheWeek_UsingDayOfWeek(testData.inputDate)); + } + + @Test + void whenInputDateIsSunday_ThenFirstDayOfWeekIsPreviousWeekMonday() { + TestData testData = new TestData(LocalDate.of(2023, 2, 19), LocalDate.of(2023,2,13)); + Assertions.assertEquals(testData.expectedOutput, dayOfTheWeek.getDateOfFirstDayOfTheWeek_UsingISODayOfWeek(testData.inputDate)); + Assertions.assertEquals(testData.expectedOutput, dayOfTheWeek.getDateOfFirstDayOfTheWeek_UsingCalendar(testData.inputDate)); + Assertions.assertEquals(testData.expectedOutput, dayOfTheWeek.getDateOfFirstDayOfTheWeek_UsingDayOfWeek(testData.inputDate)); + } + + @Test + void whenInputDateIsNewYearMidWeek_ThenFirstDayOfWeekIsPreviousYearLastMonday() { + TestData testData = new TestData(LocalDate.of(2022, 1, 1), LocalDate.of(2021,12,27)); + Assertions.assertEquals(testData.expectedOutput, dayOfTheWeek.getDateOfFirstDayOfTheWeek_UsingISODayOfWeek(testData.inputDate)); + Assertions.assertEquals(testData.expectedOutput, dayOfTheWeek.getDateOfFirstDayOfTheWeek_UsingCalendar(testData.inputDate)); + Assertions.assertEquals(testData.expectedOutput, dayOfTheWeek.getDateOfFirstDayOfTheWeek_UsingDayOfWeek(testData.inputDate)); + } + + @Test + void whenInputDataHasLocale_ThenFirstDayOfWeekIsMondayOrSundayInDifferentLocale() { + List testData = Arrays.asList( + new TestData(LocalDate.of(2023, 2, 20),Locale.FRANCE,LocalDate.of(2023,2,20)), + new TestData(LocalDate.of(2023, 2, 19),Locale.FRANCE,LocalDate.of(2023,2,13)), + new TestData(LocalDate.of(2022, 1, 1),Locale.FRANCE,LocalDate.of(2021,12,27)), + // day of week starts from sunday in USA + new TestData(LocalDate.of(2023, 2, 20),Locale.US,LocalDate.of(2023,2,19)), + new TestData(LocalDate.of(2023, 2, 19),Locale.US,LocalDate.of(2023,2,19)), + new TestData(LocalDate.of(2022, 1, 1),Locale.US,LocalDate.of(2021,12,26)) + ); + testData.forEach(data -> Assertions.assertEquals(data.expectedOutput, dayOfTheWeek.getDateOfFirstDayOfTheWeek_UsingLocale(data.inputDate,data.inputLocale))); + } + + private static class TestData{ + private final LocalDate inputDate; + private Locale inputLocale; + private final LocalDate expectedOutput; + + private TestData(LocalDate inputDate, LocalDate expectedOutput){ + this.inputDate = inputDate; + this.expectedOutput = expectedOutput; + } + + private TestData(LocalDate inputDate, Locale inputLocale, LocalDate expectedOutput){ + this.inputDate = inputDate; + this.inputLocale = inputLocale; + this.expectedOutput = expectedOutput; + } + } +} \ No newline at end of file