Search code examples
androidsimpledateformatandroid-date

Date Formatting is not working in android?


I have created a method to format date to "dd-MMM-yyyy" which is :

public String formatDate(String day, String month, String year) {
        // Do something with the date chosen by the user
        SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
        Calendar c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_MONTH, Integer.valueOf(day));
        c.set(Calendar.MONTH, Integer.valueOf(month));
        c.set(Calendar.YEAR, Integer.valueOf(year));
        return df.format(c.getTime());
    }

I am calling this method like formatDate("12","10","2014") and getting result 12-Oct-2015.I am not able to understand why is it returning me year 2015 instead of 2014.Please help


Solution

  • probably because months in Calendar starts from 0 and from 1. Try passing 11, or better Calendar.DECEMBER for december. You can find here the documentation