82. Getting the first and last day of the year
Getting the first and last day of the given year (as a numeric value) can be done via LocalDate
and the handy TemporalAdjusters
, firstDayOfYear()
, and lastDayOfYear()
. First, we create a LocalDate
from the given year. Next, we use this LocalDate
with firstDayOfYear()
/lastDayOfYear()
as in the following code:
public static String fetchFirstDayOfYear(int year, boolean name) {
LocalDate ld = LocalDate.ofYearDay(year, 1);
LocalDate firstDay = ld.with(firstDayOfYear());
if (!name) {
return firstDay.toString();
}
return DateTimeFormatter.ofPattern("EEEE").format(firstDay);
}
And, for the last day, the code is almost similar:
public static String fetchLastDayOfYear(int year, boolean name) {
LocalDate ld = LocalDate.ofYearDay(year, 31);
LocalDate lastDay = ld.with(lastDayOfYear());
if (!name) {
return lastDay.toString();
}
return DateTimeFormatter.ofPattern("EEEE").format(lastDay);...