Charaf Eddine Mechalikh
Charaf Eddine Mechalikh

Reputation: 1248

Android get Day of week from date returns different values for the same day of week

Here is my code that convert string ("12/05/2015") from textView into date with the same format, and i want to get from that date its day of week.

String d1=((TextView)findViewById(R.id.tvDate2)).getText().toString(); 
    SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
    Date convertedDate = new Date();
    try {
        convertedDate = dateFormat.parse(d1);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    Calendar c = Calendar.getInstance();
    c.setTime(convertedDate);
    int day= c.get(Calendar.DAY_OF_WEEK);
    System.out.println("day:"+day);

when i try 22/05/2015 (friday) it returns 4 but for 29/05/2015 (also friday) it returns 6

so where is the problem

Upvotes: 1

Views: 276

Answers (2)

Andy Turner
Andy Turner

Reputation: 140328

You have got your months and days flipped around in your format string. Try:

"dd/MM/yyyy"

Upvotes: 2

rgettman
rgettman

Reputation: 178263

You've mixed up the month and the day.

Either change your format to "dd/MM/yyyy" or change the inputted date string to "05/22/2015".

Upvotes: 2

Related Questions