In Java, how to get strings of days of week (Sun, Mon, ..., Sat) with system's default Locale (language)
Asked Answered
P

7

31

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?

Prove answered 14/5, 2013 at 10:59 Comment(1)
On a related note: Get date of first day of week based on LocalDate.now() in Java 8Maidel
M
58

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()
Mella answered 14/5, 2013 at 11:3 Comment(4)
Is there any static method? Calendar.getDisplayName also needs an instance.Prove
I found an answer myself. String[] namesOfDays = DateFormatSymbols.getInstance().getShortWeekdays(); will get the string array.Prove
If you need to specify a locale, you can use DateFormatSymbols.getInstance(locale).getShortWeekdays().Prove
FYI, these date-time classes were supplanted years ago by the java.time classes defined in JSR 310.Maidel
S
11
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.

Stonework answered 14/5, 2013 at 11:10 Comment(0)
M
4

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?

Maidel answered 16/3, 2017 at 5:34 Comment(0)
T
0

Without 1.8, you can use DateFormatSymbols (which also works with Android).

DateFormatSymbols.getWeekdays()

Returns: the weekday strings. Use Calendar.SUNDAY, Calendar.MONDAY, etc. to index the result array.

Thoughtout answered 10/7, 2018 at 5:12 Comment(0)
F
0

I think my code is useful. You can change it for your purpose easily.

Result string array is "Sat", "Sun", "Mon", "Tue", "Wed", "Thu" and "Fri".

    public String[] getNameOfDays(){
        SimpleDateFormat sdf_day_of_week = new SimpleDateFormat("EEE", Locale.getDefault());
        String[] nameOfDays = new String[7];

        Calendar calendar = Calendar.getInstance();

        for(int i=0; i<7; i++) {
            calendar.set(Calendar.DAY_OF_WEEK, i);
            nameOfDays[i] = sdf_day_of_week.format(calendar.getTime());
        }

        return nameOfDays;
    }
Flash answered 2/4, 2020 at 14:49 Comment(2)
Please 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 in java.time, the modern Java date and time API, and its DateTimeFormatter. Yes, you can use it on Android. For older Android see How to use ThreeTenABP in Android Project.Whittington
@OleV.V. Oh. I didn't know that SimpleDateFormat and other old date time classes are troublesome. Thank you for let me know.Flash
G
0
public class daysOfWeek {

     public static String days(String s, int k) {
         String[] days = new String[] {"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};
         int len = days.length;
         int index = 0;
         int result = 0;
         for(int i=0; i<len; i++) {
             if(days[i]==s) {
                index =i;
                k%=7;
                result = (index +k)%7;
             }
            
         }
         return days[result];
     }
     
     public static void main(String args[]) {
         String S ="Tue"; int k =25;
         String s =days(S,k);
         System.out.println(s);
     }

}
Gerstner answered 27/6, 2020 at 22:17 Comment(0)
S
-3

Please try this

public static String[] namesOfDays =  {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};


 int day = Calendar.getInstance().get(Calendar.DAY_OF_WEEK);

System.out.println("Day := "+namesOfDays[day-1]);
Sixgun answered 23/11, 2013 at 6:46 Comment(1)
I think he wants the days localized.Lighten

© 2022 - 2024 — McMap. All rights reserved.