classpath
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

java.util.Date mess


From: Jeroen Frijters
Subject: java.util.Date mess
Date: Mon, 11 Oct 2004 15:39:31 +0200

Hi,

I have some code that depends on unspecified behavior of java.util.Date.
In the Sun JDK if you pass out of range parameters to the Date
constructors that take year, month, date, etc. it will automatically
convert to a valid date (for example, new Date(104, 8, 41) is Oct 11,
2004.)

I have a rather lame patch that addresses this (see below), would anyone
object to this?

Regards,
Jeroen

Index: java/util/Date.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/util/Date.java,v
retrieving revision 1.17
diff -u -r1.17 Date.java
--- java/util/Date.java 23 Apr 2004 20:34:11 -0000      1.17
+++ java/util/Date.java 11 Oct 2004 12:37:23 -0000
@@ -93,7 +93,7 @@
    */
   public Date(int year, int month, int day)
   {
-    time = new GregorianCalendar(year + 1900, month,
day).getTimeInMillis();
+    this(year, month, day, 0, 0, 0);
   }
 
   /**
@@ -103,9 +103,7 @@
    */
   public Date(int year, int month, int day, int hour, int min)
   {
-    time =
-      new GregorianCalendar(year + 1900, month, day, hour,
-                           min).getTimeInMillis();
+    this(year, month, day, hour, min, 0);
   }
 
   /**
@@ -115,9 +113,13 @@
    */
   public Date(int year, int month, int day, int hour, int min, int sec)
   {
-    time =
-      new GregorianCalendar(year + 1900, month, day, hour, min,
-                           sec).getTimeInMillis();
+    GregorianCalendar cal = new GregorianCalendar(year + 1900, 0, 1);
+    cal.add(GregorianCalendar.MONTH, month);
+    cal.add(GregorianCalendar.DAY_OF_MONTH, day - 1);
+    cal.add(GregorianCalendar.HOUR, hour);
+    cal.add(GregorianCalendar.MINUTE, min);
+    cal.add(GregorianCalendar.SECOND, sec);
+    time = cal.getTimeInMillis();
   }
 
   /**




reply via email to

[Prev in Thread] Current Thread [Next in Thread]