Kirmani88 Kirmani88 - 4 months ago 16
Java Question

Date Parser parsing every month to January

I have written following code to parse Date into this format

25 July, 2016
. This is what i have done to achieve this.

Date _date;
String datePattern = "dd MMMM, yyyy";
DateFormat formatter = new SimpleDateFormat("yyyy-MM-DD");
SimpleDateFormat format = new SimpleDateFormat(datePattern);
String parseDate;
try {
_date= formatter.parse(date); //date Value is coming from serverin this format = 2016-07-21T16:01:57
parseDate = format.format(_date);
} catch (ParseException e) {
e.printStackTrace();
}


The problem is that, all it converts all Months to
January
It converts this datet
2016-07-21T16:01:57
to
21 January, 2016
.

Kindly guide me what i am doing wrong here.

Answer

Try this :

    DateFormat userDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); //time format from server
    DateFormat dateFormatNeeded = new SimpleDateFormat("yyyy-MM-DD"); // time format needed
    Date newDate = null;
    try {
        newDate = userDateFormat.parse(date); // this parses server date format
    } catch (ParseException e) {
        e.printStackTrace();
    }
    //this converts to required date format
    String convertedDate = dateFormatNeeded.format(newDate);
    return convertedDate;
Comments