Search code examples
androiddatedayofweekweekday

How to get day from specified date in android


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)?


Solution

  • 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);