OSDN Git Service

2004-05-05 Tom Tromey <tromey@redhat.com>
[pf3gnuchains/gcc-fork.git] / libjava / java / util / Date.java
index 3d23780..43ae969 100644 (file)
-/* Copyright (C) 1998, 1999  Cygnus Solutions
+/* java.util.Date
+   Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING.  If not, write to the
+Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+02111-1307 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version. */
 
-   This file is part of libgcj.
-
-This software is copyrighted work licensed under the terms of the
-Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
-details.  */
 
 package java.util;
-import java.text.*;
 
 /**
+ * This class represents a specific time in milliseconds since the epoch.
+ * The epoch is 1970, January 1 00:00:00.0000 UTC.  
+ *
+ * Date is intended to reflect universal time coordinate (UTC), but doesn't
+ * handle the leap seconds.
+ *
+ * Prior to jdk 1.1 this class was the sole Time class and had also 
+ * calendar functionality.  But this can't be localized, so a new Calendar
+ * class was created, that you should use instead.  The functions which
+ * get or return a year, month, day etc. are all deprecated and shouldn't be
+ * used.  Use Calendar instead.
+ * 
+ * @see Calendar
+ * @see GregorianCalendar
+ * @see java.text.DateFormat
+ * @author Jochen Hoenicke
  * @author Per Bothner <bothner@cygnus.com>
- * @date October 24, 1998.
  */
