diff --git a/core-java-modules/core-java-date-operations-2/src/main/java/com/baeldung/weeknumber/WeekNumberUsingCalendar.java b/core-java-modules/core-java-date-operations-2/src/main/java/com/baeldung/weeknumber/WeekNumberUsingCalendar.java index db32421aa4..7f4a57d997 100644 --- a/core-java-modules/core-java-date-operations-2/src/main/java/com/baeldung/weeknumber/WeekNumberUsingCalendar.java +++ b/core-java-modules/core-java-date-operations-2/src/main/java/com/baeldung/weeknumber/WeekNumberUsingCalendar.java @@ -8,11 +8,11 @@ import java.util.Locale; public class WeekNumberUsingCalendar { - public int getWeekNumberFrom(String day, String dateFormat, Locale locale) throws ParseException { + public int getWeekNumberFrom(String dateString, String dateFormat, Locale locale) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat(dateFormat); - + Date date = sdf.parse(dateString); + Calendar calendar = Calendar.getInstance(locale); - Date date = sdf.parse(day); calendar.setTime(date); return calendar.get(Calendar.WEEK_OF_YEAR); @@ -25,8 +25,8 @@ public class WeekNumberUsingCalendar { return calendar.get(Calendar.WEEK_OF_YEAR); } - public int getWeekNumberFrom(int year, int month, int day, int firstDayOfWeek, int minimalDaysInFirstWeek, Locale locale) { - Calendar calendar = Calendar.getInstance(locale); + public int getWeekNumberFrom(int year, int month, int day, int firstDayOfWeek, int minimalDaysInFirstWeek) { + Calendar calendar = Calendar.getInstance(); calendar.setFirstDayOfWeek(firstDayOfWeek); calendar.setMinimalDaysInFirstWeek(minimalDaysInFirstWeek); calendar.set(year, month, day); @@ -34,8 +34,4 @@ public class WeekNumberUsingCalendar { return calendar.get(Calendar.WEEK_OF_YEAR); } - public static void main(String[] args) { - WeekNumberUsingCalendar calendar = new WeekNumberUsingCalendar(); - System.out.println(calendar.getWeekNumberFrom(2020, 2, 22, Locale.CANADA)); - } } diff --git a/core-java-modules/core-java-date-operations-2/src/main/java/com/baeldung/weeknumber/WeekNumberUsingLocalDate.java b/core-java-modules/core-java-date-operations-2/src/main/java/com/baeldung/weeknumber/WeekNumberUsingLocalDate.java index 4e1b7be3be..86d60311a9 100644 --- a/core-java-modules/core-java-date-operations-2/src/main/java/com/baeldung/weeknumber/WeekNumberUsingLocalDate.java +++ b/core-java-modules/core-java-date-operations-2/src/main/java/com/baeldung/weeknumber/WeekNumberUsingLocalDate.java @@ -15,7 +15,7 @@ public class WeekNumberUsingLocalDate { .weekOfYear()); } - public Integer getWeekNumberUsinWeekFieldsFrom(int year, int month, int day, Locale locale) { + public Integer getWeekNumberUsingWeekFieldsFrom(int year, int month, int day, Locale locale) { LocalDate date = LocalDate.of(year, month, day); return date.get(WeekFields.of(locale) diff --git a/core-java-modules/core-java-date-operations-2/src/test/java/com/baeldung/weeknumber/WeekNumberUsingCalendarUnitTest.java b/core-java-modules/core-java-date-operations-2/src/test/java/com/baeldung/weeknumber/WeekNumberUsingCalendarUnitTest.java index 587a459277..955f2e93a4 100644 --- a/core-java-modules/core-java-date-operations-2/src/test/java/com/baeldung/weeknumber/WeekNumberUsingCalendarUnitTest.java +++ b/core-java-modules/core-java-date-operations-2/src/test/java/com/baeldung/weeknumber/WeekNumberUsingCalendarUnitTest.java @@ -9,38 +9,40 @@ import java.util.Locale; import org.junit.Test; public class WeekNumberUsingCalendarUnitTest { + @Test - public void givenDateInStringAndDateFormatUsingLocaleItaly_thenGettingWeekNumberUsingCalendarIsCorrectlyReturned() throws ParseException { + public void givenDateFormatAndLocaleItaly_whenGetWeekNumber_thenWeekIsReturnedCorrectly() throws ParseException { WeekNumberUsingCalendar calendar = new WeekNumberUsingCalendar(); assertEquals(12, calendar.getWeekNumberFrom("20200322", "yyyyMMdd", Locale.ITALY)); } @Test - public void givenDateInStringAndDateFormatUsingLocaleCanada_thenGettingWeekNumberUsingCalendarIsCorrectlyReturned() throws ParseException { + public void givenDateFormatAndLocaleCanada_whenGetWeekNumber_thenWeekIsReturnedCorrectly() throws ParseException { WeekNumberUsingCalendar calendar = new WeekNumberUsingCalendar(); assertEquals(13, calendar.getWeekNumberFrom("20200322", "yyyyMMdd", Locale.CANADA)); } @Test - public void givenDateInYearMonthDayNumbersLocaleItaly_thenGettingWeekNumberUsingCalendarIsCorrectlyReturned() { + public void givenDateUsingFieldsAndLocaleItaly_whenGetWeekNumber_thenWeekIsReturnedCorrectly() { WeekNumberUsingCalendar calendar = new WeekNumberUsingCalendar(); assertEquals(12, calendar.getWeekNumberFrom(2020, 2, 22, Locale.ITALY)); } @Test - public void givenDateInYearMonthDayNumbersLocaleItalyChangingWeekCalculationSettings_thenGettingWeekNumberUsingCalendarIsCorrectlyReturned() { - WeekNumberUsingCalendar calendar = new WeekNumberUsingCalendar(); - - assertEquals(13, calendar.getWeekNumberFrom(2020, 2, 22, Calendar.SUNDAY, 4, Locale.ITALY)); - } - - @Test - public void givenDateInYearMonthDayNumbersLocaleCanada_thenGettingWeekNumberUsingCalendarIsCorrectlyReturned() { + public void givenDateUsingFieldsAndLocaleCanada_whenGetWeekNumber_thenWeekIsReturnedCorrectly() { WeekNumberUsingCalendar calendar = new WeekNumberUsingCalendar(); assertEquals(13, calendar.getWeekNumberFrom(2020, 2, 22, Locale.CANADA)); } + + @Test + public void givenDateUsingFieldsAndLocaleItaly_whenChangingWeekCalcSettings_thenWeekIsReturnedCorrectly() { + WeekNumberUsingCalendar calendar = new WeekNumberUsingCalendar(); + + assertEquals(13, calendar.getWeekNumberFrom(2020, 2, 22, Calendar.SUNDAY, 4)); + } + } diff --git a/core-java-modules/core-java-date-operations-2/src/test/java/com/baeldung/weeknumber/WeekNumberUsingLocalDateUnitTest.java b/core-java-modules/core-java-date-operations-2/src/test/java/com/baeldung/weeknumber/WeekNumberUsingLocalDateUnitTest.java index 9b98222ece..6c8f1469b2 100644 --- a/core-java-modules/core-java-date-operations-2/src/test/java/com/baeldung/weeknumber/WeekNumberUsingLocalDateUnitTest.java +++ b/core-java-modules/core-java-date-operations-2/src/test/java/com/baeldung/weeknumber/WeekNumberUsingLocalDateUnitTest.java @@ -8,42 +8,37 @@ import org.junit.Test; public class WeekNumberUsingLocalDateUnitTest { @Test - public void givenDateInStringAndDateFormatUsingWeekFieldsWithLocaleItaly_thenGettingWeekNumberUsingLocalDateIsCorrectlyReturned() { + public void givenDateFormatAndLocaleItaly_whenGetWeekNumber_thenWeekIsReturnedCorrectly() { WeekNumberUsingLocalDate localDate = new WeekNumberUsingLocalDate(); - assertEquals(12, localDate.getWeekNumberUsingWeekFiedsFrom("20200322", "yyyyMMdd", Locale.ITALY) - .longValue()); + assertEquals(Integer.valueOf(12), localDate.getWeekNumberUsingWeekFiedsFrom("20200322", "yyyyMMdd", Locale.ITALY)); } @Test - public void givenDateInStringAndDateFormatUsingWeekFieldsWithLocaleCanada_thenGettingWeekNumberUsingLocalDateIsCorrectlyReturned() { + public void givenDateFormatAndLocaleCanada_whenGetWeekNumber_thenWeekIsReturnedCorrectly() { WeekNumberUsingLocalDate localDate = new WeekNumberUsingLocalDate(); - assertEquals(13, localDate.getWeekNumberUsingWeekFiedsFrom("20200322", "yyyyMMdd", Locale.CANADA) - .longValue()); + assertEquals(Integer.valueOf(13), localDate.getWeekNumberUsingWeekFiedsFrom("20200322", "yyyyMMdd", Locale.CANADA)); } @Test - public void givenDateInStringAndDateFormatUsingChronoFieds_thenGettingWeekNumberUsingLocalDateIsCorrectlyReturned() { + public void givenDateUsingChronoFields_whenGetWeekNumber_thenWeekIsReturnedCorrectly() { WeekNumberUsingLocalDate localDate = new WeekNumberUsingLocalDate(); - assertEquals(12, localDate.getWeekNumberUsingChronoFieldFrom(2020, 3, 22) - .longValue()); + assertEquals(Integer.valueOf(12), localDate.getWeekNumberUsingChronoFieldFrom(2020, 3, 22)); } @Test - public void givenDateInYearMonthDayNumbersUsingWeekFieldsWithLocaleItaly_thenGettingWeekNumberUsingLocalDateIsCorrectlyReturned() { + public void givenDateUsingFieldsWithLocaleItaly_whenGetWeekNumber_thenWeekIsReturnedCorrectly() { WeekNumberUsingLocalDate localDate = new WeekNumberUsingLocalDate(); - assertEquals(12, localDate.getWeekNumberUsinWeekFieldsFrom(2020, 3, 22, Locale.ITALY) - .longValue()); + assertEquals(Integer.valueOf(12), localDate.getWeekNumberUsingWeekFieldsFrom(2020, 3, 22, Locale.ITALY)); } @Test - public void givenDateInYearMonthDayNumbersUsingWeekFieldsWithLocaleCanada_thenGettingWeekNumberUsingLocalDateIsCorrectlyReturned() { + public void givenDateUsingFieldsWithLocaleCanada_whenGetWeekNumber_thenWeekIsReturnedCorrectly() { WeekNumberUsingLocalDate localDate = new WeekNumberUsingLocalDate(); - assertEquals(13, localDate.getWeekNumberUsinWeekFieldsFrom(2020, 3, 22, Locale.CANADA) - .longValue()); + assertEquals(Integer.valueOf(13), localDate.getWeekNumberUsingWeekFieldsFrom(2020, 3, 22, Locale.CANADA)); } }