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

SimpleDateFormat formats milliseconds incorrectly with width of 1 or 2

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • None
    • 1.4.0
    • core-libs
    • generic
    • generic



      Name: bsT130419 Date: 10/19/2001


      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24)
      Java HotSpot(TM) Client VM (build 1.3.1-b24, mixed mode)

      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta2-b77)
      Java HotSpot(TM) Client VM (build 1.4.0-beta2-b77, mixed mode)

      The following code prints times with various precision in the milliseconds part.
      In Java 1.2, the requested width is respected. In java 1.3 and later, leading
      zeros are omitted to reduce the value to the desired width, yielding e.g.
      01.01.1970 01:00:00.1 instead of
      01.01.1970 01:00:00.0 for the time
      01.01.1970 01:00:00.001.

      Sample code:
      import java.text.SimpleDateFormat;
      import java.util.Date;

      public class TestDateFormat
      {
          public static void main(String[] args) {
              long[] times = new long[] { 0, 1, 99, 100, 101 };

              try {
              System.out.println("One digit milliseconds:");
              SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss.S");
              for (int i=0; i < times.length; i++) {
                  long t = times[i];
                  System.out.println(t+": "+sdf.format(new Date(t)));
              }

              System.out.println("Two digit milliseconds:");
              sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss.SS");
              for (int i=0; i < times.length; i++) {
                  long t = times[i];
                  System.out.println(t+": "+sdf.format(new Date(t)));
              }

              System.out.println("Three digit milliseconds:");
              sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss.SSS");
              for (int i=0; i < times.length; i++) {
                  long t = times[i];
                  System.out.println(t+": "+sdf.format(new Date(t)));
              }
              } catch(Exception e) {
                  System.out.println(e);
              }
          }
      }

      Output on JDK 1.2.2:
      One digit milliseconds:
      0: 01.01.1970 01:00:00.0
      1: 01.01.1970 01:00:00.0
      99: 01.01.1970 01:00:00.0
      100: 01.01.1970 01:00:00.1
      101: 01.01.1970 01:00:00.1
      Two digit milliseconds:
      0: 01.01.1970 01:00:00.00
      1: 01.01.1970 01:00:00.00
      99: 01.01.1970 01:00:00.09
      100: 01.01.1970 01:00:00.10
      101: 01.01.1970 01:00:00.10
      Three digit milliseconds:
      0: 01.01.1970 01:00:00.000
      1: 01.01.1970 01:00:00.001
      99: 01.01.1970 01:00:00.099
      100: 01.01.1970 01:00:00.100
      101: 01.01.1970 01:00:00.101

      Output on JDK 1.3.1, 1.4:
      One digit milliseconds:
      0: 01.01.1970 01:00:00.0
      1: 01.01.1970 01:00:00.1
      99: 01.01.1970 01:00:00.99
      100: 01.01.1970 01:00:00.100
      101: 01.01.1970 01:00:00.101
      Two digit milliseconds:
      0: 01.01.1970 01:00:00.00
      1: 01.01.1970 01:00:00.01
      99: 01.01.1970 01:00:00.99
      100: 01.01.1970 01:00:00.100
      101: 01.01.1970 01:00:00.101
      Three digit milliseconds:
      0: 01.01.1970 01:00:00.000
      1: 01.01.1970 01:00:00.001
      99: 01.01.1970 01:00:00.099
      100: 01.01.1970 01:00:00.100
      101: 01.01.1970 01:00:00.101
      (Review ID: 134048)
      ======================================================================

            nlindenbsunw Norbert Lindenberg (Inactive)
            bstrathesunw Bill Strathearn (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: