Table of Contents
In this post, we will see how to get list of weekday names in Java.
Using DateFormatSymbols’s getWeekdays() method
We can use DateFormatSymbols's getWeekdays()
to get list of weekday names in Java. It will provide complete weekday names like Sunday
, Monday
etc.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package org.arpit.java2blog; import java.text.DateFormatSymbols; import java.util.Arrays; public class GetListOfWeekdayNames { public static void main(String[] args) { // Create DateFormatSymbols with default Locale DateFormatSymbols dfs = new DateFormatSymbols(); String[] weekdays = dfs.getWeekdays(); System.out.println("Weekdays are: "+ Arrays.toString(weekdays)); } } |
Output
1 2 3 |
Weekdays are: [, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday] |
Using DateFormatSymbols’s getShortWeekdays() method [For short names]
If you need short names rather than complete weekdays name, you can use DateFormatSymbols's getShortWeekdays()
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package org.arpit.java2blog; import java.text.DateFormatSymbols; import java.util.Arrays; import java.util.Locale; public class GetListOfWeekdayNames { public static void main(String[] args) { // Create DateFormatSymbols with default Locale DateFormatSymbols dfs = new DateFormatSymbols(); String[] shortWeekDays = dfs.getShortWeekdays(); System.out.println("Weekdays with short names are: "+ Arrays.toString(shortWeekDays)); } } |
Output
1 2 3 |
Weekdays with short names are: [, Sun, Mon, Tue, Wed, Thu, Fri, Sat] |
Using DateFormatSymbols’s getWeekdays() with Locale
If you are looking for weekday names in different Locale like German or French, you can specify Locale while creating object of DateFormatSymbols
. It will work for both methods i.e. getWeekdays()
and getShortWeekdays()
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package org.arpit.java2blog; import java.text.DateFormatSymbols; import java.util.Arrays; import java.util.Locale; public class GetListOfWeekdayNames { public static void main(String[] args) { DateFormatSymbols dfs = new DateFormatSymbols(Locale.GERMAN); String[] weekdays = dfs.getWeekdays(); System.out.println("German weekdays are: "+ Arrays.toString(weekdays)); String[] shortWeekDays = dfs.getShortWeekdays(); System.out.println("German weekdays in short name are: "+ Arrays.toString(shortWeekDays)); } } |
Output
1 2 3 4 |
German weekdays are: [, Sonntag, Montag, Dienstag, Mittwoch, Donnerstag, Freitag, Samstag] German weekdays in short name are: [, So., Mo., Di., Mi., Do., Fr., Sa.] |
Further reading:
That’s all about how to get list of months in Java.