1 Reply Latest reply: Oct 13, 2006 10:01 AM by doremifasollatido RSS

    WEEK_OF_MONTH parsing in SimpleDateFormat

    807607
      Hello,

      I am trying to use the WEEK_OF_MONTH (capital W) in SimpleDateFormat. I want to be able to do both parse() and format() of in the format "yyMMWW". I also want to use false lenient in order to have strict control.

      I have seen that the first week every month (WEEK_OF_MONTH == 1) can not be parsed properly. Below is an example program demonstrating the issue.

      The program tries 100 dates from Jan 1 2006 and onwards. It converts to the yyMMWW format (which always goes fine). I also try to convert back from the yyMMWW format (using parse() method) and see that it fails on some dates.

      I get an parse exception like this:

      ...
      060204 -> 060201 -> java.text.ParseException: Unparseable date: "060201"
      060205 -> 060201 -> java.text.ParseException: Unparseable date: "060201"
      060206 -> 060202 -> 060206
      060207 -> 060202 -> 060206
      ...


      import java.text.ParseException;
      import java.text.SimpleDateFormat;
      import java.util.Calendar;
      import java.util.Date;
      import java.util.GregorianCalendar;

      public class SimpleDateFormatIssue {

           public static void main(String[] args) throws ParseException {
                GregorianCalendar cal = new GregorianCalendar();
                cal.setFirstDayOfWeek(Calendar.MONDAY);
                cal.setLenient(false); //Strict checkin, significant for the issue
                //System.out.println(cal.getMinimalDaysInFirstWeek()); //doesnt matter what

                SimpleDateFormat fYYMMDD = new SimpleDateFormat("yyMMdd");
                fYYMMDD.setCalendar(cal);
                SimpleDateFormat fYYMMWW = new SimpleDateFormat("yyMMWW");
                fYYMMWW.setCalendar(cal);
                Date d = fYYMMDD.parse("060101");
                for (int i = 0; i < 100; i++) {
                     String outputYYMMWW = fYYMMWW.format(d);
                     Date parsedYYMMWW = null;
                     Exception exc = null;
                     try {
                          parsedYYMMWW = fYYMMWW.parse(outputYYMMWW);
                     } catch (ParseException e) {
                          exc = e;
                     }
                     System.out.println(fYYMMDD.format(d) + " -> " + outputYYMMWW + " -> " + (parsedYYMMWW != null ? fYYMMDD.format(parsedYYMMWW) : exc));
                     d = getDayAfter(cal,d);
                }
           }
           
           public static Date getDayAfter(Calendar cal, Date date) {
                cal.setTime(date);
                cal.add(Calendar.DAY_OF_MONTH,1);
                Date dayAfter = cal.getTime();
                return dayAfter;
           }
      }

      So I wonder, am I doing SimpleDateFormat abuse when I want to parse a yyMMWW formated string?

      Or perhaps I have a bug in my program... :)

      I saw some bugs in bug parade about SimpleDateFormat and WEEK_IN_MONTH but they all appears to be fixed in Java 5 which I am using:

      java -version
      java version "1.5.0_06"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
      Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode, sharing)

      Thank you in advance for any pointers.

      /Anders