14 Replies Latest reply: Aug 27, 2009 7:40 PM by 807580 RSS

    Unexpected SimpleDateFormat behavior.

    807580
      Failing test case:
      import static org.junit.Assert.fail;
      
      import java.text.DateFormat;
      import java.text.ParseException;
      import java.text.SimpleDateFormat;
      
      import org.junit.Test;
      
      
      public class DateFormatTests {
      
          @Test
          public void testRequiredTwoDigitDay() {
              DateFormat df = new SimpleDateFormat("yyyyMMdd");
              df.setLenient(false);
              try {
                  df.parse("2009081");
                  fail();
              } 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.