Diego Rivera
Diego Rivera

Reputation: 433

SimpleDateFormat getting wrong day

Ok, my backend is sending me this date: 2015-10-23 17:28:35.932620000

In my android app I have an object that holds all the info (including this date). This is the setter:

public void setCreatedDate(String pDateAsString)
{
    /** Intenta convertir la fecha cargada */
    try {
        /** Obtiene la hora que viene de parametro como un Date */
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", IConstants.LOCALE);
        mFineCreatedDate = dateFormat.parse(pDateAsString);                 
    } catch (java.text.ParseException e) {
        e.printStackTrace();
    }           
}

This is the getter:

public String getCreatedDateAsString()
{
    DateFormat dateFormat = new SimpleDateFormat("EEEE FF/MMM/yyyy hh:mm aa",Locale.getDefault());
    String dateString = dateFormat.format(mFineCreatedDate);
    return dateString.substring(0, 1).toUpperCase(Locale.getDefault()) + dateString.substring(1).toLowerCase(Locale.getDefault());
}

Which is formatted to be showed in a more-readable way for the users. However this is happening:

A date like 2015-05-04 08:51:32 is being displayed Monday 01/may/2015 08:51am

As you can see the day is wrong, and I really don't know why. As for me (based on the documentation of SimpleDateFormat) everything is fine.

Upvotes: 2

Views: 1739

Answers (2)

Ayush
Ayush

Reputation: 173

It's working try to parse your date like this.

String dtStart = "11/08/2013 08:48:10";
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
try {
    date = format.parse(dtStart);
    System.out.println("Date ->" + date);
} catch (Exception e) {
    e.printStackTrace();
}

Upvotes: 0

ac0de
ac0de

Reputation: 260

You should change the "FF" (day of week in month) to "dd" (day in month). For more info refer to this link.

Upvotes: 4

Related Questions