Trying to convert a date YYYY-MM-DD to DD-MMM-YYYY but the function always returns January as the month.

Viewed 7358

I have the following function to convert date:

public String dateConvert(String D){

        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-DD");
        SimpleDateFormat format2 = new SimpleDateFormat("dd-MMMM-yyyy");
        Date date = null;
        try {
            date = format1.parse(D);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String dateString = format2.format(date);
        dateString = dateString.replace("-", " "); 
        System.out.println(dateString);
        return ((dateString));
    }

However what ever date I pass to this, the month is always converted to January. I cant understand where I am going wrong!

2 Answers
Related