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 <deboshree.banerjee@cred.club>
This commit is contained in:
ciphx 2023-03-23 22:40:40 +05:30 committed by GitHub
parent 494ccc72fa
commit 20829a6110
2 changed files with 109 additions and 0 deletions

View File

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

View File

@ -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> 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;
}
}
}