SimpleDateFormat parse and format on same date string gives different results

Neilers

I've got a simple task, that is, to convert a date that's in this format: "2014-06-13" to this format: "January 6, 2013"

I've declared two SimpleDateFormat objects for this:

private SimpleDateFormat mInputFormat = new SimpleDateFormat("yyyy-MM-DD", Locale.ENGLISH);
private SimpleDateFormat mOutputFormat = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH);

So I write the code to do so:

Log.v(TAG, "orig: " + releaseDate);
try {
    Date date = mInputFormat.parse(releaseDate); // create a date object using the first pattern
    Log.v(TAG, "parsed:" + mInputFormat.format(date));
    String newDate = mOutputFormat.format(date); // format the date object into the new format
    Log.v(TAG, newDate);
    aq.id(R.id.text_2).text(newDate); // sets the date
} catch (ParseException e) {
    e.printStackTrace();
}

The thing is, the dates are completely off. Here's what's printed on the logs:

09-15 20:07:49.035: V/TwoLinerListItemView(26700): orig: 2014-06-13
09-15 20:07:49.035: V/TwoLinerListItemView(26700): parsed:2014-01-13
09-15 20:07:49.035: V/TwoLinerListItemView(26700): January 13, 2014

I am so baffled as to why these results are so different. First of all, the original string, 2014-06-13 was parsed into a date object using the input format. Then, I formatted that resulting string using the exact same inputdate format, and then and there the date was changed. It's like you parsed the string "1" then it became the integer 2, then when you made the integer back to a string it became 3. Whut.

Opiatefuchs

You used the big D in the first Format:

     new SimpleDateFormat("yyyy-MM-DD", Locale.ENGLISH);

but D is for Day in Year. You have to use dd, that´s for day in month. Change it to:

     new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why time format and parse gives different results?

simpledateformat format new Date() and then parse again return a different date

DateFormatter parse String to Date gives wrong format

DateTimeFormatter and SimpleDateFormat produce different results using same input string

Date.parse yields different results on what seems to be the same date string

Cannot parse String date with SimpleDateFormat

Parse String to Date with Different Format in Java

Parse UTC date string and convert to different format

SimpleDateFormat .format() gives different results in Java8 vs. Java11

SimpleDateFormat Parse not returning Date in required Format

How to parse a string to date and back to string with same format

Why can't this SimpleDateFormat parse this date string?

DateTimeFormatter unable to parse a Date String but SimpleDateFormat is able to

Parse and format date in string

Same code with different indentation gives different results?

SimpleDateFormat.parse() - generates wrong date for different date-formats

Different results for same date string using lubridate package in R

boost::format gives different results than round

Moment.js 2 different date strings when i parse using moment gives same value

new Date(long) gives different results

MongoDB ISODate() gives different results than Date()?

Add date in php gives different results

du gives two different results for the same file

Hashing Gives Different Results for the Same Input in SQL

Why the same styling gives different results

The same query on Sparql gives different results

Comparing same character gives different results

Why the same font size gives different results?

sizeof() gives different results for the same object