In Java, how to get strings of days of week (Sun, Mon, ..., Sat) with system's default Locale (language)
Solution 1
If I have not misunderstood you
calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.US);
is what you are looking for. Here you can find the documentation,
Or you can also use, getShortWeekdays()
String[] namesOfDays = DateFormatSymbols.getInstance().getShortWeekdays()
Solution 2
Date now = new Date();
// EEE gives short day names, EEEE would be full length.
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE", Locale.US);
String asWeek = dateFormat.format(now);
You can create the date with your desired date and time. And achieve what you want.
Solution 3
tl;dr
DayOfWeek.MONDAY.getDisplayName(
TextStyle.SHORT ,
Locale.getDefault()
)
java.time
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes. Much of java.time is back-ported to Android (see below).
DayOfWeek
The DayOfWeek
enum defines seven objects, one for each day-of-week. The class offers several handy methods including getDisplayName
to generate a string with localized day name.
To localize, specify:
-
TextStyle
to determine how long or abbreviated should the string be. -
Locale
to determine (a) the human language for translation of name of day, name of month, and such, and (b) the cultural norms deciding issues of abbreviation, capitalization, punctuation, separators, and such.
Example:
String output = DayOfWeek.MONDAY.getDisplayName( TextStyle.SHORT , Locale.CANADA_FRENCH );
You can use the JVM’s current default time zone rather than specify one. But keep in mind the risk: The default zone can be changed at any moment during execution by any code in any thread of any app running within the JVM.
Locale locale = Locale.getDefault() ;
String output = DayOfWeek.MONDAY.getDisplayName( TextStyle.SHORT , locale );
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
-
Java SE 8 and SE 9 and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
-
Java SE 6 and SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
-
Android
- The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
- See How to use ThreeTenABP….
Naetmul
Updated on June 28, 2020Comments
-
Naetmul almost 4 years
The simplest way:
String[] namesOfDays = new String[7] { "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" };
This method does not use Locale. Therefore, if the system's language is not English, this method does not work properly.
Using Joda time, we can do like this:
String[] namesOfDays = new String[7]; LocalDate now = new LocalDate(); for (int i=0; i<7; i++) { /* DateTimeConstants.MONDAY = 1, TUESDAY = 2, ..., SUNDAY = 7 */ namesOfDays[i] = now.withDayOfWeek((DateTimeConstants.SUNDAY + i - 1) % 7 + 1) .dayOfWeek().getAsShortText(); }
However, this method uses today's date and calendar calculations, which are useless for the final purpose. Also, it is a little complicated.
Is there an easy way to get Strings like
"Sun"
,"Mon"
, ...,"Sat"
with system's default locale? -
Naetmul about 11 yearsIs there any static method? Calendar.getDisplayName also needs an instance.
-
Naetmul about 11 yearsI found an answer myself. String[] namesOfDays = DateFormatSymbols.getInstance().getShortWeekdays(); will get the string array.
-
Scott Merritt over 9 yearsI think he wants the days localized.
-
Naetmul over 6 yearsIf you need to specify a locale, you can use
DateFormatSymbols.getInstance(locale).getShortWeekdays()
. -
Basil Bourque almost 5 yearsFYI, these date-time classes were supplanted years ago by the java.time classes defined in JSR 310.
-
Ole V.V. about 4 yearsPlease don’t teach the young ones to use the long outdated and notoriously troublesome
SimpleDateFormat
class. At least not as the first option. And not without any reservation. Today we have so much better injava.time
, the modern Java date and time API, and itsDateTimeFormatter
. Yes, you can use it on Android. For older Android see How to use ThreeTenABP in Android Project. -
mazend about 4 years@OleV.V. Oh. I didn't know that
SimpleDateFormat
and other old date time classes are troublesome. Thank you for let me know.