What is the easiest way to get the current day of the week in Android?
Asked Answered
I

12

149

What would be the easiest way to get the current day of the week in Android?

Insensibility answered 7/4, 2011 at 0:54 Comment(0)
P
330

The Java Calendar class works.

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

switch (day) {
    case Calendar.SUNDAY:
        // Current day is Sunday
        break;
    case Calendar.MONDAY:
        // Current day is Monday
        break;
    case Calendar.TUESDAY:
        // etc.
        break;
}

For much better datetime handling consider using the Java 8 time API:

String day = LocalDate.now().getDayOfWeek().name()

To use this below Android SDK 26 you'll need to enable Java 8 desugaring in build.gradle:

android {
  defaultConfig {
    // Required when setting minSdkVersion to 20 or lower
    multiDexEnabled true
  }

  compileOptions {
    // Flag to enable support for the new language APIs
    coreLibraryDesugaringEnabled true
    // Sets Java compatibility to Java 8
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

dependencies {
  coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.9'
}

More information on Android's Java 8 support: https://developer.android.com/studio/write/java8-support

Purify answered 7/4, 2011 at 1:5 Comment(3)
This is appliclable across Java, not Android. The "Calendar" class is from java.util packageGelt
@Navin Israni What do you mean? I can't see any problem in using java.util. Calendar is added in API level 1.Poss
be careful, Calendar returns MONDAY as 2 whitch does not meet the standard. Java 8 datetime is correct with MONDAY as 1 (ISO)Eringo
B
41
Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();
// 3 letter name form of the day
System.out.println(new SimpleDateFormat("EE", Locale.ENGLISH).format(date.getTime()));
// full name form of the day
System.out.println(new SimpleDateFormat("EEEE", Locale.ENGLISH).format(date.getTime()));

Result (for today):

Sat
Saturday

UPDATE: java8

LocalDate date = LocalDate.now();
DayOfWeek dow = date.getDayOfWeek();
System.out.println("Enum = " + dow);

String dayName = dow.getDisplayName(TextStyle.FULL, Locale.ENGLISH);
System.out.println("FULL = " + dayName);

dayName = dow.getDisplayName(TextStyle.FULL_STANDALONE, Locale.ENGLISH);
System.out.println("FULL_STANDALONE = " + dayName);

dayName = dow.getDisplayName(TextStyle.NARROW, Locale.ENGLISH);
System.out.println("NARROW = " + dayName);

dayName = dow.getDisplayName(TextStyle.NARROW_STANDALONE, Locale.ENGLISH);
System.out.println("NARROW_STANDALONE = " + dayName);

dayName = dow.getDisplayName(TextStyle.SHORT, Locale.ENGLISH);
System.out.println("SHORT = " + dayName);

dayName = dow.getDisplayName(TextStyle.SHORT_STANDALONE, Locale.ENGLISH);
System.out.println("SHORT_STANDALONE = " + dayName);

Result (for today):

Enum = SATURDAY
FULL = Saturday
FULL_STANDALONE = Saturday
NARROW = S
NARROW_STANDALONE = 6
SHORT = Sat
SHORT_STANDALONE = Sat
Bankhead answered 11/4, 2015 at 10:34 Comment(0)
H
30

Java 8 datetime API made it so much easier :

LocalDate.now().getDayOfWeek().name()

Will return you the name of the day as String

Output : THURSDAY

Heywood answered 26/10, 2017 at 7:1 Comment(1)
Nice! but requires API Level 26 in AndroidAbydos
G
29
Calendar.getInstance().get(Calendar.DAY_OF_WEEK)

or

new GregorianCalendar().get(Calendar.DAY_OF_WEEK);

Just the same as in Java, nothing particular to Android.

Goyette answered 7/4, 2011 at 1:7 Comment(2)
this returns an intSuzisuzie
You can check that from Calender.SUNDAY etc.Georgettageorgette
H
15
public String weekdays[] = new      DateFormatSymbols(Locale.ITALIAN).getWeekdays();
 Calendar c = Calendar.getInstance();
 Date date = new Date();
 c.setTime(date);
 int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
 System.out.println(dayOfWeek);
 System.out.println(weekdays[dayOfWeek]);
Halutz answered 13/2, 2015 at 5:36 Comment(0)
L
12

If you do not want to use Calendar class at all you can use this

String weekday_name = new SimpleDateFormat("EEEE", Locale.ENGLISH).format(System.currentTimeMillis());

i.e., result is,

"Sunday"
Librate answered 14/6, 2015 at 15:8 Comment(0)
L
6

Here is my simple approach to get Current day

public String getCurrentDay(){

    String daysArray[] = {"Sunday","Monday","Tuesday", "Wednesday","Thursday","Friday", "Saturday"};

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

    return daysArray[day];

}
Legendary answered 29/11, 2016 at 10:45 Comment(2)
I'm not sure why, but in my case day numbers starts from 1, not from 0. So, my array looks like this: String[] days = {"?", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};Abshire
you can get rid of the "?" then like int day = calendar.get(Calendar.DAY_OF_WEEK) - 1;Legendary
C
6

you can use that code for Kotlin which you will use calendar class from java into Kotlin

    val day = Calendar.getInstance().get(Calendar.DAY_OF_WEEK)

    fun dayOfWeek() {
    println("What day is it today?")
    val day = Calendar.getInstance().get(Calendar.DAY_OF_WEEK)
    println( when (day) {
      1 -> "Sunday"
      2 -> "Monday"
      3 -> "Tuesday"
      4 -> "Wednesday"
      5 -> "Thursday"
      6 -> "Friday"
      7 -> "Saturday"
      else -> "Time has stopped"
   })
 }
Cos answered 16/6, 2020 at 12:45 Comment(0)
T
4

Using both method you find easy if you wont last seven days you use (currentdaynumber+7-1)%7,(currentdaynumber+7-2)%7.....upto 6

public static String getDayName(int day){
    switch(day){
        case 0:
            return "Sunday";
        case 1:
            return "Monday";
        case 2:
            return "Tuesday";
        case 3:
            return "Wednesday";
        case 4:
            return "Thursday";
        case 5:
            return  "Friday";
        case 6:
            return "Saturday";
    }

    return "Worng Day";
}
public static String getCurrentDay(){
    SimpleDateFormat dayFormat = new SimpleDateFormat("EEEE", Locale.US);
    Calendar calendar = Calendar.getInstance();
    return dayFormat.format(calendar.getTime());

}
Timekeeper answered 17/2, 2016 at 7:11 Comment(0)
R
0

Just in case you ever want to do this not on Android it's helpful to think about which day where as not all devices mark their calendar in local time.

From Java 8 onwards:

LocalDate.now(ZoneId.of("America/Detroit")).getDayOfWeek()
Remediless answered 17/8, 2020 at 20:18 Comment(0)
T
0

If you want to define the date string in strings.xml. You can do like below.
Calendar.DAY_OF_WEEK return value from 1 -> 7 <=> Calendar.SUNDAY -> Calendar.SATURDAY

strings.xml

<string-array name="title_day_of_week">
    <item>日</item> <!-- sunday -->
    <item>月</item> <!-- monday -->
    <item>火</item>
    <item>水</item>
    <item>木</item>
    <item>金</item>
    <item>土</item> <!-- saturday -->
</string-array>

DateExtension.kt

fun String.getDayOfWeek(context: Context, format: String): String {
    val date = SimpleDateFormat(format, Locale.getDefault()).parse(this)
    return date?.getDayOfWeek(context) ?: "unknown"
}

fun Date.getDayOfWeek(context: Context): String {
    val c = Calendar.getInstance().apply { time = this@getDayOfWeek }
    return context.resources.getStringArray(R.array.title_day_of_week)[c[Calendar.DAY_OF_WEEK] - 1]
}

Using

// get current day
val currentDay = Date().getDayOfWeek(context)

// get specific day
val dayString = "2021-1-4"
val day = dayString.getDayOfWeek(context, "yyyy-MM-dd")
Traditionalism answered 4/1, 2021 at 3:3 Comment(0)
O
-10

As DAY_OF_WEEK in GregorianCalender class is a static field you can access it directly as foolows

int dayOfWeek = GregorianCalender.DAY_OF_WEEK;

Orgell answered 7/10, 2013 at 6:49 Comment(1)
you can easily use Calendar class for java and Kotlin tooCos

© 2022 - 2024 — McMap. All rights reserved.