Thursday, August 27, 2009

Unexpected SimpleDateFormat Behavior

Failing test case:

import static org.junit.Assert.*;
import java.text.*;
import org.junit.Test;

public class DateFormatTests {

@Test
public void testRequiredTwoDigitDay() {
DateFormat df = new SimpleDateFormat("yyyyMMdd");
df.setLenient(false);
try {
df.parse("2009081");
fail("Expected two-digit day");
} catch (ParseException e) {
e.printStackTrace();
}
}
}

Argh.

SimpleDateFormat API Javadocs
:
For parsing, the number of pattern letters is ignored unless it's needed to separate two adjacent fields.
Tricksy API javadocs. We hatessss it forever.

No comments:

Post a Comment