Skip to content
Advertisement

How to get the weekday of a Date?

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?

Advertisement

Answer

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?

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement