Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Calendar get current day of week as string [duplicate]

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)

like image 741
ntm Avatar asked Aug 15 '13 15:08

ntm


People also ask

How do you find the current day of the week?

get(Calendar. DAY_OF_WEEK); Just the same as in Java, nothing particular to Android. You can check that from Calender.

How can I get today day in Kotlin?

Example 1: Get Current date and time in default formatnow() method. For default format, it is simply converted from a LocalDateTime object to a string using a toString() method.

How do I get the day of the week in Java?

To get the day of the week, use Calendar. DAY_OF_WEEK.


3 Answers

As simple as this

sCalendar = Calendar.getInstance();
String dayLongName = sCalendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault());
like image 80
sandalone Avatar answered Nov 04 '22 12:11

sandalone


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.

like image 23
Don Gossett Avatar answered Nov 04 '22 12:11

Don Gossett


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);

}
like image 42
arun_gopalan Avatar answered Nov 04 '22 12:11

arun_gopalan