2 Copyright (C) 1998, 1999, 2000, 2001, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
40 /*import gnu.java.lang.CPStringBuilder;
42 import java.io.IOException;
43 import java.io.ObjectInputStream;
44 import java.io.ObjectOutputStream;
45 import java.io.Serializable;
46 import java.text.DateFormat;
47 import java.text.SimpleDateFormat;
51 * This class represents a specific time in milliseconds since the epoch.
52 * The epoch is 1970, January 1 00:00:00.0000 UTC.
55 * <code>Date</code> is intended to reflect universal time coordinate (UTC),
56 * but this depends on the underlying host environment. Most operating systems
57 * don't handle the leap second, which occurs about once every year or
58 * so. The leap second is added to the last minute of the day on either
59 * the 30th of June or the 31st of December, creating a minute 61 seconds
63 * The representations of the date fields are as follows:
66 * Years are specified as the difference between the year
67 * and 1900. Thus, the final year used is equal to
68 * 1900 + y, where y is the input value.
71 * Months are represented using zero-based indexing,
72 * making 0 January and 11 December.
75 * Dates are represented with the usual values of
79 * Hours are represented in the twenty-four hour clock,
80 * with integer values from 0 to 23. 12am is 0, and
84 * Minutes are again as usual, with values from 0 to 59.
87 * Seconds are represented with the values 0 through to 61,
88 * with 60 and 61 being leap seconds (as per the ISO C standard).
93 * Prior to JDK 1.1, this class was the sole class handling date and time
94 * related functionality. However, this particular solution was not
95 * amenable to internationalization. The new <code>Calendar</code>
96 * class should now be used to handle dates and times, with <code>Date</code>
97 * being used only for values in milliseconds since the epoch. The
98 * <code>Calendar</code> class, and its concrete implementations, handle
99 * the interpretation of these values into minutes, hours, days, months
100 * and years. The formatting and parsing of dates is left to the
101 * <code>DateFormat</code> class, which is able to handle the different
102 * types of date format which occur in different locales.
106 * @see GregorianCalendar
107 * @see java.text.DateFormat
108 * @author Jochen Hoenicke
109 * @author Per Bothner (bothner@cygnus.com)
110 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
113 //implements Cloneable, Comparable<Date>, Serializable
116 * This is the serialization UID for this class
117 * for compatability with Sun's JDK.
119 private static final long serialVersionUID = 7523967970034938905L;
122 * The time in milliseconds since the epoch.
124 private transient long time;
127 * An array of week names used to map names to integer values.
129 private static final String[] weekNames = { "Sun", "Mon", "Tue", "Wed",
130 "Thu", "Fri", "Sat" };
132 * An array of month names used to map names to integer values.
134 private static final String[] monthNames = { "Jan", "Feb", "Mar", "Apr",
135 "May", "Jun", "Jul", "Aug",
136 "Sep", "Oct", "Nov", "Dec" };
138 * Creates a new Date Object representing the current time.
142 time = System.currentTimeMillis();
146 * Creates a new Date Object representing the given time.
148 * @param time the time in milliseconds since the epoch.
150 public Date(long time)
156 * Creates a new Date Object representing the given time.
158 * @deprecated use <code>new GregorianCalendar(year+1900, month,
159 * day)</code> instead.
160 * @param year the difference between the required year and 1900.
161 * @param month the month as a value between 0 and 11.
162 * @param day the day as a value between 0 and 31.
164 /*public Date(int year, int month, int day)
166 this(year, month, day, 0, 0, 0);
170 * Creates a new Date Object representing the given time.
172 * @deprecated use <code>new GregorianCalendar(year+1900, month,
173 * day, hour, min)</code> instead.
174 * @param year the difference between the required year and 1900.
175 * @param month the month as a value between 0 and 11.
176 * @param day the day as a value between 0 and 31.
177 * @param hour the hour as a value between 0 and 23, in 24-hour
179 * @param min the minute as a value between 0 and 59.
181 /*public Date(int year, int month, int day, int hour, int min)
183 this(year, month, day, hour, min, 0);
187 * Creates a new Date Object representing the given time.
189 * @deprecated use <code>new GregorianCalendar(year+1900, month,
190 * day, hour, min, sec)</code> instead.
191 * @param year the difference between the required year and 1900.
192 * @param month the month as a value between 0 and 11.
193 * @param day the day as a value between 0 and 31.
194 * @param hour the hour as a value between 0 and 23, in 24-hour
196 * @param min the minute as a value between 0 and 59.
197 * @param sec the second as a value between 0 and 61 (with 60
198 * and 61 being leap seconds).
200 /*public Date(int year, int month, int day, int hour, int min, int sec)
202 GregorianCalendar cal =
203 new GregorianCalendar(year + 1900, month, day, hour, min, sec);
204 time = cal.getTimeInMillis();
208 * Creates a new Date from the given string representation. This
209 * does the same as <code>new Date(Date.parse(s))</code>
211 * @deprecated use <code>java.text.DateFormat.parse(s)</code> instead.
213 /*public Date(String s)
219 * Returns a copy of this <code>Date</code> object.
221 * @return a copy, or null if the object couldn't be
223 * @see Object#clone()
225 /*public Object clone()
229 return super.clone();
231 catch (CloneNotSupportedException ex)
238 * Returns the number of milliseconds since the epoch
239 * specified by the given arguments. The arguments are
240 * interpreted relative to UTC rather than the local
243 * @deprecated Use <code>Calendar</code> with a UTC
244 * <code>TimeZone</code> instead.
245 * @param year the difference between the required year and 1900.
246 * @param month the month as a value between 0 and 11.
247 * @param date the day as a value between 0 and 31.
248 * @param hrs the hour as a value between 0 and 23, in 24-hour
250 * @param min the minute as a value between 0 and 59.
251 * @param sec the second as a value between 0 and 61 (with 60
252 * and 61 being leap seconds).
253 * @return the time in milliseconds since the epoch.
255 /*public static long UTC(int year, int month, int date,
256 int hrs, int min, int sec)
258 GregorianCalendar cal =
259 new GregorianCalendar(year + 1900, month, date, hrs, min, sec);
260 cal.set(Calendar.ZONE_OFFSET, 0);
261 cal.set(Calendar.DST_OFFSET, 0);
262 return cal.getTimeInMillis();
266 * Gets the time represented by this object.
268 * @return the time in milliseconds since the epoch.
270 public long getTime()
276 * Returns the number of minutes offset used with UTC to give the time
277 * represented by this object in the current time zone. The date information
278 * from this object is also used to determine whether or not daylight savings
279 * time is in effect. For example, the offset for the UK would be 0 if the
280 * month of the date object was January, and 1 if the month was August.
283 * <code>Calendar.get(Calendar.ZONE_OFFSET)+Calendar.get(Calendar.DST_OFFSET)</code>
285 * @return The time zone offset in minutes of the local time zone
286 * relative to UTC. The time represented by this object is used to
287 * determine if we should use daylight savings.
289 /*public int getTimezoneOffset()
291 Calendar cal = Calendar.getInstance();
292 cal.setTimeInMillis(time);
293 return - (cal.get(Calendar.ZONE_OFFSET)
294 + cal.get(Calendar.DST_OFFSET)) / (60 * 1000);
298 * Sets the time which this object should represent.
300 * @param time the time in milliseconds since the epoch.
302 public void setTime(long time)
308 * Tests if this date is after the specified date.
310 * @param when the other date
311 * @return true, if the date represented by this object is
312 * strictly later than the time represented by when.
314 public boolean after(Date when)
316 return time > when.time;
320 * Tests if this date is before the specified date.
322 * @param when the other date
323 * @return true, if the date represented by when is strictly later
324 * than the time represented by this object.
326 public boolean before(Date when)
328 return time < when.time;
332 * Compares two dates for equality.
334 * @param obj the object to compare.
335 * @return true, if obj is a Date object and the time represented
336 * by obj is exactly the same as the time represented by this
339 public boolean equals(Object obj)
341 return (obj instanceof Date && time == ((Date) obj).time);
345 * Compares two dates.
347 * @param when the other date.
348 * @return 0, if the date represented
349 * by obj is exactly the same as the time represented by this
350 * object, a negative if this Date is before the other Date, and
351 * a positive value otherwise.
353 public int compareTo(Date when)
355 return (time < when.time) ? -1 : (time == when.time) ? 0 : 1;
359 * Computes the hash code of this <code>Date</code> as the
360 * XOR of the most significant and the least significant
361 * 32 bits of the 64 bit milliseconds value.
363 * @return the hash code.
365 public int hashCode()
367 return (int) time ^ (int) (time >>> 32);
372 * Returns a string representation of this date using
373 * the following date format:
376 * <code>day mon dd hh:mm:ss zz yyyy</code>
378 * <p>where the fields used here are:
381 * <code>day</code> -- the day of the week
382 * (Sunday through to Saturday).
385 * <code>mon</code> -- the month (Jan to Dec).
388 * <code>dd</code> -- the day of the month
389 * as two decimal digits (01 to 31).
392 * <code>hh</code> -- the hour of the day
393 * as two decimal digits in 24-hour clock notation
397 * <code>mm</code> -- the minute of the day
398 * as two decimal digits (01 to 59).
401 * <code>ss</code> -- the second of the day
402 * as two decimal digits (01 to 61).
405 * <code>zz</code> -- the time zone information if available.
406 * The possible time zones used include the abbreviations
407 * recognised by <code>parse()</code> (e.g. GMT, CET, etc.)
408 * and may reflect the fact that daylight savings time is in
409 * effect. The empty string is used if there is no time zone
413 * <code>yyyy</code> -- the year as four decimal digits.
417 * The <code>DateFormat</code> class should now be
418 * preferred over using this method.
421 * @return A string of the form 'day mon dd hh:mm:ss zz yyyy'
422 * @see #parse(String)
425 /*public String toString()
427 Calendar cal = Calendar.getInstance();
428 cal.setTimeInMillis(time);
429 String day = "0" + cal.get(Calendar.DATE);
430 String hour = "0" + cal.get(Calendar.HOUR_OF_DAY);
431 String min = "0" + cal.get(Calendar.MINUTE);
432 String sec = "0" + cal.get(Calendar.SECOND);
433 String year = "000" + cal.get(Calendar.YEAR);
434 return weekNames[cal.get(Calendar.DAY_OF_WEEK) - 1] + " "
435 + monthNames[cal.get(Calendar.MONTH)] + " "
436 + day.substring(day.length() - 2) + " "
437 + hour.substring(hour.length() - 2) + ":"
438 + min.substring(min.length() - 2) + ":"
439 + sec.substring(sec.length() - 2) + " "
441 cal.getTimeZone().getDisplayName(cal.getTimeZone().inDaylightTime(this),
442 TimeZone.SHORT) + " " +
443 year.substring(year.length() - 4);
447 * Returns a locale-dependent string representation of this
448 * <code>Date</code> object.
450 * @deprecated Use DateFormat.format(Date)
451 * @return A locale-dependent string representation.
452 * @see #parse(String)
455 /*public String toLocaleString()
457 return java.text.DateFormat.getInstance().format(this);
462 * Returns a string representation of this <code>Date</code>
463 * object using GMT rather than the local timezone.
464 * The following date format is used:
467 * <code>d mon yyyy hh:mm:ss GMT</code>
469 * <p>where the fields used here are:
472 * <code>d</code> -- the day of the month
473 * as one or two decimal digits (1 to 31).
476 * <code>mon</code> -- the month (Jan to Dec).
479 * <code>yyyy</code> -- the year as four decimal digits.
482 * <code>hh</code> -- the hour of the day
483 * as two decimal digits in 24-hour clock notation
487 * <code>mm</code> -- the minute of the day
488 * as two decimal digits (01 to 59).
491 * <code>ss</code> -- the second of the day
492 * as two decimal digits (01 to 61).
495 * <code>GMT</code> -- the literal string "GMT"
496 * indicating Greenwich Mean Time as opposed to
497 * the local timezone.
501 * @deprecated Use DateFormat.format(Date) with a GMT TimeZone.
502 * @return A string of the form 'd mon yyyy hh:mm:ss GMT' using
503 * GMT as opposed to the local timezone.
504 * @see #parse(String)
507 /*public String toGMTString()
509 java.text.DateFormat format = java.text.DateFormat.getInstance();
510 format.setTimeZone(TimeZone.getTimeZone("GMT"));
511 return format.format(this);
515 * Parses the time zone string.
517 * @param tok The token containing the time zone.
518 * @param sign The sign (+ or -) used by the time zone.
519 * @return An integer representing the number of minutes offset
520 * from GMT for the time zone.
522 /*private static int parseTz(String tok, char sign)
523 throws IllegalArgumentException
529 // parseInt doesn't handle '+' so strip off sign.
530 num = Integer.parseInt(tok.substring(1));
532 catch (NumberFormatException ex)
534 throw new IllegalArgumentException(tok);
537 // Convert hours to minutes.
541 num = (num / 100) * 60 + num % 100;
543 return sign == '-' ? -num : num;
547 * Parses the month string.
549 * @param tok the token containing the month.
550 * @return An integer between 0 and 11, representing
551 * a month from January (0) to December (11),
552 * or -1 if parsing failed.
554 /*private static int parseMonth(String tok)
556 // Initialize strings for month names.
557 // We could possibly use the fields of DateFormatSymbols but that is
558 // localized and thus might not match the English words specified.
559 String months[] = { "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY",
560 "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER",
561 "NOVEMBER", "DECEMBER" };
564 for (i = 0; i < 12; i++)
565 if (months[i].startsWith(tok))
568 // Return -1 if not found.
573 * Parses the day of the week string.
575 * @param tok the token containing the day of the week.
576 * @return true if the token was parsed successfully.
578 /*private static boolean parseDayOfWeek(String tok)
580 // Initialize strings for days of the week names.
581 // We could possibly use the fields of DateFormatSymbols but that is
582 // localized and thus might not match the English words specified.
583 String daysOfWeek[] = { "SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY",
584 "THURSDAY", "FRIDAY", "SATURDAY" };
587 for (i = 0; i < 7; i++)
588 if (daysOfWeek[i].startsWith(tok))
596 * Parses a String and returns the time, in milliseconds since the
597 * epoch, it represents. Most syntaxes are handled, including
598 * the IETF date standard "day, dd mon yyyy hh:mm:ss zz" (see
599 * <code>toString()</code> for definitions of these fields).
600 * Standard U.S. time zone abbreviations are recognised, in
601 * addition to time zone offsets in positive or negative minutes.
602 * If a time zone is specified, the specified time is assumed to
603 * be in UTC and the appropriate conversion is applied, following
604 * parsing, to convert this to the local time zone. If no zone
605 * is specified, the time is assumed to already be in the local
609 * The method parses the string progressively from left to right.
610 * At the end of the parsing process, either a time is returned
611 * or an <code>IllegalArgumentException</code> is thrown to signify
612 * failure. The ASCII characters A-Z, a-z, 0-9, and ',', '+', '-',
613 * ':' and '/' are the only characters permitted within the string,
614 * besides whitespace and characters enclosed within parantheses
618 * A sequence of consecutive digits are recognised as a number,
619 * and interpreted as follows:
622 * A number preceded by a sign (+ or -) is taken to be a time zone
623 * offset. The time zone offset can be specified in either hours
624 * or minutes. The former is assumed if the number is less than 24.
625 * Otherwise, the offset is assumed to be in minutes. A - indicates
626 * a time zone west of GMT, while a + represents a time zone to the
627 * east of GMT. The time zones are always assumed to be relative
628 * to GMT, and a (redundant) specification of this can be included
629 * with the time zone. For example, '-9', 'utc-9' and 'GMT-9' all
630 * represent a time zone nine hours west of GMT. Similarly,
631 * '+4', 'ut+4' and 'UTC+4' all give 4 hours east of GMT.
634 * A number equal to or greater than 70 is regarded as a year specification.
635 * Values lower than 70 are only assumed to indicate a year if both the
636 * day of the month and the month itself have already been recognised.
637 * Year values less than 100 are interpreted as being relative to the current
638 * century when the <code>Date</code> class is initialised.. Given a century,
639 * x, the year is assumed to be within the range x - 80 to x + 19. The value
640 * itself is then used as a match against the two last digits of one of these
641 * years. For example, take x to be 2004. A two-digit year is assumed to fall
642 * within the range x - 80 (1924) and x + 19 (2023). Thus, any intepreted value
643 * between 0 and 23 is assumed to be 2000 to 2023 and values between 24 and 99
644 * are taken as being 1924 to 1999. This only applies for the case of 2004.
645 * With a different year, the values will be interpreted differently. 2005
646 * will used 0 to 24 as 2000 to 2024 and 25 to 99 as 1925 to 1999, for example.
647 * This behaviour differs from that of <code>SimpleDateFormat</code> and is
648 * time-dependent (a two-digit year will be interpreted differently depending
649 * on the time the code is run).
652 * Numbers followed by a colon are interpreted by first an hour, and then
653 * as a minute, once an hour has been found.
657 * Numbers followed by a slash are regarded first as a month, and then as
658 * a day of the month once the month has been found. This follows the
659 * U.S. date format of mm/dd, rather than the European dd/mm. Months
660 * are converted to the recognised value - 1 before storage, in order
661 * to put the number within the range 0 to 11.
664 * Numbers followed by commas, whitespace, hyphens or the end of the string
665 * are interpreted in the following order: hour, minute, second, day of month.
666 * The first type not already recognised in the current string being parsed is
672 * A sequence of consecutive alphabetic characters is recognised as a word,
673 * and interpreted as follows, in a case-insentive fashion:
676 * The characters 'AM' or 'PM' restrict the hour value to a value between 0
677 * and 12. In the latter case, 12 is added to the hour value before storage.
680 * Any words which match any prefix of one of the days of the week ('Monday',
681 * 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' and 'Sunday'),
682 * are simply ignored.
685 * Any words which match any prefix of one of the months of the year ('January',
686 * 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September',
687 * 'October', 'November', 'December') are recognised and interpreted as the
688 * appropriate value between 0 and 11. The first match made against a
689 * month is the one used, in the order specified here. For example, 'Ma' is
690 * intepreted as 'March' (2) and not as 'May' (4). Similarly, 'Ju' is 'June',
694 * The words 'GMT', 'UT' and 'UTC' are interpreted as specifying UTC as the
695 * time zone in use for this date.
698 * The word pairs 'EST'/'EDT', 'CST'/'CDT', 'MST'/'MDT' and 'PST'/'PDT' are
699 * interpreted as the appropriate U.S. time zone abbreviation. Each pair
700 * is the standard and daylight savings time zone specification, respectively,
701 * for each zone within the U.S, these being Eastern Standard/Daylight Time
702 * (-5), Central Standard/Daylight Time (-6), Mountain Standard/Daylight Time
703 * (-7) and Pacific Standard/Daylight Time (-8).
707 * @param string The String to parse.
708 * @return The time in milliseconds since the epoch.
709 * @throws IllegalArgumentException if the string fails to parse.
710 * @deprecated Use DateFormat.parse(String)
712 * @see SimpleDateFormat
714 /*public static long parse(String string)
716 // Initialize date/time fields before parsing begins.
724 boolean localTimezone = true;
726 // Trim out any nested stuff in parentheses now to make parsing easier.
727 CPStringBuilder buf = new CPStringBuilder();
728 int parenNesting = 0;
729 int len = string.length();
730 for (int i = 0; i < len; i++)
732 char ch = string.charAt(i);
733 if (ch >= 'a' && ch <= 'z')
737 else if (parenNesting == 0)
744 // Make all chars upper case to simplify comparisons later.
745 // Also ignore commas; treat them as delimiters.
746 StringTokenizer strtok = new StringTokenizer(buf.toString(), " \t\n\r,");
748 while (strtok.hasMoreTokens())
750 String tok = strtok.nextToken();
751 char firstch = tok.charAt(0);
752 if ((firstch == '+' || firstch == '-') && year >= 0)
754 timezone = parseTz(tok, firstch);
755 localTimezone = false;
757 else if (firstch >= '0' && firstch <= '9')
760 while (tok != null && tok.length() > 0)
762 int punctOffset = tok.length();
765 for (int i = 0; ; i++)
767 if (i >= punctOffset)
774 punct = tok.charAt(i);
775 if (punct >= '0' && punct <= '9')
777 if (num > 999999999) // in case of overflow
778 throw new IllegalArgumentException(tok);
779 num = 10 * num + (punct - '0');
797 else if (lastPunct == ':' && hour >= 0 && (minute < 0 || second < 0))
805 && (punct == ' ' || punct == ','
806 || punct == '/' || punct < 0))
807 || (num < 70 && day >= 0 && month >= 0 && year < 0))
813 int curYear = 1900 + new Date().getYear();
814 int firstYear = curYear - 80;
815 year = firstYear / 100 * 100 + num;
816 if (year < firstYear)
820 else if (punct == '/')
827 else if (hour >= 0 && minute < 0)
829 else if (minute >= 0 && second < 0)
834 throw new IllegalArgumentException(tok);
836 // Advance string if there's more to process in this token.
837 if (punct < 0 || punctOffset + 1 >= tok.length())
840 tok = tok.substring(punctOffset + 1);
844 else if (firstch >= 'A' && firstch <= 'Z')
846 if (tok.equals("AM"))
848 if (hour < 1 || hour > 12)
849 throw new IllegalArgumentException(tok);
853 else if (tok.equals("PM"))
855 if (hour < 1 || hour > 12)
856 throw new IllegalArgumentException(tok);
860 else if (parseDayOfWeek(tok))
861 { // Ignore it; throw the token away.
863 else if (tok.equals("UT") || tok.equals("UTC") || tok.equals("GMT"))
864 localTimezone = false;
865 else if (tok.startsWith("UT") || tok.startsWith("GMT"))
868 if (tok.charAt(1) == 'T' && tok.charAt(2) != 'C')
871 char sign = tok.charAt(signOffset);
872 if (sign != '+' && sign != '-')
873 throw new IllegalArgumentException(tok);
875 timezone = parseTz(tok.substring(signOffset), sign);
876 localTimezone = false;
878 else if ((tmpMonth = parseMonth(tok)) >= 0)
880 else if (tok.length() == 3 && tok.charAt(2) == 'T')
882 // Convert timezone offset from hours to minutes.
883 char ch = tok.charAt(0);
893 throw new IllegalArgumentException(tok);
895 // Shift 60 minutes for Daylight Savings Time.
896 if (tok.charAt(1) == 'D')
898 else if (tok.charAt(1) != 'S')
899 throw new IllegalArgumentException(tok);
901 localTimezone = false;
904 throw new IllegalArgumentException(tok);
907 throw new IllegalArgumentException(tok);
910 // Unspecified hours, minutes, or seconds should default to 0.
918 // Throw exception if any other fields have not been recognized and set.
919 if (year < 0 || month < 0 || day < 0)
920 throw new IllegalArgumentException("Missing field");
922 // Return the time in either local time or relative to GMT as parsed.
923 // If no time-zone was specified, get the local one (in minutes) and
924 // convert to milliseconds before adding to the UTC.
925 GregorianCalendar cal
926 = new GregorianCalendar(year, month, day, hour, minute, second);
929 cal.set(Calendar.ZONE_OFFSET, timezone * 60 * 1000);
930 cal.set(Calendar.DST_OFFSET, 0);
932 return cal.getTimeInMillis();
936 * Returns the difference between the year represented by this
937 * <code>Date</code> object and 1900.
939 * @return the year minus 1900 represented by this date object.
940 * @deprecated Use Calendar instead of Date, and use get(Calendar.YEAR)
941 * instead. Note the 1900 difference in the year.
945 /*public int getYear()
947 Calendar cal = Calendar.getInstance();
948 cal.setTimeInMillis(time);
949 return cal.get(Calendar.YEAR) - 1900;
953 * Sets the year to the specified year, plus 1900. The other
954 * fields are only altered as required to match the same date
955 * and time in the new year. Usually, this will mean that
956 * the fields are not changed at all, but in the case of
957 * a leap day or leap second, the fields will change in
958 * relation to the existence of such an event in the new year.
959 * For example, if the date specifies February the 29th, 2000,
960 * then this will become March the 1st if the year is changed
961 * to 2001, as 2001 is not a leap year. Similarly, a seconds
962 * value of 60 or 61 may result in the seconds becoming 0 and
963 * the minute increasing by 1, if the new time does not include
966 * @param year the year minus 1900.
967 * @deprecated Use Calendar instead of Date, and use
968 * set(Calendar.YEAR, year) instead. Note about the 1900
969 * difference in year.
973 /*public void setYear(int year)
975 Calendar cal = Calendar.getInstance();
976 cal.setTimeInMillis(time);
977 cal.set(Calendar.YEAR, 1900 + year);
978 time = cal.getTimeInMillis();
982 * Returns the month represented by this <code>Date</code> object,
983 * as a value between 0 (January) and 11 (December).
985 * @return the month represented by this date object (zero based).
986 * @deprecated Use Calendar instead of Date, and use get(Calendar.MONTH)
988 * @see #setMonth(int)
991 /*public int getMonth()
993 Calendar cal = Calendar.getInstance();
994 cal.setTimeInMillis(time);
995 return cal.get(Calendar.MONTH);
999 * Sets the month to the given value. The other
1000 * fields are only altered as necessary to match
1001 * the same date and time in the new month. In most
1002 * cases, the other fields won't change at all. However,
1003 * in the case of a shorter month or a leap second, values
1004 * may be adjusted. For example, if the day of the month
1005 * is currently 31, and the month value is changed from
1006 * January (0) to September (8), the date will become
1007 * October the 1st, as September only has 30 days. Similarly,
1008 * a seconds value of 60 or 61 (a leap second) may result
1009 * in the seconds value being reset to 0 and the minutes
1010 * value being incremented by 1, if the new time does
1011 * not include a leap second.
1013 * @param month the month, with a zero-based index
1015 * @deprecated Use Calendar instead of Date, and use
1016 * set(Calendar.MONTH, month) instead.
1020 /*public void setMonth(int month)
1022 Calendar cal = Calendar.getInstance();
1023 cal.setTimeInMillis(time);
1024 cal.set(Calendar.MONTH, month);
1025 time = cal.getTimeInMillis();
1029 * Returns the day of the month of this <code>Date</code>
1030 * object, as a value between 0 and 31.
1032 * @return the day of month represented by this date object.
1033 * @deprecated Use Calendar instead of Date, and use get(Calendar.DATE)
1036 * @see #setDate(int)
1038 /*public int getDate()
1040 Calendar cal = Calendar.getInstance();
1041 cal.setTimeInMillis(time);
1042 return cal.get(Calendar.DATE);
1046 * Sets the date to the given value. The other
1047 * fields are only altered as necessary to match
1048 * the same date and time on the new day of the month. In most
1049 * cases, the other fields won't change at all. However,
1050 * in the case of a leap second or the day being out of
1051 * the range of the current month, values
1052 * may be adjusted. For example, if the day of the month
1053 * is currently 30 and the month is June, a new day of the
1054 * month value of 31 will cause the month to change to July,
1055 * as June only has 30 days . Similarly,
1056 * a seconds value of 60 or 61 (a leap second) may result
1057 * in the seconds value being reset to 0 and the minutes
1058 * value being incremented by 1, if the new time does
1059 * not include a leap second.
1061 * @param date the date.
1062 * @deprecated Use Calendar instead of Date, and use
1063 * set(Calendar.DATE, date) instead.
1067 /*public void setDate(int date)
1069 Calendar cal = Calendar.getInstance();
1070 cal.setTimeInMillis(time);
1071 cal.set(Calendar.DATE, date);
1072 time = cal.getTimeInMillis();
1076 * Returns the day represented by this <code>Date</code>
1077 * object as an integer between 0 (Sunday) and 6 (Saturday).
1079 * @return the day represented by this date object.
1080 * @deprecated Use Calendar instead of Date, and use get(Calendar.DAY_OF_WEEK)
1084 /*public int getDay()
1086 Calendar cal = Calendar.getInstance();
1087 cal.setTimeInMillis(time);
1088 // For Calendar, Sunday is 1. For Date, Sunday is 0.
1089 return cal.get(Calendar.DAY_OF_WEEK) - 1;
1093 * Returns the hours represented by this <code>Date</code>
1094 * object as an integer between 0 and 23.
1096 * @return the hours represented by this date object.
1097 * @deprecated Use Calendar instead of Date, and use get(Calendar.HOUR_OF_DAY)
1100 * @see #setHours(int)
1102 /*public int getHours()
1104 Calendar cal = Calendar.getInstance();
1105 cal.setTimeInMillis(time);
1106 return cal.get(Calendar.HOUR_OF_DAY);
1110 * Sets the hours to the given value. The other
1111 * fields are only altered as necessary to match
1112 * the same date and time in the new hour. In most
1113 * cases, the other fields won't change at all. However,
1114 * in the case of a leap second, values
1115 * may be adjusted. For example,
1116 * a seconds value of 60 or 61 (a leap second) may result
1117 * in the seconds value being reset to 0 and the minutes
1118 * value being incremented by 1 if the new hour does
1119 * not contain a leap second.
1121 * @param hours the hours.
1122 * @deprecated Use Calendar instead of Date, and use
1123 * set(Calendar.HOUR_OF_DAY, hours) instead.
1127 /*public void setHours(int hours)
1129 Calendar cal = Calendar.getInstance();
1130 cal.setTimeInMillis(time);
1131 cal.set(Calendar.HOUR_OF_DAY, hours);
1132 time = cal.getTimeInMillis();
1136 * Returns the number of minutes represented by the <code>Date</code>
1137 * object, as an integer between 0 and 59.
1139 * @return the minutes represented by this date object.
1140 * @deprecated Use Calendar instead of Date, and use get(Calendar.MINUTE)
1143 * @see #setMinutes(int)
1145 /*public int getMinutes()
1147 Calendar cal = Calendar.getInstance();
1148 cal.setTimeInMillis(time);
1149 return cal.get(Calendar.MINUTE);
1153 * Sets the minutes to the given value. The other
1154 * fields are only altered as necessary to match
1155 * the same date and time in the new minute. In most
1156 * cases, the other fields won't change at all. However,
1157 * in the case of a leap second, values
1158 * may be adjusted. For example,
1159 * a seconds value of 60 or 61 (a leap second) may result
1160 * in the seconds value being reset to 0 and the minutes
1161 * value being incremented by 1 if the new minute does
1162 * not contain a leap second.
1164 * @param minutes the minutes.
1165 * @deprecated Use Calendar instead of Date, and use
1166 * set(Calendar.MINUTE, minutes) instead.
1168 * @see #getMinutes()
1170 /*public void setMinutes(int minutes)
1172 Calendar cal = Calendar.getInstance();
1173 cal.setTimeInMillis(time);
1174 cal.set(Calendar.MINUTE, minutes);
1175 time = cal.getTimeInMillis();
1179 * Returns the number of seconds represented by the <code>Date</code>
1180 * object, as an integer between 0 and 61 (60 and 61 being leap seconds).
1182 * @return the seconds represented by this date object.
1183 * @deprecated Use Calendar instead of Date, and use get(Calendar.SECOND)
1186 * @see #setSeconds(int)
1188 /*public int getSeconds()
1190 Calendar cal = Calendar.getInstance();
1191 cal.setTimeInMillis(time);
1192 return cal.get(Calendar.SECOND);
1196 * Sets the seconds to the given value. The other
1197 * fields are only altered as necessary to match
1198 * the same date and time in the new minute. In most
1199 * cases, the other fields won't change at all. However,
1200 * in the case of a leap second, values
1201 * may be adjusted. For example, setting the
1202 * seconds value to 60 or 61 (a leap second) may result
1203 * in the seconds value being reset to 0 and the minutes
1204 * value being incremented by 1, if the current time does
1205 * not contain a leap second.
1207 * @param seconds the seconds.
1208 * @deprecated Use Calendar instead of Date, and use
1209 * set(Calendar.SECOND, seconds) instead.
1211 * @see #getSeconds()
1213 /*public void setSeconds(int seconds)
1215 Calendar cal = Calendar.getInstance();
1216 cal.setTimeInMillis(time);
1217 cal.set(Calendar.SECOND, seconds);
1218 time = cal.getTimeInMillis();
1222 * Deserializes a <code>Date</code> object from an
1223 * input stream, setting the time (in milliseconds
1224 * since the epoch) to the long value read from the
1227 * @param input the input stream.
1228 * @throws IOException if an I/O error occurs in the stream.
1229 * @throws ClassNotFoundException if the class of the
1230 * serialized object could not be found.
1232 /*private void readObject(ObjectInputStream input)
1233 throws IOException, ClassNotFoundException
1235 input.defaultReadObject();
1236 time = input.readLong();
1240 * Serializes a <code>Date</code> object to an output stream,
1241 * storing the time (in milliseconds since the epoch) as a long
1242 * value in the stream.
1244 * @serialdata A long value representing the offset from the epoch
1245 * in milliseconds. This is the same value that is returned by the
1247 * @param output the output stream.
1248 * @throws IOException if an I/O error occurs in the stream.
1250 /*private void writeObject(ObjectOutputStream output)
1253 output.defaultWriteObject();
1254 output.writeLong(time);