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?
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.
You want "dd" (day in month), not "DD" (day in year):
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.GERMAN);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With