17

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?

Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
Sunny Chhatwal
  • 185
  • 1
  • 1
  • 7
  • 2
    What is the error? Are you sure you are using the proper date object that the Calendar object expects (java.util.Date) – dymmeh Sep 03 '13 at 19:52

6 Answers6

41

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?

Kashif
  • 4,642
  • 7
  • 44
  • 97
Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
3
// kotlin

val calendar = Calendar.getInstance()

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

data.text = dateInfo
stealthyninja
  • 10,343
  • 11
  • 51
  • 59
  • 5
    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. – erosenin Mar 25 '20 at 06:05
1

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.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

This is one of the many things that have become a lot easier with the advent of java.time, the modern Java date and time API.

    String tempDate = "2020-03-29";
    LocalDate date = LocalDate.parse(tempDate);
    DayOfWeek day = date.getDayOfWeek();
    System.out.println(day);

Output:

SUNDAY

Of course we now have got an enum for the days of the week. There’s no longer any reason to fiddle with integers and having to remember on what day of week they begin and whether the days are numbered from 0 or 1.

I am expoiting the fact that your expected input format (yyyy-MM-dd in your code) is ISO 8601, the default for java.time, so we don’t need to specify any formatter explicitly.

Question: Doesn’t java.time require Android API level 26?

java.time works nicely on both older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

In Kotlin, just do this:

val yourDateStr = "2021-01-01"
val df = DateFormat.parse(yourDateStr)
val weedDay = df.getWeekDay(yourTimeZone)
Jian Hui
  • 11
  • 2
0

the below code works, depending on what number you enter in the DAY_OF_WEEK, that returns the specific weekday, in this example it always returns a future date and day will always be Tuesday.

DAY_OF_WEEK

public static String getTodaysDateAndTime(){

    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DATE, 5);
    cal.add(Calendar.DAY_OF_WEEK, 2);
    Date date = cal.getTime();
    SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    String futureDate = dateformat.format(date);
    System.out.println(futureDate);
    return futureDate;
}
  • 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. – Ole V.V. Nov 15 '21 at 15:48