40

i tried to get a string with the name of the actual weekday this way:

            Calendar c = Calendar.getInstance();
            int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);

            if (c.get(Calendar.MONDAY) == dayOfWeek) weekDay = "monday";
            else if (c.get(Calendar.TUESDAY) == dayOfWeek) weekDay = "tuesday";
            else if (c.get(Calendar.WEDNESDAY) == dayOfWeek) weekDay = "wednesday";
            else if (c.get(Calendar.THURSDAY) == dayOfWeek) weekDay = "thursday";
            else if (c.get(Calendar.FRIDAY) == dayOfWeek) weekDay = "friday";
            else if (c.get(Calendar.SATURDAY) == dayOfWeek) weekDay = "saturday";
            else if (c.get(Calendar.SUNDAY) == dayOfWeek) weekDay = "sunday";

but weekDay stays always null and i actually have no idea why because the debugger says that dayOfWeek is 5 so i should be equal to c.get(Calendar.THURSDAY)

ntm
  • 731
  • 2
  • 13
  • 37
  • What is the *type* of `c.get(`? An `int`? Or the `Integer` reference type? – nanofarad Aug 15 '13 at 15:53
  • just use an array, why do you need if else. String[] days=new String[]{"Monday".... although it is safer to actually instantiate inside a method. days[Calendar.getInstance().get(Calendar.Day_OF_WEEK)] should do the trick. month and day are 0 based in the utils package – Andrew Scott Evans Apr 29 '14 at 17:48

6 Answers6

83

As simple as this

sCalendar = Calendar.getInstance();
String dayLongName = sCalendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault());
sandalone
  • 41,141
  • 63
  • 222
  • 338
64

I accomplished this by doing the following:

String weekDay;
SimpleDateFormat dayFormat = new SimpleDateFormat("EEEE", Locale.getDefault());

Calendar calendar = Calendar.getInstance();
weekDay = dayFormat.format(calendar.getTime());
  • Use the day format for "EEEE" will return the full weekday name, i.e. Tuesday
  • Use the day format for "E" will return the the abbreviated name, i.e. Tue
  • Another benefit to returning this format in Android is that it will translate the weekday based on the locale.

You will want to review the SimpleDateFormat to learn more. I think this is the cleanest approach in that you do not need a switch or if statement if your only need is to get the string value.

Don Gossett
  • 782
  • 5
  • 12
  • 9
    This is definitely the best answer. Simple and beautiful. Thank u! Perhaps instead of Locale.US I would use Locale.getDefault(). – Joao Sousa Dec 18 '13 at 23:00
  • `Locale locale = itemView.getContext().getResources().getConfiguration().getLocales().get(0)` – Mooing Duck Apr 04 '20 at 21:16
17

You are supposed to compare dayOfWeek directly with Calendar.MONDAY etc. See code below

Also, I have put brackets around if else. Do not rely on indentation for code flow, explicitly put brackets even if your if-else has only one statement.

    public static void main(String[] args) {

    String weekDay = "";

    Calendar c = Calendar.getInstance();
    int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);

    if (Calendar.MONDAY == dayOfWeek) {
        weekDay = "monday";
    } else if (Calendar.TUESDAY == dayOfWeek) {
        weekDay = "tuesday";
    } else if (Calendar.WEDNESDAY == dayOfWeek) {
        weekDay = "wednesday";
    } else if (Calendar.THURSDAY == dayOfWeek) {
        weekDay = "thursday";
    } else if (Calendar.FRIDAY == dayOfWeek) {
        weekDay = "friday";
    } else if (Calendar.SATURDAY == dayOfWeek) {
        weekDay = "saturday";
    } else if (Calendar.SUNDAY == dayOfWeek) {
        weekDay = "sunday";
    }

    System.out.println(weekDay);

}
arun_gopalan
  • 261
  • 1
  • 7
3

There was no need to use the c.get

    Calendar c = Calendar.getInstance();
    int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);                

    if (Calendar.MONDAY == dayOfWeek) weekDay = "monday";
    else if (Calendar.TUESDAY == dayOfWeek) weekDay = "tuesday";
    else if (Calendar.WEDNESDAY == dayOfWeek) weekDay = "wednesday";
    else if (Calendar.THURSDAY == dayOfWeek) weekDay = "thursday";
    else if (Calendar.FRIDAY == dayOfWeek) weekDay = "friday";
    else if (Calendar.SATURDAY == dayOfWeek) weekDay = "saturday";
    else if (Calendar.SUNDAY == dayOfWeek) weekDay = "sunday";

    System.out.println(weekDay);

and output:

thursday

You can see your error if you try to print the values with c.get with this code

    System.out.println(c.get(Calendar.MONDAY));
    System.out.println(c.get(Calendar.TUESDAY));
    System.out.println(c.get(Calendar.WEDNESDAY));
    System.out.println(c.get(Calendar.THURSDAY));
    System.out.println(c.get(Calendar.FRIDAY));
    System.out.println(c.get(Calendar.SATURDAY));
    System.out.println(c.get(Calendar.SUNDAY));

For example I get:

7
33
3
15
227
5
2013

And the result will be incorrect, in my case I get Sunday as weekDay if I use your code.

Oscerd
  • 1,616
  • 11
  • 14
3

just do the following:

    Date date = new Date();
    CharSequence time = DateFormat.format("EEEE", date.getTime()); // gives like (Wednesday)
Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
2

try this

import java.text.DateFormatSymbols;
import java.util.Date;
...
String[] weekdays = new DateFormatSymbols().getWeekdays();
String[] month = new DateFormatSymbols().getMonths();
System.outprintln(weekdays[date.getDay()+1]+","+month[date.getMonth()]+" "+date.getDate());
gifpif
  • 4,507
  • 4
  • 31
  • 45