+public class Date implements Cloneable, Comparable, java.io.Serializable
+{
+  /**
+   * This is the serialization UID for this class
+   */
+  private static final long serialVersionUID = 7523967970034938905L;
+
+  /**
+   * The time in milliseconds since the epoch.
+   */
+  private transient long time;
+
+  /**
+   * Creates a new Date Object representing the current time.
+   */
+  public Date()
+  {
+    time = System.currentTimeMillis();
+  }
 
-/* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3,
- * "The Java Language Specification", ISBN 0-201-63451-1,
- * and O'Reilly's "Java in a Nutshell".
- * Status:  Need to re-write toString().
- *   Missing:  ToGMTString and toLocaleString.
- *   Serialization spec:  Specifies readObject/writeObject.
- */
+  /**
+   * Creates a new Date Object representing the given time.
+   * @param time the time in milliseconds since the epoch.
+   */
+  public Date(long time)
+  {
+    this.time = time;
+  }
 
-public class Date implements java.io.Serializable, Cloneable
-{
-  private long millis;
+  /**
+   * Creates a new Date Object representing the given time.
+   * @deprecated use <code>new GregorianCalendar(year+1900, month,
+   * day)</code> instead.  
+   */
+  public Date(int year, int month, int day)
+  {
+    time = new GregorianCalendar(year + 1900, month, day).getTimeInMillis();
+  }
 
-  public Date() { millis = System.currentTimeMillis(); }
+  /**
+   * Creates a new Date Object representing the given time.
+   * @deprecated use <code>new GregorianCalendar(year+1900, month,
+   * day, hour, min)</code> instead.  
+   */
+  public Date(int year, int month, int day, int hour, int min)
+  {
+    time =
+      new GregorianCalendar(year + 1900, month, day, hour,
+                           min).getTimeInMillis();
+  }
 
-  public Date(long millis) { this.millis = millis; }
+  /**
+   * Creates a new Date Object representing the given time.
+   * @deprecated use <code>new GregorianCalendar(year+1900, month,
+   * day)</code> instead.  
+   */
+  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();
+  }
 
-  public Date(int year, int month, int date, int hours,
-             int minutes, int seconds)
+  /**
+   * Creates a new Date from the given string representation.  This
+   * does the same as <code>new Date(Date.parse(s))</code>
+   * @see #parse
+   * @deprecated use <code>java.text.DateFormat.parse(s)</code> instead.  
+   */
+  public Date(String s)
   {
-    setTime(year, month, date, hours, minutes, seconds);
+    time = parse(s);
   }
 
-  public Date(int year, int month, int date, int hours, int minutes)
+  public Object clone()
   {
-    setTime(year, month, date, hours, minutes, 0);
+    try
+      {
+       return super.clone();
+      }
+    catch (CloneNotSupportedException ex)
+      {
+       return null;
+      }
   }
 
-  public Date(int year, int month, int date)
+  /**
+   * @deprecated Use Calendar with a UTC TimeZone instead.
+   * @return the time in millis since the epoch.
+   */
+  public static long UTC(int year, int month, int date,
+                        int hrs, int min, int sec)
   {
-    setTime(year, month, date, 0, 0, 0);
+    GregorianCalendar cal =
+      new GregorianCalendar(year + 1900, month, date, hrs, min, sec);
+    cal.set(Calendar.ZONE_OFFSET, 0);
+    cal.set(Calendar.DST_OFFSET, 0);
+    return cal.getTimeInMillis();
   }
 
-  public Date (String s) { this(parse(s)); }
+  /**
+   * Gets the time represented by this Object
+   * @return the time in milliseconds since the epoch.
+   */
+  public long getTime()
+  {
+    return time;
+  }
 
-  private static int skipParens(String string, int offset)
+  /**
+   * @deprecated use
+   * Calendar.get(Calendar.ZONE_OFFSET)+Calendar.get(Calendar.DST_OFFSET)
+   * instead.
+   * @return The time zone offset in minutes of the local time zone
+   * relative to UTC.  The time represented by this object is used to
+   * determine if we should use daylight savings.
+   */
+  public int getTimezoneOffset()
   {
-    int len = string.length();
-    int p = 0;
-    int i;
+    Calendar cal = Calendar.getInstance();
+    cal.setTimeInMillis(time);
+    return (cal.get(Calendar.ZONE_OFFSET)
+           + cal.get(Calendar.DST_OFFSET)) / (60 * 1000);
+  }
 
-    for (i = offset; i < len; ++i)
-      {
-       if (string.charAt(i) == '(')
-         ++p;
-       else if (string.charAt(i) == ')')
-         {
-           --p;
-           if (p == 0)
-             return i + 1;
-           // If we've encounted unbalanced parens, just return the
-           // leftover one as an ordinary character.  It will be
-           // caught later in parsing and cause an
-           // IllegalArgumentException.
-           if (p < 0)
-             return i;
-         }
-      }
+  /**
+   * Sets the time which this Object should represented.
+   * @param time the time in milliseconds since the epoch.  */
+  public void setTime(long time)
+  {
+    this.time = time;
+  }
+
+  /**
+   * Tests if this date is after the specified date.
+   * @param when the other date
+   * @return true, if the date represented by this Object is
+   * strictly later than the time represented by when.  
+   */
+  public boolean after(Date when)
+  {
+    return time > when.time;
+  }
+
+  /**
+   * Tests if this date is before the specified date.
+   * @param when the other date
+   * @return true, if the date represented by when is strictly later
+   * than the time represented by this object.
+   */
+  public boolean before(Date when)
+  {
+    return time < when.time;
+  }
+
+  /**
+   * Compares two dates for equality.
+   * @param obj the object to compare.
+   * @return true, if obj is a Date object and the date represented
+   * by obj is exactly the same as the time represented by this
+   * object.  
+   */
+  public boolean equals(Object obj)
+  {
+    return (obj instanceof Date && time == ((Date) obj).time);
+  }
+
+  /**
+   * Compares two dates.
+   * @param when the other date.
+   * @return 0, if the date represented
+   * by obj is exactly the same as the time represented by this
+   * object, a negative if this Date is before the other Date, and
+   * a positive value otherwise.  
+   */
+  public int compareTo(Date when)
+  {
+    return (time < when.time) ? -1 : (time == when.time) ? 0 : 1;
+  }
+
+  /**
+   * Compares this Date to another.  This behaves like
+   * <code>compareTo(Date)</code>, but it may throw a
+   * <code>ClassCastException</code>
+   * @param obj the other date.
+   * @return 0, if the date represented
+   * by obj is exactly the same as the time represented by this
+   * object, a negative if this Date is before the other Date, and
+   * a positive value otherwise.  
+   * @exception ClassCastException if obj is not of type Date.
+   */
+  public int compareTo(Object obj)
+  {
+    return compareTo((Date) obj);
+  }
 
-    // Not sure what to do if `p != 0' here.
-    return i;
+  public int hashCode()
+  {
+    return (int) time ^ (int) (time >>> 32);
+  }
+
+  private static final String[] weekNames = { "Sun", "Mon", "Tue", "Wed",
+                                             "Thu", "Fri", "Sat" };
+
+  private static final String[] monthNames = { "Jan", "Feb", "Mar", "Apr",
+                                              "May", "Jun", "Jul", "Aug",
+                                              "Sep", "Oct", "Nov", "Dec" };
+
+  public String toString()
+  {
+    Calendar cal = Calendar.getInstance();
+    cal.setTimeInMillis(time);
+    String day = "0" + cal.get(Calendar.DATE);
+    String hour = "0" + cal.get(Calendar.HOUR_OF_DAY);
+    String min = "0" + cal.get(Calendar.MINUTE);
+    String sec = "0" + cal.get(Calendar.SECOND);
+    String year = "000" + cal.get(Calendar.YEAR);
+    return weekNames[cal.get(Calendar.DAY_OF_WEEK) - 1] + " "
+      + monthNames[cal.get(Calendar.MONTH)] + " "
+      + day.substring(day.length() - 2) + " "
+      + hour.substring(hour.length() - 2) + ":"
+      + min.substring(min.length() - 2) + ":"
+      + sec.substring(sec.length() - 2) + " "
+      +
+      cal.getTimeZone().getDisplayName(cal.getTimeZone().inDaylightTime(this),
+                                      TimeZone.SHORT) + " " +
+      year.substring(year.length() - 4);
+  }
+
+  /** Format this object in a locale-specific way.
+   * @deprecated Use DateFormat.format(Date)
+   */
+  public String toLocaleString()
+  {
+    return java.text.DateFormat.getInstance().format(this);
+  }
+
+  /** Format this object in a standard format in the GMT timezone.
+   * @deprecated Use DateFormat.format(Date) with a GMT TimeZone.
+   */
+  public String toGMTString()
+  {
+    java.text.DateFormat format = java.text.DateFormat.getInstance();
+    format.setTimeZone(TimeZone.getTimeZone("GMT"));
+    return format.format(this);
   }
 
   private static int parseTz(String tok, char sign)
@@ -134,6 +362,10 @@ public class Date implements java.io.Serializable, Cloneable
     return false;
   }
 
