76. Displaying the names of the days of the week
One of the hidden gems in Java is java.text.DateFormatSymbols. This class is a wrapper for date-time formatting data such as the names of the days of the week, and the names of the months. All these names are localizable.Typically you will use DateFormatSymbols via a DateFormat such as SimpleDateFormat, but in order to solve this problem we can use it directly as in the following code:
String[] weekdays = new DateFormatSymbols().getWeekdays();
IntStream.range(0, weekdays.length)
.filter(t -> !weekdays[t].isBlank())
.mapToObj(t -> String.format(“Day: %d -> %s”,
t, weekdays[t]))
.forEach(System.out::println);
This code will output the week day’s names as follows:
Day: 1 -> Sunday
…
Day: 7 -> Saturday
Challenge yourself to come up with another solution.
77. 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 from the given year a LocalDate. Next, we use this LocalDate with firstDayOfYear()/lastDayOfYear() as in the following code:
public static String firstDayOfYear(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 lastDayOfYear(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);
}
If the flag argument (name) is false then we return the first/last day via LocalDate.toString() so we will get something as 2020-01-01 (first day of 2020) and 2020-12-31 (last day of 2020). If this flag argument is true then we rely on the EEEE pattern to return only the name of the first/last day of the year as Wednesday (first day of 2020) and Thursday (last day of 2020).In the bundle code, you can also find a solution to this problem via the Calendar API.
78. Getting the first and last day of the week
Let’s assume given an integer (nrOfWeeks) representing the number of weeks that we want to extract the first and last day of each week starting from now. For instance, for the given nrOfWeeks = 3 and a local date as 06/02/2023, we want this:
[ Mon 06/02/2023,
Sun 12/02/2023,
Mon 13/02/2023,
Sun 19/02/2023,
Mon 20/02/2023,
Sun 26/02/2023
]
This is much easier than might seem. We just need a loop from 0 to nrOfWeeks and two TemporalAdjusters to fit the first/last day of each week. More precisely we need the nextOrSame(DayOfWeek dayOfWeek) and previousOrSame(DayOfWeek dayOfWeek) adjusters.The nextOrSame() adjuster’s role is to adjust the current date to the first occurrence of the given day-of-week after the date being adjusted (this can be next-or-same). On the other hand, the previousOrSame() adjuster’s role is to adjust the current date to the first occurrence of the given day-of-week before the date being adjusted (this can be previous-or-same). For instance, if today is [Tuesday 07/02/2023] then previousOrSame(DayOfWeek.MONDAY) will return [Monday 06/02/2023], and nextOrSame(DayOfWeek.SUNDAY) will return [Sunday 12/02/2023].Based on these statements, we can solve our problem via the following code:
public static List<String> weekBoundaries(int nrOfWeeks) {
List<String> boundaries = new ArrayList<>();
LocalDate timeline = LocalDate.now();
DateTimeFormatter dtf = DateTimeFormatter
.ofPattern(“EEE dd/MM/yyyy”);
for (int i = 0; i < nrOfWeeks; i++) {
boundaries.add(dtf.format(timeline.with(
previousOrSame(DayOfWeek.MONDAY))));
boundaries.add(dtf.format(timeline.with(
nextOrSame(DayOfWeek.SUNDAY))));
timeline = timeline.plusDays(7);
}
return boundaries;
} In the bundled code, you can also see a solution based on the Calendar API.