From 92b922b6c1afc53418c933533e69677810daa55d Mon Sep 17 00:00:00 2001 From: Michael Olayemi Date: Sun, 30 Jul 2023 04:42:23 +0000 Subject: [PATCH] How to Get the Start and the End Date of a Year using Java (#14493) * How to Get the Start and the End Date of a Year using Java * How to Get the Start and the End Date of a Year using Java * How to Get the Start and the End Date of a Year using Java --- .../FirstAndLastDayOfYearUnitTest.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 core-java-modules/core-java-date-operations-3/src/test/java/com/baeldung/firstandlastdayofyear/FirstAndLastDayOfYearUnitTest.java diff --git a/core-java-modules/core-java-date-operations-3/src/test/java/com/baeldung/firstandlastdayofyear/FirstAndLastDayOfYearUnitTest.java b/core-java-modules/core-java-date-operations-3/src/test/java/com/baeldung/firstandlastdayofyear/FirstAndLastDayOfYearUnitTest.java new file mode 100644 index 0000000000..5434c84af5 --- /dev/null +++ b/core-java-modules/core-java-date-operations-3/src/test/java/com/baeldung/firstandlastdayofyear/FirstAndLastDayOfYearUnitTest.java @@ -0,0 +1,55 @@ +package com.baeldung.firstandlastdayofyear; + +import static org.junit.Assert.assertEquals; + +import java.text.SimpleDateFormat; +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.time.Month; +import java.util.Calendar; +import java.util.Date; +import org.junit.jupiter.api.Test; + +import static java.time.temporal.TemporalAdjusters.firstDayOfYear; +import static java.time.temporal.TemporalAdjusters.lastDayOfYear; + +public class FirstAndLastDayOfYearUnitTest { + + @Test + public void givenCurrentDate_whenGettingFirstAndLastDayOfYear_thenCorrectDatesReturned() { + LocalDate today = LocalDate.now(); + LocalDate firstDay = today.with(firstDayOfYear()); + LocalDate lastDay = today.with(lastDayOfYear()); + + assertEquals("2023-01-01", firstDay.toString()); + assertEquals("2023-12-31", lastDay.toString()); + } + + @Test + public void givenCalendarSetToFirstDayOfYear_whenFormattingDateToISO8601_thenFormattedDateMatchesFirstDay() { + Calendar cal = Calendar.getInstance(); + cal.set(Calendar.YEAR, 2023); + cal.set(Calendar.DAY_OF_YEAR, 1); + Date firstDay = cal.getTime(); + + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + String formattedDate = sdf.format(firstDay); + + assertEquals("2023-01-01", formattedDate); + } + + @Test + public void givenCalendarSetToFirstDayOfYear_whenFormattingDateToISO8601_thenFormattedDateMatchesLastDay() { + Calendar cal = Calendar.getInstance(); + cal.set(Calendar.YEAR, 2023); + cal.set(Calendar.MONTH, 11); + cal.set(Calendar.DAY_OF_MONTH, 31); + Date lastDay = cal.getTime(); + + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + String formattedDate = sdf.format(lastDay); + + assertEquals("2023-12-31", formattedDate); + } + +}