I have this unit test that is failing and I would like to get it to pass:
@Test
public void testDateFormatsWithNo() throws Exception {
List<String> dateStrings = Lists.newArrayList("2014-08-02T22:21:32Z", "2014-05-27T17:11:55.597Z");
try {
String twoMillis = "yyyy-MM-dd'T'HH:mm:ss'Z'";
for (String dateString : dateStrings) {
DateTimeFormat.forPattern(twoMillis).parseDateTime(dateString);
}
} catch (Exception e) {
fail(e.getMessage());
}
}
@Test
public void testDateFormatsWithThreeMillis() throws Exception {
List<String> dateStrings = Lists.newArrayList("2014-08-02T22:21:32Z", "2014-05-27T17:11:55.597Z");
try {
String threeMillis = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
for (String dateString : dateStrings) {
DateTimeFormat.forPattern(threeMillis).parseDateTime(dateString);
}
} catch (Exception e) {
fail(e.getMessage());
}
}
It fails with these error messages, respectively:
java.lang.AssertionError: Invalid format: "2014-05-27T17:11:55.597Z" is malformed at ".597Z"
java.lang.AssertionError: Invalid format: "2014-08-02T22:21:32Z" is malformed at "Z"
I want to know if there is a single date format pattern I can use to parse both of these date strings. As you can see, some strings have 2 milliseconds and some have 3. Is this possible?