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

戏子无情 提交于 2019-11-30 10:45:43

As simple as this

sCalendar = Calendar.getInstance();
String dayLongName = sCalendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault());

I accomplished this by doing the following:

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

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.

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

}

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.

just do the following:

    Date date = new Date();
    CharSequence time = DateFormat.format("EEEE", date.getTime()); // gives like (Wednesday)

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());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!