+  /** Parse a String and return the time it represents.
+   * @param s The String to parse.
+   * @deprecated Use DateFormat.parse(String)
+   */
   public static long parse(String string)
   {
     // Initialize date/time fields before parsing begins.
@@ -148,20 +380,25 @@ public class Date implements java.io.Serializable, Cloneable
 
     // Trim out any nested stuff in parentheses now to make parsing easier.
     StringBuffer buf = new StringBuffer();
-    int off = 0;
-    int openParenOffset, tmpMonth;
-    while ((openParenOffset = string.indexOf('(', off)) >= 0)
+    int parenNesting = 0;
+    int len = string.length();
+    for (int i = 0;  i < len;  i++)
       {
-       // Copy part of string leading up to open paren.
-       buf.append(string.substring(off, openParenOffset));
-       off = skipParens(string, openParenOffset);
+       char ch = string.charAt(i);
+       if (ch >= 'a' && ch <= 'z')
+         ch -= 'a' - 'A';
+       if (ch == '(')
+         parenNesting++;
+       else if (parenNesting == 0)
+         buf.append(ch);
+       else if (ch == ')')
+         parenNesting--;
       }
-    buf.append(string.substring(off));
+    int tmpMonth;
 
     // Make all chars upper case to simplify comparisons later.
     // Also ignore commas; treat them as delimiters.
-    StringTokenizer strtok =
-      new StringTokenizer(buf.toString().toUpperCase(), " \t\n\r,");
+    StringTokenizer strtok = new StringTokenizer(buf.toString(), " \t\n\r,");
 
     while (strtok.hasMoreTokens())
       {
@@ -176,56 +413,58 @@ public class Date implements java.io.Serializable, Cloneable
          {
            while (tok != null && tok.length() > 0)
              {
-               // A colon or slash may be valid in the number.
-               // Find the first of these before calling parseInt.
-               int colon = tok.indexOf(':');
-               int slash = tok.indexOf('/');
-               int hyphen = tok.indexOf('-');
-               // We choose tok.length initially because it makes
-               // processing simpler.
-               int punctOffset = tok.length();
-               if (colon >= 0)
-                 punctOffset = Math.min(punctOffset, colon);
-               if (slash >= 0)
-                 punctOffset = Math.min(punctOffset, slash);
-               if (hyphen >= 0)
-                 punctOffset = Math.min(punctOffset, hyphen);
-               // Following code relies on -1 being the exceptional
-               // case.
-               if (punctOffset == tok.length())
-                 punctOffset = -1;
-
-               int num;
-               try
-                 {
-                   num = Integer.parseInt(punctOffset < 0 ? tok :
-                                          tok.substring(0, punctOffset));
-                 }
-               catch (NumberFormatException ex)
-                 {
-                   throw new IllegalArgumentException(tok);
-                 }
-
-               // TBD: Spec says year can be followed by a slash.  That might
-               // make sense if using YY/MM/DD formats, but it would fail in
-               // that format for years <= 70.  Also, what about 1900?  That
-               // is interpreted as the year 3800; seems that the comparison
-               // should be num >= 1900 rather than just > 1900.
-               // What about a year of 62 - 70?  (61 or less could be a (leap)
-               // second).  70/MM/DD cause an exception but 71/MM/DD is ok
-               // even though there's no ambiguity in either case.
-               // For the parse method, the spec as written seems too loose.
-               // Until shown otherwise, we'll follow the spec as written.
-               if (num > 70 && (punctOffset < 0 || punctOffset == slash))
-                 year = num > 1900 ? num - 1900 : num;
-               else if (punctOffset > 0 && punctOffset == colon)
+               int punctOffset = tok.length();
+               int num = 0;
+               int punct;
+               for (int i = 0;  ;  i++)
+                 {
+                   if (i >= punctOffset)
+                     {
+                       punct = -1;
+                       break;
+                     }
+                   else
+                     {
+                       punct = tok.charAt(i);
+                       if (punct >= '0' && punct <= '9')
+                         {
+                           if (num > 999999999) // in case of overflow
+                             throw new IllegalArgumentException(tok);
+                           num = 10 * num + (punct - '0');
+                         }
+                       else
+                         {
+                           punctOffset = i;
+                           break;
+                         }
+                     }
+                     
+                 }
+
+               if (punct == ':')
                  {
                    if (hour < 0)
                      hour = num;
                    else
                      minute = num;
                  }
-               else if (punctOffset > 0 && punctOffset == slash)
+               else if ((num >= 70
+                         && (punct == ' ' || punct == ','
+                             || punct == '/' || punct < 0))
+                        || (num < 70 && day >= 0 && month >= 0 && year < 0))
+                 {
+                   if (num >= 100)
+                     year = num;
+                   else
+                     {
+                       int curYear = 1900 + new Date().getYear();
+                       int firstYear = curYear - 80;
+                       year = firstYear / 100 * 100 + num;
+                       if (year < firstYear)
+                         year += 100;
+                     }
+                 }
+               else if (punct == '/')
                  {
                    if (month < 0)
                      month = num - 1;
@@ -242,7 +481,7 @@ public class Date implements java.io.Serializable, Cloneable
                  throw new IllegalArgumentException(tok);
 
                // Advance string if there's more to process in this token.
-               if (punctOffset < 0 || punctOffset + 1 >= tok.length())
+               if (punct < 0 || punctOffset + 1 >= tok.length())
                  tok = null;
                else
                  tok = tok.substring(punctOffset + 1);
@@ -313,148 +552,221 @@ public class Date implements java.io.Serializable, Cloneable
          throw new IllegalArgumentException(tok);
       }
 
-    // Unspecified minutes and seconds should default to 0.
+    // Unspecified hours, minutes, or seconds should default to 0.
+    if (hour < 0)
+      hour = 0;
     if (minute < 0)
       minute = 0;
     if (second < 0)
       second = 0;
 
     // Throw exception if any other fields have not been recognized and set.
-    if (year < 0 || month < 0 || day < 0 || hour < 0)
+    if (year < 0 || month < 0 || day < 0)
       throw new IllegalArgumentException("Missing field");
 
     // Return the time in either local time or relative to GMT as parsed.
     // If no time-zone was specified, get the local one (in minutes) and
     // convert to milliseconds before adding to the UTC.
-    return UTC(year, month, day, hour, minute, second) + (localTimezone ?
-               new Date(year, month, day).getTimezoneOffset() * 60 * 1000:
-               -timezone * 60 * 1000);
-  }
-
-  public boolean after (Date when) { return this.millis > when.millis; }
-  public boolean before (Date when) { return this.millis < when.millis; }
-
-  public boolean equals(Object obj)
-  {
-    return (obj != null && obj instanceof Date
-           && ((Date)obj).millis == this.millis);
-  }
-
-  public long getTime() { return millis; }
-
-  public int hashCode()
-  {
-    return (int)(millis^(millis>>>32));
-  }
-
-  private void setTime(int year, int month, int date,
-                      int hours, int minutes, int seconds)
-  {
-    Calendar cal = new GregorianCalendar(year+1900, month, date,
-                                        hours, minutes, seconds);
-    millis = cal.getTimeInMillis();
-  }
-
-  public void setTime(long millis) { this.millis = millis; }
-
-  private int getField (int fld)
-  {
-    Calendar cal = new GregorianCalendar();
-    cal.setTime(this);
-    return cal.get(fld);
-  }
-
-  public int getYear ()
-  {
-    return getField(Calendar.YEAR) - 1900;
+    GregorianCalendar cal
+      = new GregorianCalendar(year, month, day, hour, minute, second);
+    if (!localTimezone)
+      {
+       cal.set(Calendar.ZONE_OFFSET, timezone * 60 * 1000);
+       cal.set(Calendar.DST_OFFSET, 0);
+      }
+    return cal.getTimeInMillis();
   }
 
-  public int getMonth ()
+  /**
+   * @return the year minus 1900 represented by this date object.
+   * @deprecated Use Calendar instead of Date, and use get(Calendar.YEAR)
+   * instead.  Note about the 1900 difference in year.
+   */
+  public int getYear()
   {
-    return getField(Calendar.MONTH);
+    Calendar cal = Calendar.getInstance();
+    cal.setTimeInMillis(time);
+    return cal.get(Calendar.YEAR) - 1900;
   }
 
-  public int getDate ()
+  /**
+   * Sets the year to year minus 1900, not changing the other fields.
+   * @param year the year minus 1900.
+   * @deprecated Use Calendar instead of Date, and use
+   * set(Calendar.YEAR, year) instead.  Note about the 1900
+   * difference in year.  
+   */
+  public void setYear(int year)
   {
-    return getField(Calendar.DATE);
+    Calendar cal = Calendar.getInstance();
+    cal.setTimeInMillis(time);
+    cal.set(Calendar.YEAR, 1900 + year);
+    time = cal.getTimeInMillis();
   }
 
-  public int getDay ()
+  /**
+   * @return the month represented by this date object (zero based).
+   * @deprecated Use Calendar instead of Date, and use get(Calendar.MONTH)
+   * instead.
+   */
+  public int getMonth()
   {
-    return getField(Calendar.DAY_OF_WEEK) - 1;
+    Calendar cal = Calendar.getInstance();
+    cal.setTimeInMillis(time);
+    return cal.get(Calendar.MONTH);
   }
 
-  public int getHours ()
+  /**
+   * Sets the month to the given value, not changing the other fields.
+   * @param month the month, zero based.
+   * @deprecated Use Calendar instead of Date, and use
+   * set(Calendar.MONTH, month) instead. 
+   */
+  public void setMonth(int month)
   {
-    return getField(Calendar.HOUR_OF_DAY);
+    Calendar cal = Calendar.getInstance();
+    cal.setTimeInMillis(time);
+    cal.set(Calendar.MONTH, month);
+    time = cal.getTimeInMillis();
   }
 
-  public int getMinutes ()
+  /**
+   * @return the day of month represented by this date object.
+   * @deprecated Use Calendar instead of Date, and use get(Calendar.DATE)
+   * instead.
+   */
+  public int getDate()
   {
-    return getField(Calendar.MINUTE);
+    Calendar cal = Calendar.getInstance();
+    cal.setTimeInMillis(time);
+    return cal.get(Calendar.DATE);
   }
 
-  public int getSeconds ()
+  /**
+   * Sets the date to the given value, not changing the other fields.
+   * @param date the date.
+   * @deprecated Use Calendar instead of Date, and use
+   * set(Calendar.DATE, date) instead. 
+   */
+  public void setDate(int date)
   {
-    return getField(Calendar.SECOND);
+    Calendar cal = Calendar.getInstance();
+    cal.setTimeInMillis(time);
+    cal.set(Calendar.DATE, date);
+    time = cal.getTimeInMillis();
   }
 
-  private void setField (int fld, int value)
+  /**
+   * @return the day represented by this date object.
+   * @deprecated Use Calendar instead of Date, and use get(Calendar.DAY_OF_WEEK)
+   * instead.
+   */
+  public int getDay()
   {
-    Calendar cal = new GregorianCalendar();
-    cal.setTime(this);
-    cal.set(fld, value);
-    millis = cal.getTimeInMillis();
+    Calendar cal = Calendar.getInstance();
+    cal.setTimeInMillis(time);
+    // For Calendar, Sunday is 1.  For Date, Sunday is 0.
+    return cal.get(Calendar.DAY_OF_WEEK) - 1;
   }
 
-  public void setYear (int year)
+  /**
+   * @return the hours represented by this date object.
+   * @deprecated Use Calendar instead of Date, and use get(Calendar.HOUR_OF_DAY)
+   * instead.
+   */
+  public int getHours()
   {
-    setField(Calendar.YEAR, 1900 + year);
+    Calendar cal = Calendar.getInstance();
+    cal.setTimeInMillis(time);
+    return cal.get(Calendar.HOUR_OF_DAY);
   }
 
-  public void setMonth (int month)
+  /**
+   * Sets the hours to the given value, not changing the other fields.
+   * @param hours the hours.
+   * @deprecated Use Calendar instead of Date, and use
+   * set(Calendar.HOUR_OF_DAY, hours) instead. 
+   */
+  public void setHours(int hours)
   {
-    setField(Calendar.MONTH, month);
+    Calendar cal = Calendar.getInstance();
+    cal.setTimeInMillis(time);
+    cal.set(Calendar.HOUR_OF_DAY, hours);
+    time = cal.getTimeInMillis();
   }
 
-  public void setDate (int date)
+  /**
+   * @return the minutes represented by this date object.
+   * @deprecated Use Calendar instead of Date, and use get(Calendar.MINUTE)
+   * instead.
+   */
+  public int getMinutes()
   {
-    setField(Calendar.DATE, date);
+    Calendar cal = Calendar.getInstance();
+    cal.setTimeInMillis(time);
+    return cal.get(Calendar.MINUTE);
   }
 
-  public void setHours (int hours)
+  /**
+   * Sets the minutes to the given value, not changing the other fields.
+   * @param minutes the minutes.
+   * @deprecated Use Calendar instead of Date, and use
+   * set(Calendar.MINUTE, minutes) instead. 
+   */
+  public void setMinutes(int minutes)
   {
-    setField(Calendar.HOUR_OF_DAY, hours);
+    Calendar cal = Calendar.getInstance();
+    cal.setTimeInMillis(time);
+    cal.set(Calendar.MINUTE, minutes);
+    time = cal.getTimeInMillis();
   }
 
-  public void setMinutes (int minutes)
+  /**
+   * @return the seconds represented by this date object.
+   * @deprecated Use Calendar instead of Date, and use get(Calendar.SECOND)
+   * instead.
+   */
+  public int getSeconds()
   {
-    setField(Calendar.MINUTE, minutes);
+    Calendar cal = Calendar.getInstance();
+    cal.setTimeInMillis(time);
+    return cal.get(Calendar.SECOND);
   }
 
-  public void setSeconds (int seconds)
+  /**
+   * Sets the seconds to the given value, not changing the other fields.
+   * @param seconds the seconds.
+   * @deprecated Use Calendar instead of Date, and use
+   * set(Calendar.SECOND, seconds) instead. 
+   */
+  public void setSeconds(int seconds)
   {
-    setField(Calendar.SECOND, seconds);
+    Calendar cal = Calendar.getInstance();
+    cal.setTimeInMillis(time);
+    cal.set(Calendar.SECOND, seconds);
+    time = cal.getTimeInMillis();
   }
 
-  public int getTimezoneOffset ()
+  /**
+   * Reads an Object from the stream.
+   */
+  private void readObject(java.io.ObjectInputStream input)
+    throws java.io.IOException, ClassNotFoundException
   {
-    Calendar cal = new GregorianCalendar(); 
-    cal.setTime(this);
-    return - (cal.get(Calendar.ZONE_OFFSET)
-             + cal.get(Calendar.DST_OFFSET)/(60*1000));
+    input.defaultReadObject();
+    time = input.readLong();
   }
 
-  public native String toString ();
-
-  // TODO: toLocaleString
-  // TODO: toGMTString
-
-  public static long UTC (int year, int month, int date,
-                         int hours, int minutes, int seconds)
+  /**
+   * Writes an Object to the stream.
+   * @serialdata A long value representing the offset from the epoch
+   * in milliseconds.  This is the same value that is returned by the
+   * method getTime().
+   */
+  private void writeObject(java.io.ObjectOutputStream output)
+    throws java.io.IOException
   {
-    GregorianCalendar cal = new GregorianCalendar (TimeZone.zoneGMT);
-    cal.set(year+1900, month, date, hours, minutes, seconds);
-    return cal.getTimeInMillis();
+    output.defaultWriteObject();
+    output.writeLong(time);
   }
 }