简体   繁体   中英

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

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" .

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

"dd/MM/yyyy"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM