JDK-4624709 : Doc: document Date.toString not supporting BC dates
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util:i18n
  • Affected Version: 1.4.0
  • Priority: P5
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2002-01-16
  • Updated: 2003-09-09
  • Resolved: 2003-09-09
Related Reports
Duplicate :  
Description

Name: jl125535			Date: 01/16/2002


FULL PRODUCT VERSION :
java version "1.4.0-beta3"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta3-b84)
Java HotSpot(TM) Client VM (build 1.4.0-beta3-b84, mixed mode)

java version "1.3.1_01"
Java(TM) 2 Runtime Environment, Standard Edition (build
1.3.1_01a)
Java HotSpot(TM) Client VM (build 1.3.1_01, mixed mode)

java version "1.2.2"
Solaris VM (build Solaris_JDK_1.2.2_07, native threads, sunwjit)

FULL OPERATING SYSTEM VERSION :
Microsoft Windows XP [Version 5.1.2600]

ADDITIONAL OPERATING SYSTEMS :
Solaris 5.8



A DESCRIPTION OF THE PROBLEM :
The java.util.Date(long) constructor produces incorrect Date
values for negative numbers smaller than -111081607200000 so
that there exists a negative number for which the resulting
date is the same as the correct positive number.

For example, both Date(1010686408060) and
Date(-125313284792000 ) produce Thu Jan 10 12:13:28 CST 2002.

In 1.4 beta 3 and 1.3.1 on Solaris 8, the code produces a compiler error.

It appears that the Date(long) constructor has some restrictions
on the domain of the parameter.  It would be helpful if the javadoc
(and the compiler) mentioned those restrictions.

The acceptable range for long integer types is at
"http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#85587".

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. create a java.util.Date object using the (long)
constructor with a negative value smaller than -111081607200000.
2. Date object created is not correct (or a compiler error results).


EXPECTED VERSUS ACTUAL BEHAVIOR :
using -111081607200000
produces Sat Jan 01 00:00:00 CST 1551 which is correct
using -111081607200001
produces Fri Dec 31 23:59:59 CST 1552 which is incorrect
the Correct value should be sometime before 1551 rather than
after

In 1.4 beta 3 and 1.3.1 on Solaris 8, the code produces a compiler error.

% javac *.java
DateTest.java:6: integer number too large: -111081607200000
            Date argDate1 = new Date(-111081607200000);
                                      ^
DateTest.java:7: integer number too large: -111081607200001
            Date argDate2 = new Date(-111081607200001);


This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.util.*;

public class test {

    public static void main(String args[]) {
            Date argDate1 = new Date(-111081607200000);
            Date argDate2 = new Date(-111081607200001);
            System.out.println("arg date1: " + argDate1);
            System.out.println("arg date2: " + argDate2);
    }
}

---------- END SOURCE ----------
(Review ID: 138158) 
======================================================================

Comments
SUGGESTED FIX *** Date.java Wed Apr 3 02:03:20 2002 --- Date.java.fixed Wed Mar 13 23:14:27 2002 *************** *** 76,82 **** * <ul> * <li>A year <i>y</i> is represented by the integer * <i>y</i>&nbsp;<code>-&nbsp;1900</code>. ! * <li>A month is represented by an integer from 0 to 11; 0 is January, * 1 is February, and so forth; thus 11 is December. * <li>A date (day of month) is represented by an integer from 1 to 31 * in the usual manner. --- 76,82 ---- * <ul> * <li>A year <i>y</i> is represented by the integer * <i>y</i>&nbsp;<code>-&nbsp;1900</code>. ! * <li>A month is represented by an integer form 0 to 11; 0 is January, * 1 is February, and so forth; thus 11 is December. * <li>A date (day of month) is represented by an integer from 1 to 31 * in the usual manner. *************** *** 96,101 **** --- 96,112 ---- * In all cases, arguments given to methods for these purposes need * not fall within the indicated ranges; for example, a date may be * specified as January 32 and is interpreted as meaning February 1. + * <p> + + * This <code>Date</code> class supports the Gregorian and Julian + * calendar systems with the default cutover date. See {@link + * GregorianCalendar} for details. The formatting methods, such as + * {@link #toString}, do not support the era information and BC (BCE) + * dates are formatted with a positive year number. If it is required + * to format BC dates to be distinguished from AD (CE) dates, use the + * {@link java.text.SimpleDateFormat SimpleDateFormat} class to + * include the era information in output. + * * @author James Gosling * @author Arthur van Hoff *************** *** 944,951 **** /** * Converts this <code>Date</code> object to a <code>String</code> * of the form: ! * <blockquote><pre> ! * dow mon dd hh:mm:ss zzz yyyy</pre></blockquote> * where:<ul> * <li><tt>dow</tt> is the day of the week (<tt>Sun, Mon, Tue, Wed, * Thu, Fri, Sat</tt>). --- 955,962 ---- /** * Converts this <code>Date</code> object to a <code>String</code> * of the form: ! * <blockquote> ! * dow mon dd hh:mm:ss zzz yyyy</blockquote> * where:<ul> * <li><tt>dow</tt> is the day of the week (<tt>Sun, Mon, Tue, Wed, * Thu, Fri, Sat</tt>). *************** *** 966,975 **** --- 977,991 ---- * that is, it consists of no characters at all. * <li><tt>yyyy</tt> is the year, as four decimal digits. * </ul> + * <p> + * The format does not include any era information. Use the {@link + * java.text.SimpleDateFormat SimpleDateFormat} class to format BC + * (BCE) dates. * * @return a string representation of this date. * @see java.util.Date#toLocaleString() * @see java.util.Date#toGMTString() + * @see java.text.DateFormat */ public String toString() { DateFormat formatter = null; *************** *** 1030,1035 **** --- 1046,1055 ---- * Greenwich Mean Time. * </ul><p> * The result does not depend on the local time zone. + * <p> + * The format does not include any era information. Use the {@link + * java.text.SimpleDateFormat SimpleDateFormat} class to format BC + * (BCE) dates. * * @return a string representation of this date, using the Internet GMT * conventions. ###@###.### 2002-04-19
19-04-2002

EVALUATION The compile-time error is specified by section 3.10.1 of the Java Language Specification. Since the values don't have an 'L' or 'l' suffix, they are interpreted as type int, and the values given are too large for that type. The possibly surprising date strings are caused by to the fact that Date.toString doesn't include the era, and so it's not obvious that most of the dates used in this test case are BC. Maybe the documentation should explicitly mention that limitation. ###@###.### 2002-01-16 Date(-111081607200000L) is BC 1552/12/31 10:00:00.000 (Julian). Date.toString() uses the format which doesn't include the era as documented its API doc. Use SimpleDateFormat to format BC dates. ###@###.### 2002-03-13 The formatting issue of Date.toString will be addressed in 4484483. ###@###.### 2003-09-09
13-03-2002