How to get day from specified date in android

Android is everything for me picture Android is everything for me · Jan 2, 2013 · Viewed 19.5k times · Source

Suppose my date is 02-01-2013

and it is stored in a variable like:

String strDate = "02-01-2013";

then how should I get the day of this date (i.e TUESDAY)?

Answer

jellyfication picture jellyfication · Jan 2, 2013

Use Calendar class from java api.

Calendar calendar = new GregorianCalendar(2008, 01, 01); // Note that Month value is 0-based. e.g., 0 for January.
int reslut = calendar.get(Calendar.DAY_OF_WEEK);
switch (result) {
case Calendar.MONDAY:
    System.out.println("It's Monday !");
    break;
}

You could also use SimpleDateFormater and Date for parsing dates

Date date = new Date();
SimpleDateFormat date_format = new SimpleDateFormat("yyyy-MM-dd");
try {
    date = date_format.parse("2008-01-01");
} catch (ParseException e) {
    e.printStackTrace();
}

calendar.setTime(date);