JDK-4516915 : SimpleDateFormat formats milliseconds incorrectly with width of 1 or 2
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.text
  • Affected Version: 1.4.0
  • Priority: P3
  • Status: Closed
  • Resolution: Not an Issue
  • OS: generic
  • CPU: generic
  • Submitted: 2001-10-19
  • Updated: 2001-10-23
  • Resolved: 2001-10-23
Related Reports
Relates :  
Description

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) 
======================================================================

Comments
WORK AROUND Name: bsT130419 Date: 10/19/2001 I will always specify SSS from now on. ======================================================================
11-06-2004

EVALUATION 1.3 fixed a longstanding bug in the handling of millisecond formatting (the 'S' pattern character). The specification says that, for numbers, "The count of pattern letters determine[s] ... the minimum number of digits". This implies that more digits will be used if that's necessary to express larger values. ###@###.### 2001-10-23
23-10-2001