Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SimpleDateFormatter does not recognize months

Tags:

java

date

parsing

I want to parse a date string but I fail miserably. To illustrate my problem I wrote this simple JUnit test:

@Test
public void testParseJavaDate() throws ParseException {     
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD_HH-mm-ss", Locale.GERMAN);

    String inputtime = "2011-04-21_16-01-08";
    Date parse = sdf.parse(inputtime);

    assertEquals(inputtime,sdf.format(parse));
}

This test fails with this message:

org.junit.ComparisonFailure: expected:<2011-0[4]-21_16-01-08> but was:<2011-0[1]-21_16-01-08>

I don't get why the formatter cannot parse the date correctly. Do you have any ideas?

like image 218
nemoo Avatar asked May 13 '11 13:05

nemoo


2 Answers

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.GERMAN);

String inputtime = "2011-04-21_16-01-08";
Date parse = sdf.parse(inputtime);

use dd instead of DD.

like image 104
A B Avatar answered Sep 23 '22 16:09

A B


You want "dd" (day in month), not "DD" (day in year):

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.GERMAN);
like image 30
duffymo Avatar answered Sep 21 '22 16:09

duffymo