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?

Naetmul
  • 14,544
  • 8
  • 57
  • 81
  • On a related note: [*Get date of first day of week based on LocalDate.now() in Java 8*](https://stackoverflow.com/q/28450720/642706) – Basil Bourque May 20 '19 at 02:42

7 Answers7

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()
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • Is there any static method? Calendar.getDisplayName also needs an instance. – Naetmul May 14 '13 at 11:13
  • 4
    I found an answer myself. String[] namesOfDays = DateFormatSymbols.getInstance().getShortWeekdays(); will get the string array. – Naetmul May 14 '13 at 11:24
  • 1
    If you need to specify a locale, you can use `DateFormatSymbols.getInstance(locale).getShortWeekdays()`. – Naetmul Aug 23 '17 at 01:01
  • FYI, these date-time classes were supplanted years ago by the *java.time* classes defined in JSR 310. – Basil Bourque May 20 '19 at 02:41
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.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
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?

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
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.

yincrash
  • 6,394
  • 1
  • 39
  • 41
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;
    }
mazend
  • 456
  • 1
  • 7
  • 37
  • 1
    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,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Android. For older Android see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Apr 03 '20 at 14:25
  • @OleV.V. Oh. I didn't know that `SimpleDateFormat` and other old date time classes are troublesome. Thank you for let me know. – mazend Apr 04 '20 at 12:30
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);
     }

}
Matt Ke
  • 3,599
  • 12
  • 30
  • 49
xyz
  • 1
  • 1
-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]);
Nitin Karale
  • 789
  • 3
  • 12
  • 34