From 2401a9af7d767d42b37d99e4d1ab6ae356c316bc Mon Sep 17 00:00:00 2001 From: Nacho Verdu Date: Mon, 16 Mar 2020 18:37:46 +0000 Subject: [PATCH 1/2] BAEL-3912: Changes done --- .../getweeknumber/GetWeekNumberNewJava.java | 29 +++++++++++++++ .../getweeknumber/GetWeekNumberOldJava.java | 35 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 core-java-modules/core-java-date-operations-2/src/main/java/com/baeldung/getweeknumber/GetWeekNumberNewJava.java create mode 100644 core-java-modules/core-java-date-operations-2/src/main/java/com/baeldung/getweeknumber/GetWeekNumberOldJava.java diff --git a/core-java-modules/core-java-date-operations-2/src/main/java/com/baeldung/getweeknumber/GetWeekNumberNewJava.java b/core-java-modules/core-java-date-operations-2/src/main/java/com/baeldung/getweeknumber/GetWeekNumberNewJava.java new file mode 100644 index 0000000000..abe9367fc2 --- /dev/null +++ b/core-java-modules/core-java-date-operations-2/src/main/java/com/baeldung/getweeknumber/GetWeekNumberNewJava.java @@ -0,0 +1,29 @@ +package com.baeldung.getweeknumber; + +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + +public class GetWeekNumberNewJava { + private LocalDate date; + + public GetWeekNumberNewJava(String day, String dayFormat) { + date = LocalDate.parse(day, DateTimeFormatter.ofPattern(dayFormat)); + } + + public GetWeekNumberNewJava(int year, int month, int day) { + date = LocalDate.of(year, month, day); + } + + public DayOfWeek getDayOfWeek() { + return date.getDayOfWeek(); + } + + public static void main(String[] args) { + GetWeekNumberNewJava example1 = new GetWeekNumberNewJava(2020, 3, 2); + System.out.println(example1.getDayOfWeek()); + + GetWeekNumberNewJava example2 = new GetWeekNumberNewJava("20200302", "yyyyMMdd"); + System.out.println(example2.getDayOfWeek().getValue()); + } +} diff --git a/core-java-modules/core-java-date-operations-2/src/main/java/com/baeldung/getweeknumber/GetWeekNumberOldJava.java b/core-java-modules/core-java-date-operations-2/src/main/java/com/baeldung/getweeknumber/GetWeekNumberOldJava.java new file mode 100644 index 0000000000..6a63d24607 --- /dev/null +++ b/core-java-modules/core-java-date-operations-2/src/main/java/com/baeldung/getweeknumber/GetWeekNumberOldJava.java @@ -0,0 +1,35 @@ +package com.baeldung.getweeknumber; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; + +public class GetWeekNumberOldJava { + private Calendar calendar; + + public GetWeekNumberOldJava(String day, String dayFormat) throws ParseException { + SimpleDateFormat sdf = new SimpleDateFormat(dayFormat); + + calendar = Calendar.getInstance(); + Date date = sdf.parse(day); + calendar.setTime(date); + } + + public GetWeekNumberOldJava(int year, int month, int day) { + calendar = Calendar.getInstance(); + calendar.set(year, month, day); + } + + public int getDayOfWeek() { + return calendar.get(Calendar.DAY_OF_WEEK); + } + + public static void main(String[] args) throws ParseException { + GetWeekNumberOldJava example1 = new GetWeekNumberOldJava(2020, 2, 2); + System.out.println(example1.getDayOfWeek()); + + GetWeekNumberOldJava example2 = new GetWeekNumberOldJava("20200302", "yyyyMMdd"); + System.out.println(example2.getDayOfWeek()); + } +} From 5249021b680b550922c2fed9a4770601ce38c4a1 Mon Sep 17 00:00:00 2001 From: Nacho Verdu Date: Wed, 18 Mar 2020 18:22:32 +0000 Subject: [PATCH 2/2] BAEL-3912: Changes done for comments BAEL-3912: Changes done for comments BAEL-3912: Changes done for comments BAEL-3912: Changes done for comments BAEL-3912: Changes done for comments BAEL-3912: Changes done for comments BAEL-3912: Changes done for comments --- .../getweeknumber/GetWeekNumberNewJava.java | 29 ----------- .../getweeknumber/GetWeekNumberOldJava.java | 35 ------------- .../weeknumber/WeekNumberUsingCalendar.java | 41 ++++++++++++++++ .../weeknumber/WeekNumberUsingLocalDate.java | 30 ++++++++++++ .../WeekNumberUsingCalendarUnitTest.java | 46 +++++++++++++++++ .../WeekNumberUsingLocalDateUnitTest.java | 49 +++++++++++++++++++ 6 files changed, 166 insertions(+), 64 deletions(-) delete mode 100644 core-java-modules/core-java-date-operations-2/src/main/java/com/baeldung/getweeknumber/GetWeekNumberNewJava.java delete mode 100644 core-java-modules/core-java-date-operations-2/src/main/java/com/baeldung/getweeknumber/GetWeekNumberOldJava.java create mode 100644 core-java-modules/core-java-date-operations-2/src/main/java/com/baeldung/weeknumber/WeekNumberUsingCalendar.java create mode 100644 core-java-modules/core-java-date-operations-2/src/main/java/com/baeldung/weeknumber/WeekNumberUsingLocalDate.java create mode 100644 core-java-modules/core-java-date-operations-2/src/test/java/com/baeldung/weeknumber/WeekNumberUsingCalendarUnitTest.java create mode 100644 core-java-modules/core-java-date-operations-2/src/test/java/com/baeldung/weeknumber/WeekNumberUsingLocalDateUnitTest.java diff --git a/core-java-modules/core-java-date-operations-2/src/main/java/com/baeldung/getweeknumber/GetWeekNumberNewJava.java b/core-java-modules/core-java-date-operations-2/src/main/java/com/baeldung/getweeknumber/GetWeekNumberNewJava.java deleted file mode 100644 index abe9367fc2..0000000000 --- a/core-java-modules/core-java-date-operations-2/src/main/java/com/baeldung/getweeknumber/GetWeekNumberNewJava.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.baeldung.getweeknumber; - -import java.time.DayOfWeek; -import java.time.LocalDate; -import java.time.format.DateTimeFormatter; - -public class GetWeekNumberNewJava { - private LocalDate date; - - public GetWeekNumberNewJava(String day, String dayFormat) { - date = LocalDate.parse(day, DateTimeFormatter.ofPattern(dayFormat)); - } - - public GetWeekNumberNewJava(int year, int month, int day) { - date = LocalDate.of(year, month, day); - } - - public DayOfWeek getDayOfWeek() { - return date.getDayOfWeek(); - } - - public static void main(String[] args) { - GetWeekNumberNewJava example1 = new GetWeekNumberNewJava(2020, 3, 2); - System.out.println(example1.getDayOfWeek()); - - GetWeekNumberNewJava example2 = new GetWeekNumberNewJava("20200302", "yyyyMMdd"); - System.out.println(example2.getDayOfWeek().getValue()); - } -} diff --git a/core-java-modules/core-java-date-operations-2/src/main/java/com/baeldung/getweeknumber/GetWeekNumberOldJava.java b/core-java-modules/core-java-date-operations-2/src/main/java/com/baeldung/getweeknumber/GetWeekNumberOldJava.java deleted file mode 100644 index 6a63d24607..0000000000 --- a/core-java-modules/core-java-date-operations-2/src/main/java/com/baeldung/getweeknumber/GetWeekNumberOldJava.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.baeldung.getweeknumber; - -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Calendar; -import java.util.Date; - -public class GetWeekNumberOldJava { - private Calendar calendar; - - public GetWeekNumberOldJava(String day, String dayFormat) throws ParseException { - SimpleDateFormat sdf = new SimpleDateFormat(dayFormat); - - calendar = Calendar.getInstance(); - Date date = sdf.parse(day); - calendar.setTime(date); - } - - public GetWeekNumberOldJava(int year, int month, int day) { - calendar = Calendar.getInstance(); - calendar.set(year, month, day); - } - - public int getDayOfWeek() { - return calendar.get(Calendar.DAY_OF_WEEK); - } - - public static void main(String[] args) throws ParseException { - GetWeekNumberOldJava example1 = new GetWeekNumberOldJava(2020, 2, 2); - System.out.println(example1.getDayOfWeek()); - - GetWeekNumberOldJava example2 = new GetWeekNumberOldJava("20200302", "yyyyMMdd"); - System.out.println(example2.getDayOfWeek()); - } -} 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 new file mode 100644 index 0000000000..db32421aa4 --- /dev/null +++ b/core-java-modules/core-java-date-operations-2/src/main/java/com/baeldung/weeknumber/WeekNumberUsingCalendar.java @@ -0,0 +1,41 @@ +package com.baeldung.weeknumber; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.Locale; + +public class WeekNumberUsingCalendar { + + public int getWeekNumberFrom(String day, String dateFormat, Locale locale) throws ParseException { + SimpleDateFormat sdf = new SimpleDateFormat(dateFormat); + + Calendar calendar = Calendar.getInstance(locale); + Date date = sdf.parse(day); + calendar.setTime(date); + + return calendar.get(Calendar.WEEK_OF_YEAR); + } + + public int getWeekNumberFrom(int year, int month, int day, Locale locale) { + Calendar calendar = Calendar.getInstance(locale); + calendar.set(year, month, day); + + 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); + calendar.setFirstDayOfWeek(firstDayOfWeek); + calendar.setMinimalDaysInFirstWeek(minimalDaysInFirstWeek); + calendar.set(year, month, day); + + 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 new file mode 100644 index 0000000000..4e1b7be3be --- /dev/null +++ b/core-java-modules/core-java-date-operations-2/src/main/java/com/baeldung/weeknumber/WeekNumberUsingLocalDate.java @@ -0,0 +1,30 @@ +package com.baeldung.weeknumber; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoField; +import java.time.temporal.WeekFields; +import java.util.Locale; + +public class WeekNumberUsingLocalDate { + + public Integer getWeekNumberUsingWeekFiedsFrom(String day, String dayFormat, Locale locale) { + LocalDate date = LocalDate.parse(day, DateTimeFormatter.ofPattern(dayFormat)); + + return date.get(WeekFields.of(locale) + .weekOfYear()); + } + + public Integer getWeekNumberUsinWeekFieldsFrom(int year, int month, int day, Locale locale) { + LocalDate date = LocalDate.of(year, month, day); + + return date.get(WeekFields.of(locale) + .weekOfYear()); + } + + public Integer getWeekNumberUsingChronoFieldFrom(int year, int month, int day) { + LocalDate date = LocalDate.of(year, month, day); + + return date.get(ChronoField.ALIGNED_WEEK_OF_YEAR); + } +} \ No newline at end of file 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 new file mode 100644 index 0000000000..587a459277 --- /dev/null +++ b/core-java-modules/core-java-date-operations-2/src/test/java/com/baeldung/weeknumber/WeekNumberUsingCalendarUnitTest.java @@ -0,0 +1,46 @@ +package com.baeldung.weeknumber; + +import static org.junit.Assert.assertEquals; + +import java.text.ParseException; +import java.util.Calendar; +import java.util.Locale; + +import org.junit.Test; + +public class WeekNumberUsingCalendarUnitTest { + @Test + public void givenDateInStringAndDateFormatUsingLocaleItaly_thenGettingWeekNumberUsingCalendarIsCorrectlyReturned() throws ParseException { + WeekNumberUsingCalendar calendar = new WeekNumberUsingCalendar(); + + assertEquals(12, calendar.getWeekNumberFrom("20200322", "yyyyMMdd", Locale.ITALY)); + } + + @Test + public void givenDateInStringAndDateFormatUsingLocaleCanada_thenGettingWeekNumberUsingCalendarIsCorrectlyReturned() throws ParseException { + WeekNumberUsingCalendar calendar = new WeekNumberUsingCalendar(); + + assertEquals(13, calendar.getWeekNumberFrom("20200322", "yyyyMMdd", Locale.CANADA)); + } + + @Test + public void givenDateInYearMonthDayNumbersLocaleItaly_thenGettingWeekNumberUsingCalendarIsCorrectlyReturned() { + 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() { + WeekNumberUsingCalendar calendar = new WeekNumberUsingCalendar(); + + assertEquals(13, calendar.getWeekNumberFrom(2020, 2, 22, Locale.CANADA)); + } +} 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 new file mode 100644 index 0000000000..9b98222ece --- /dev/null +++ b/core-java-modules/core-java-date-operations-2/src/test/java/com/baeldung/weeknumber/WeekNumberUsingLocalDateUnitTest.java @@ -0,0 +1,49 @@ +package com.baeldung.weeknumber; + +import static org.junit.Assert.assertEquals; + +import java.util.Locale; + +import org.junit.Test; + +public class WeekNumberUsingLocalDateUnitTest { + @Test + public void givenDateInStringAndDateFormatUsingWeekFieldsWithLocaleItaly_thenGettingWeekNumberUsingLocalDateIsCorrectlyReturned() { + WeekNumberUsingLocalDate localDate = new WeekNumberUsingLocalDate(); + + assertEquals(12, localDate.getWeekNumberUsingWeekFiedsFrom("20200322", "yyyyMMdd", Locale.ITALY) + .longValue()); + } + + @Test + public void givenDateInStringAndDateFormatUsingWeekFieldsWithLocaleCanada_thenGettingWeekNumberUsingLocalDateIsCorrectlyReturned() { + WeekNumberUsingLocalDate localDate = new WeekNumberUsingLocalDate(); + + assertEquals(13, localDate.getWeekNumberUsingWeekFiedsFrom("20200322", "yyyyMMdd", Locale.CANADA) + .longValue()); + } + + @Test + public void givenDateInStringAndDateFormatUsingChronoFieds_thenGettingWeekNumberUsingLocalDateIsCorrectlyReturned() { + WeekNumberUsingLocalDate localDate = new WeekNumberUsingLocalDate(); + + assertEquals(12, localDate.getWeekNumberUsingChronoFieldFrom(2020, 3, 22) + .longValue()); + } + + @Test + public void givenDateInYearMonthDayNumbersUsingWeekFieldsWithLocaleItaly_thenGettingWeekNumberUsingLocalDateIsCorrectlyReturned() { + WeekNumberUsingLocalDate localDate = new WeekNumberUsingLocalDate(); + + assertEquals(12, localDate.getWeekNumberUsinWeekFieldsFrom(2020, 3, 22, Locale.ITALY) + .longValue()); + } + + @Test + public void givenDateInYearMonthDayNumbersUsingWeekFieldsWithLocaleCanada_thenGettingWeekNumberUsingLocalDateIsCorrectlyReturned() { + WeekNumberUsingLocalDate localDate = new WeekNumberUsingLocalDate(); + + assertEquals(13, localDate.getWeekNumberUsinWeekFieldsFrom(2020, 3, 22, Locale.CANADA) + .longValue()); + } +}