Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4104866

SimpleDateFormat.parse() does not handle fractional seconds correctly

XMLWordPrintable

    • sparc
    • solaris_2.6



      Name: diC59631 Date: 01/19/98


      When trying to parse a time that contains
      fractional seconds, but not three digits, it
      appears that java treats the number as if
      three digits, so 3.4 seconds is interpreted as
      3.004 seconds. It should either check to see
      how many digits were specified.

      import java.text.SimpleDateFormat;
      import java.text.DateFormatSymbols;
      import java.util.Date;
      import java.util.TimeZone;

      public class test {

          public static void main(String argv[]) {
              SimpleDateFormat df;
              Date date;
              String tdate = "30-MAR-1990 16:08:06.65";

              df = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss.SSSS");
              df.setTimeZone(TimeZone.getDefault());

              DateFormatSymbols dfs = df.getDateFormatSymbols();
              String upmons[] = {"JAN", "FEB", "MAR", "APR", "MAY", "JUN",
                                        "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};
              dfs = df.getDateFormatSymbols();
              dfs.setShortMonths(upmons);
              try {
                  date = df.parse(tdate);
              }
              catch(Throwable e) { System.out.println(e.getMessage()); date = null; }
              if(date != null)
                  System.out.println(tdate + " " + df.format(date));
          }

      Output:

      30-MAR-1990 16:08:06.65 30-Mar-1990 16:08:06.065

      (Review ID: 23266)
      ======================================================================

            aliusunw Alan Liu (Inactive)
            dindrigo Daniel Indrigo (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: