In this Java tutorial, we will explore various methods to display the name of the day of the week, catering to different scenarios and requirements. Note that we should be careful of using the int values assigned to weekdays as they differ from method to method.
1. Display the Day of the Week using Java 8 DayOfWeek
The java.time.DayOfWeek is part of new Java 8 date-time APIs and represents a day-of-week, such as ‘Tuesday‘. It is an enum representing the 7 days of the week – Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday.
In addition to the textual enum name, each day-of-week has an int value from 1 (Monday) to 7 (Sunday). Some locales also assign different numeric values to the days, declaring Sunday to have the value 1, however this class provides no support for this. Use WeekFields for localized week-numbering.
1.1. Displaying Names of all Days of The Week
The following Java program iterates over DayOfWeek enum constants and prints all the days’ names ‘days[t]‘ and assigned int values t.
DayOfWeek[] days = DayOfWeek.values();
IntStream.range(0, days.length)
.mapToObj(t -> String.format("Day: %d -> %s", t, days[t]))
.forEach(System.out::println);
Program output:
Day: 0 -> MONDAY
Day: 1 -> TUESDAY
Day: 2 -> WEDNESDAY
Day: 3 -> THURSDAY
Day: 4 -> FRIDAY
Day: 5 -> SATURDAY
Day: 6 -> SUNDAY
1.2. Finding Name of the Day of the Week for a Given Date
Sometimes we may want to find the name of the day of the week for a specified date which we can achieve using the DayOfWeek.from(temporal) method.
LocalDate localDate = LocalDate.now();
DayOfWeek weekOfTheDay = DayOfWeek.from(localDate);
System.out.println(weekOfTheDay.getValue());
System.out.println(weekOfTheDay.getDisplayName(TextStyle.FULL, Locale.getDefault()));
Program output:
5
Friday
To print the names in a specified locale, we can pass the Locale information to the getDisplayName() method.
System.out.println(weekOfTheDay.getDisplayName(TextStyle.FULL, Locale.FRENCH));
Program output:
vendredi
2. Using DateFormatSymbols
The DateFormatSymbols class also provides methods to retrieve the names of weekdays in different languages and formats. This class can be used with legacy applications that still have not migrated to Java 8.
Notice the program output that int values are assigned from 1 to 7, whereas 1 is Sunday and 7 is Saturday.
IntStream.range(0, weekdays.length)
.filter(t -> !weekdays[t].isBlank())
.mapToObj(t -> String.format("Day: %d -> %s", t, weekdays[t]))
.forEach(System.out::println);
Program output:
Day: 1 -> Sunday
Day: 2 -> Monday
Day: 3 -> Tuesday
Day: 4 -> Wednesday
Day: 5 -> Thursday
Day: 6 -> Friday
Day: 7 -> Saturday
For displaying the name in a specific Locale, we can use the DateFormatSymbols.getInstance(locale) method to get the instance of it.
String[] weekdays = DateFormatSymbols.getInstance(Locale.FRENCH).getWeekdays();
//...
Program output:
Day: 1 -> dimanche
Day: 2 -> lundi
Day: 3 -> mardi
Day: 4 -> mercredi
Day: 5 -> jeudi
Day: 6 -> vendredi
Day: 7 -> samedi
If we want to get the name of the day of the week for a specified date, we can use the weekdays array and pass the index obtained from calendar.get(Calendar.DAY_OF_WEEK).
String[] weekdays = DateFormatSymbols.getInstance().getWeekdays();
Calendar calendar = Calendar.getInstance();
int dayOfWeekValue = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println(weekdays[dayOfWeekValue]);
Program output:
Friday
Adjust the code according to your specified date and display preferences. This approach allows you to obtain the name of the day of the week for any given date.
3. Conclusion
Obtaining and displaying the name of the day of the week is a common requirement in many applications. This tutorial explored methods using the Java 8 DayOfWeek enum and DateFormatSymbols class.
As a best practice, we should not use the integer values to represent the weekday names. Rather we should use the enum constants for error-free and clean code.
Happy Learning !!
Comments