javadatetimedayofweekdatetime-parsing

How to get DAY of the WEEK from the datetime format in JAVA?


I have date time information in this format:

String reportDate="2012-04-19 12:32:24";

I want to have as output day of the week (Thursday or 4 as result anyway is good). How to achieve this?

Thanks


Solution

  • Use the Calendar after parsing the string:

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
    Date d = sdf.parse(reportDate);
    Calendar cal = Calendar.getInstance();
    cal.setTime(d);
    return cal.get(Calendar.DAY_OF_WEEK);