How to get the weekday of a Date?

33,742

Solution 1

You can get the day-integer like that:

Calendar c = Calendar.getInstance();
c.setTime(yourdate); // yourdate is an object of type Date

int dayOfWeek = c.get(Calendar.DAY_OF_WEEK); // this will for example return 3 for tuesday

If you need the output to be "Tue" rather than 3, instead of going through a calendar, just reformat the string: new SimpleDateFormat("EE").format(date) (EE meaning "day of week, short version")

Taken from here: How to determine day of week by passing specific date?

Solution 2

// kotlin

val calendar = Calendar.getInstance()

val dateInfo = DateFormat.getDateInstance(DateFormat.FULL).format(calendar.time)

data.text = dateInfo

Solution 3

java.time

You can do it using DateTimeFormatter.ofPattern("EEEE"):

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String args[]) {
        // Test
        System.out.println(getWeekDayName("2021-04-30"));
    }

    public static String getWeekDayName(String s) {
        DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("u-M-d", Locale.ENGLISH);
        DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("EEEE", Locale.ENGLISH);
        return LocalDate.parse(s, dtfInput).format(dtfOutput);
    }
}

Output:

Friday

Alternatively, you can get it using LocalDate#getDayOfWeek:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.TextStyle;
import java.util.Locale;

public class Main {
    public static void main(String args[]) {
        // Test
        System.out.println(getWeekDayName("2021-04-30"));
    }

    public static String getWeekDayName(String s) {
        DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("u-M-d", Locale.ENGLISH);
        return LocalDate.parse(s, dtfInput).getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.ENGLISH);
    }
}

Output:

Friday

Learn more about the the modern date-time API* from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Share:
33,742

Related videos on Youtube

Sunny Chhatwal
Author by

Sunny Chhatwal

Updated on July 23, 2022

Comments

  • Sunny Chhatwal
    Sunny Chhatwal almost 2 years

    I want to get the day of week from the Java Date object when I have an array of Date in String with me.

    SimpleDateFormat  sourceDateformat = new SimpleDateFormat("yyyy-MM-dd");
    public String[] temp_date;
    public Int[] day = new Int[5];
    Date[] d1= new Date[5];
    Calendar[] cal= new Calendar[5]
    
      try {
         d1[i]= sourceDateformat.parse(temp_date[i].toString());
         cal[i].setTime(d1[i]);   // its not compiling this line..showing error on this line
         day[i]= cal[i].get(Calendar.DAY_OF_WEEK);      
        } 
     catch (ParseException e) {
            e.printStackTrace();
        }
    

    Does anyone know the answer to this?

    • dymmeh
      dymmeh over 10 years
      What is the error? Are you sure you are using the proper date object that the Calendar object expects (java.util.Date)
  • Maher Abuthraa
    Maher Abuthraa over 9 years
  • Usman Rana
    Usman Rana over 7 years
    Nice, but make a little correction 2 is for Monday,not for Tuesday.
  • erosenin
    erosenin about 4 years
    Hi, welcome to StackOverflow. This doesn't seem to answer the question, moreover it is in a different language than the original question was asked in.
  • Ole V.V.
    Ole V.V. over 2 years
    That’s how confusing the Calendar class is: for this seemingly simple task your method printed and returned 2021-11-22T16:44:53. You promised a Tuesday, but November 22 is a Monday. I tried simulating running it November 18, next Thursday. It gave me 2021-11-25T00:00:00, also a Thursday. Don’t use Calendar. It’s way too easy to get your code wrong.