Date.java revision 3261
0N/A/*
1204N/A * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
0N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
0N/A * questions.
0N/A */
0N/A
0N/Apackage java.util;
0N/A
0N/Aimport java.text.DateFormat;
0N/Aimport java.io.IOException;
0N/Aimport java.io.ObjectOutputStream;
0N/Aimport java.io.ObjectInputStream;
0N/Aimport java.lang.ref.SoftReference;
0N/Aimport sun.util.calendar.BaseCalendar;
0N/Aimport sun.util.calendar.CalendarDate;
0N/Aimport sun.util.calendar.CalendarSystem;
0N/Aimport sun.util.calendar.CalendarUtils;
0N/Aimport sun.util.calendar.Era;
0N/Aimport sun.util.calendar.Gregorian;
0N/Aimport sun.util.calendar.ZoneInfo;
0N/A
0N/A/**
0N/A * The class <code>Date</code> represents a specific instant
0N/A * in time, with millisecond precision.
0N/A * <p>
0N/A * Prior to JDK&nbsp;1.1, the class <code>Date</code> had two additional
0N/A * functions. It allowed the interpretation of dates as year, month, day, hour,
0N/A * minute, and second values. It also allowed the formatting and parsing
0N/A * of date strings. Unfortunately, the API for these functions was not
0N/A * amenable to internationalization. As of JDK&nbsp;1.1, the
0N/A * <code>Calendar</code> class should be used to convert between dates and time
0N/A * fields and the <code>DateFormat</code> class should be used to format and
0N/A * parse date strings.
0N/A * The corresponding methods in <code>Date</code> are deprecated.
0N/A * <p>
0N/A * Although the <code>Date</code> class is intended to reflect
0N/A * coordinated universal time (UTC), it may not do so exactly,
0N/A * depending on the host environment of the Java Virtual Machine.
0N/A * Nearly all modern operating systems assume that 1&nbsp;day&nbsp;=
0N/A * 24&nbsp;&times;&nbsp;60&nbsp;&times;&nbsp;60&nbsp;= 86400 seconds
0N/A * in all cases. In UTC, however, about once every year or two there
0N/A * is an extra second, called a "leap second." The leap
0N/A * second is always added as the last second of the day, and always
0N/A * on December 31 or June 30. For example, the last minute of the
0N/A * year 1995 was 61 seconds long, thanks to an added leap second.
0N/A * Most computer clocks are not accurate enough to be able to reflect
0N/A * the leap-second distinction.
0N/A * <p>
0N/A * Some computer standards are defined in terms of Greenwich mean
0N/A * time (GMT), which is equivalent to universal time (UT). GMT is
0N/A * the "civil" name for the standard; UT is the
0N/A * "scientific" name for the same standard. The
0N/A * distinction between UTC and UT is that UTC is based on an atomic
0N/A * clock and UT is based on astronomical observations, which for all
0N/A * practical purposes is an invisibly fine hair to split. Because the
0N/A * earth's rotation is not uniform (it slows down and speeds up
0N/A * in complicated ways), UT does not always flow uniformly. Leap
0N/A * seconds are introduced as needed into UTC so as to keep UTC within
0N/A * 0.9 seconds of UT1, which is a version of UT with certain
0N/A * corrections applied. There are other time and date systems as
0N/A * well; for example, the time scale used by the satellite-based
0N/A * global positioning system (GPS) is synchronized to UTC but is
0N/A * <i>not</i> adjusted for leap seconds. An interesting source of
0N/A * further information is the U.S. Naval Observatory, particularly
0N/A * the Directorate of Time at:
0N/A * <blockquote><pre>
417N/A * <a href=http://tycho.usno.navy.mil>http://tycho.usno.navy.mil</a>
417N/A * </pre></blockquote>
0N/A * <p>
417N/A * and their definitions of "Systems of Time" at:
0N/A * <blockquote><pre>
417N/A * <a href=http://tycho.usno.navy.mil/systime.html>http://tycho.usno.navy.mil/systime.html</a>
0N/A * </pre></blockquote>
0N/A * <p>
0N/A * In all methods of class <code>Date</code> that accept or return
0N/A * year, month, date, hours, minutes, and seconds values, the
0N/A * following representations are used:
0N/A * <ul>
0N/A * <li>A year <i>y</i> is represented by the integer
0N/A * <i>y</i>&nbsp;<code>-&nbsp;1900</code>.
0N/A * <li>A month is represented by an integer from 0 to 11; 0 is January,
0N/A * 1 is February, and so forth; thus 11 is December.
0N/A * <li>A date (day of month) is represented by an integer from 1 to 31
0N/A * in the usual manner.
0N/A * <li>An hour is represented by an integer from 0 to 23. Thus, the hour
0N/A * from midnight to 1 a.m. is hour 0, and the hour from noon to 1
0N/A * p.m. is hour 12.
0N/A * <li>A minute is represented by an integer from 0 to 59 in the usual manner.
0N/A * <li>A second is represented by an integer from 0 to 61; the values 60 and
0N/A * 61 occur only for leap seconds and even then only in Java
1204N/A * implementations that actually track leap seconds correctly. Because
1204N/A * of the manner in which leap seconds are currently introduced, it is
1204N/A * extremely unlikely that two leap seconds will occur in the same
1204N/A * minute, but this specification follows the date and time conventions
1204N/A * for ISO C.
0N/A * </ul>
0N/A * <p>
0N/A * In all cases, arguments given to methods for these purposes need
0N/A * not fall within the indicated ranges; for example, a date may be
0N/A * specified as January 32 and is interpreted as meaning February 1.
0N/A *
0N/A * @author James Gosling
0N/A * @author Arthur van Hoff
0N/A * @author Alan Liu
0N/A * @see java.text.DateFormat
0N/A * @see java.util.Calendar
0N/A * @see java.util.TimeZone
0N/A * @since JDK1.0
0N/A */
0N/Apublic class Date
0N/A implements java.io.Serializable, Cloneable, Comparable<Date>
0N/A{
0N/A private static final BaseCalendar gcal =
0N/A CalendarSystem.getGregorianCalendar();
0N/A private static BaseCalendar jcal;
0N/A
0N/A private transient long fastTime;
0N/A
0N/A /*
0N/A * If cdate is null, then fastTime indicates the time in millis.
0N/A * If cdate.isNormalized() is true, then fastTime and cdate are in
0N/A * synch. Otherwise, fastTime is ignored, and cdate indicates the
0N/A * time.
0N/A */
0N/A private transient BaseCalendar.Date cdate;
0N/A
0N/A // Initialized just before the value is used. See parse().
0N/A private static int defaultCenturyStart;
0N/A
0N/A /* use serialVersionUID from modified java.util.Date for
0N/A * interoperability with JDK1.1. The Date was modified to write
0N/A * and read only the UTC time.
0N/A */
0N/A private static final long serialVersionUID = 7523967970034938905L;
0N/A
0N/A /**
0N/A * Allocates a <code>Date</code> object and initializes it so that
0N/A * it represents the time at which it was allocated, measured to the
0N/A * nearest millisecond.
0N/A *
0N/A * @see java.lang.System#currentTimeMillis()
0N/A */
0N/A public Date() {
0N/A this(System.currentTimeMillis());
0N/A }
0N/A
0N/A /**
0N/A * Allocates a <code>Date</code> object and initializes it to
0N/A * represent the specified number of milliseconds since the
0N/A * standard base time known as "the epoch", namely January 1,
0N/A * 1970, 00:00:00 GMT.
0N/A *
0N/A * @param date the milliseconds since January 1, 1970, 00:00:00 GMT.
0N/A * @see java.lang.System#currentTimeMillis()
0N/A */
0N/A public Date(long date) {
0N/A fastTime = date;
0N/A }
0N/A
0N/A /**
0N/A * Allocates a <code>Date</code> object and initializes it so that
0N/A * it represents midnight, local time, at the beginning of the day
0N/A * specified by the <code>year</code>, <code>month</code>, and
0N/A * <code>date</code> arguments.
0N/A *
0N/A * @param year the year minus 1900.
0N/A * @param month the month between 0-11.
0N/A * @param date the day of the month between 1-31.
0N/A * @see java.util.Calendar
0N/A * @deprecated As of JDK version 1.1,
0N/A * replaced by <code>Calendar.set(year + 1900, month, date)</code>
0N/A * or <code>GregorianCalendar(year + 1900, month, date)</code>.
0N/A */
0N/A @Deprecated
0N/A public Date(int year, int month, int date) {
0N/A this(year, month, date, 0, 0, 0);
0N/A }
0N/A
0N/A /**
0N/A * Allocates a <code>Date</code> object and initializes it so that
0N/A * it represents the instant at the start of the minute specified by
0N/A * the <code>year</code>, <code>month</code>, <code>date</code>,
0N/A * <code>hrs</code>, and <code>min</code> arguments, in the local
0N/A * time zone.
0N/A *
0N/A * @param year the year minus 1900.
0N/A * @param month the month between 0-11.
0N/A * @param date the day of the month between 1-31.
0N/A * @param hrs the hours between 0-23.
0N/A * @param min the minutes between 0-59.
0N/A * @see java.util.Calendar
0N/A * @deprecated As of JDK version 1.1,
0N/A * replaced by <code>Calendar.set(year + 1900, month, date,
0N/A * hrs, min)</code> or <code>GregorianCalendar(year + 1900,
0N/A * month, date, hrs, min)</code>.
0N/A */
0N/A @Deprecated
0N/A public Date(int year, int month, int date, int hrs, int min) {
0N/A this(year, month, date, hrs, min, 0);
0N/A }
0N/A
0N/A /**
0N/A * Allocates a <code>Date</code> object and initializes it so that
0N/A * it represents the instant at the start of the second specified
0N/A * by the <code>year</code>, <code>month</code>, <code>date</code>,
0N/A * <code>hrs</code>, <code>min</code>, and <code>sec</code> arguments,
0N/A * in the local time zone.
0N/A *
0N/A * @param year the year minus 1900.
0N/A * @param month the month between 0-11.
0N/A * @param date the day of the month between 1-31.
0N/A * @param hrs the hours between 0-23.
0N/A * @param min the minutes between 0-59.
0N/A * @param sec the seconds between 0-59.
0N/A * @see java.util.Calendar
0N/A * @deprecated As of JDK version 1.1,
0N/A * replaced by <code>Calendar.set(year + 1900, month, date,
0N/A * hrs, min, sec)</code> or <code>GregorianCalendar(year + 1900,
0N/A * month, date, hrs, min, sec)</code>.
0N/A */
0N/A @Deprecated
0N/A public Date(int year, int month, int date, int hrs, int min, int sec) {
0N/A int y = year + 1900;
0N/A // month is 0-based. So we have to normalize month to support Long.MAX_VALUE.
0N/A if (month >= 12) {
0N/A y += month / 12;
0N/A month %= 12;
0N/A } else if (month < 0) {
0N/A y += CalendarUtils.floorDivide(month, 12);
0N/A month = CalendarUtils.mod(month, 12);
0N/A }
0N/A BaseCalendar cal = getCalendarSystem(y);
0N/A cdate = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.getDefaultRef());
0N/A cdate.setNormalizedDate(y, month + 1, date).setTimeOfDay(hrs, min, sec, 0);
0N/A getTimeImpl();
0N/A cdate = null;
0N/A }
0N/A
0N/A /**
0N/A * Allocates a <code>Date</code> object and initializes it so that
0N/A * it represents the date and time indicated by the string
0N/A * <code>s</code>, which is interpreted as if by the
0N/A * {@link Date#parse} method.
0N/A *
0N/A * @param s a string representation of the date.
0N/A * @see java.text.DateFormat
0N/A * @see java.util.Date#parse(java.lang.String)
0N/A * @deprecated As of JDK version 1.1,
0N/A * replaced by <code>DateFormat.parse(String s)</code>.
0N/A */
0N/A @Deprecated
0N/A public Date(String s) {
0N/A this(parse(s));
0N/A }
0N/A
0N/A /**
1204N/A * Return a copy of this object.
1204N/A */
1204N/A public Object clone() {
1204N/A Date d = null;
1204N/A try {
1204N/A d = (Date)super.clone();
0N/A if (cdate != null) {
0N/A d.cdate = (BaseCalendar.Date) cdate.clone();
0N/A }
1204N/A } catch (CloneNotSupportedException e) {} // Won't happen
0N/A return d;
0N/A }
0N/A
0N/A /**
0N/A * Determines the date and time based on the arguments. The
0N/A * arguments are interpreted as a year, month, day of the month,
0N/A * hour of the day, minute within the hour, and second within the
0N/A * minute, exactly as for the <tt>Date</tt> constructor with six
0N/A * arguments, except that the arguments are interpreted relative
0N/A * to UTC rather than to the local time zone. The time indicated is
0N/A * returned represented as the distance, measured in milliseconds,
0N/A * of that time from the epoch (00:00:00 GMT on January 1, 1970).
0N/A *
0N/A * @param year the year minus 1900.
0N/A * @param month the month between 0-11.
0N/A * @param date the day of the month between 1-31.
0N/A * @param hrs the hours between 0-23.
0N/A * @param min the minutes between 0-59.
0N/A * @param sec the seconds between 0-59.
0N/A * @return the number of milliseconds since January 1, 1970, 00:00:00 GMT for
0N/A * the date and time specified by the arguments.
0N/A * @see java.util.Calendar
0N/A * @deprecated As of JDK version 1.1,
0N/A * replaced by <code>Calendar.set(year + 1900, month, date,
0N/A * hrs, min, sec)</code> or <code>GregorianCalendar(year + 1900,
0N/A * month, date, hrs, min, sec)</code>, using a UTC
0N/A * <code>TimeZone</code>, followed by <code>Calendar.getTime().getTime()</code>.
0N/A */
0N/A @Deprecated
0N/A public static long UTC(int year, int month, int date,
0N/A int hrs, int min, int sec) {
0N/A int y = year + 1900;
0N/A // month is 0-based. So we have to normalize month to support Long.MAX_VALUE.
0N/A if (month >= 12) {
0N/A y += month / 12;
0N/A month %= 12;
0N/A } else if (month < 0) {
0N/A y += CalendarUtils.floorDivide(month, 12);
0N/A month = CalendarUtils.mod(month, 12);
0N/A }
0N/A int m = month + 1;
0N/A BaseCalendar cal = getCalendarSystem(y);
0N/A BaseCalendar.Date udate = (BaseCalendar.Date) cal.newCalendarDate(null);
0N/A udate.setNormalizedDate(y, m, date).setTimeOfDay(hrs, min, sec, 0);
0N/A
0N/A // Use a Date instance to perform normalization. Its fastTime
0N/A // is the UTC value after the normalization.
0N/A Date d = new Date(0);
0N/A d.normalize(udate);
0N/A return d.fastTime;
0N/A }
0N/A
0N/A /**
0N/A * Attempts to interpret the string <tt>s</tt> as a representation
0N/A * of a date and time. If the attempt is successful, the time
0N/A * indicated is returned represented as the distance, measured in
0N/A * milliseconds, of that time from the epoch (00:00:00 GMT on
0N/A * January 1, 1970). If the attempt fails, an
0N/A * <tt>IllegalArgumentException</tt> is thrown.
0N/A * <p>
0N/A * It accepts many syntaxes; in particular, it recognizes the IETF
0N/A * standard date syntax: "Sat, 12 Aug 1995 13:30:00 GMT". It also
0N/A * understands the continental U.S. time-zone abbreviations, but for
0N/A * general use, a time-zone offset should be used: "Sat, 12 Aug 1995
0N/A * 13:30:00 GMT+0430" (4 hours, 30 minutes west of the Greenwich
0N/A * meridian). If no time zone is specified, the local time zone is
0N/A * assumed. GMT and UTC are considered equivalent.
0N/A * <p>
0N/A * The string <tt>s</tt> is processed from left to right, looking for
0N/A * data of interest. Any material in <tt>s</tt> that is within the
0N/A * ASCII parenthesis characters <tt>(</tt> and <tt>)</tt> is ignored.
0N/A * Parentheses may be nested. Otherwise, the only characters permitted
0N/A * within <tt>s</tt> are these ASCII characters:
0N/A * <blockquote><pre>
0N/A * abcdefghijklmnopqrstuvwxyz
0N/A * ABCDEFGHIJKLMNOPQRSTUVWXYZ
0N/A * 0123456789,+-:/</pre></blockquote>
0N/A * and whitespace characters.<p>
0N/A * A consecutive sequence of decimal digits is treated as a decimal
0N/A * number:<ul>
0N/A * <li>If a number is preceded by <tt>+</tt> or <tt>-</tt> and a year
0N/A * has already been recognized, then the number is a time-zone
0N/A * offset. If the number is less than 24, it is an offset measured
0N/A * in hours. Otherwise, it is regarded as an offset in minutes,
0N/A * expressed in 24-hour time format without punctuation. A
0N/A * preceding <tt>-</tt> means a westward offset. Time zone offsets
0N/A * are always relative to UTC (Greenwich). Thus, for example,
0N/A * <tt>-5</tt> occurring in the string would mean "five hours west
0N/A * of Greenwich" and <tt>+0430</tt> would mean "four hours and
0N/A * thirty minutes east of Greenwich." It is permitted for the
0N/A * string to specify <tt>GMT</tt>, <tt>UT</tt>, or <tt>UTC</tt>
0N/A * redundantly-for example, <tt>GMT-5</tt> or <tt>utc+0430</tt>.
0N/A * <li>The number is regarded as a year number if one of the
0N/A * following conditions is true:
0N/A * <ul>
0N/A * <li>The number is equal to or greater than 70 and followed by a
0N/A * space, comma, slash, or end of string
0N/A * <li>The number is less than 70, and both a month and a day of
0N/A * the month have already been recognized</li>
0N/A * </ul>
0N/A * If the recognized year number is less than 100, it is
0N/A * interpreted as an abbreviated year relative to a century of
0N/A * which dates are within 80 years before and 19 years after
0N/A * the time when the Date class is initialized.
0N/A * After adjusting the year number, 1900 is subtracted from
0N/A * it. For example, if the current year is 1999 then years in
0N/A * the range 19 to 99 are assumed to mean 1919 to 1999, while
0N/A * years from 0 to 18 are assumed to mean 2000 to 2018. Note
0N/A * that this is slightly different from the interpretation of
0N/A * years less than 100 that is used in {@link java.text.SimpleDateFormat}.
0N/A * <li>If the number is followed by a colon, it is regarded as an hour,
0N/A * unless an hour has already been recognized, in which case it is
0N/A * regarded as a minute.
0N/A * <li>If the number is followed by a slash, it is regarded as a month
0N/A * (it is decreased by 1 to produce a number in the range <tt>0</tt>
0N/A * to <tt>11</tt>), unless a month has already been recognized, in
0N/A * which case it is regarded as a day of the month.
0N/A * <li>If the number is followed by whitespace, a comma, a hyphen, or
0N/A * end of string, then if an hour has been recognized but not a
0N/A * minute, it is regarded as a minute; otherwise, if a minute has
0N/A * been recognized but not a second, it is regarded as a second;
0N/A * otherwise, it is regarded as a day of the month. </ul><p>
0N/A * A consecutive sequence of letters is regarded as a word and treated
0N/A * as follows:<ul>
0N/A * <li>A word that matches <tt>AM</tt>, ignoring case, is ignored (but
0N/A * the parse fails if an hour has not been recognized or is less
0N/A * than <tt>1</tt> or greater than <tt>12</tt>).
0N/A * <li>A word that matches <tt>PM</tt>, ignoring case, adds <tt>12</tt>
0N/A * to the hour (but the parse fails if an hour has not been
0N/A * recognized or is less than <tt>1</tt> or greater than <tt>12</tt>).
0N/A * <li>Any word that matches any prefix of <tt>SUNDAY, MONDAY, TUESDAY,
0N/A * WEDNESDAY, THURSDAY, FRIDAY</tt>, or <tt>SATURDAY</tt>, ignoring
0N/A * case, is ignored. For example, <tt>sat, Friday, TUE</tt>, and
0N/A * <tt>Thurs</tt> are ignored.
0N/A * <li>Otherwise, any word that matches any prefix of <tt>JANUARY,
0N/A * FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER,
0N/A * OCTOBER, NOVEMBER</tt>, or <tt>DECEMBER</tt>, ignoring case, and
0N/A * considering them in the order given here, is recognized as
0N/A * specifying a month and is converted to a number (<tt>0</tt> to
0N/A * <tt>11</tt>). For example, <tt>aug, Sept, april</tt>, and
0N/A * <tt>NOV</tt> are recognized as months. So is <tt>Ma</tt>, which
0N/A * is recognized as <tt>MARCH</tt>, not <tt>MAY</tt>.
0N/A * <li>Any word that matches <tt>GMT, UT</tt>, or <tt>UTC</tt>, ignoring
0N/A * case, is treated as referring to UTC.
0N/A * <li>Any word that matches <tt>EST, CST, MST</tt>, or <tt>PST</tt>,
0N/A * ignoring case, is recognized as referring to the time zone in
0N/A * North America that is five, six, seven, or eight hours west of
0N/A * Greenwich, respectively. Any word that matches <tt>EDT, CDT,
0N/A * MDT</tt>, or <tt>PDT</tt>, ignoring case, is recognized as
0N/A * referring to the same time zone, respectively, during daylight
0N/A * saving time.</ul><p>
0N/A * Once the entire string s has been scanned, it is converted to a time
0N/A * result in one of two ways. If a time zone or time-zone offset has been
0N/A * recognized, then the year, month, day of month, hour, minute, and
0N/A * second are interpreted in UTC and then the time-zone offset is
0N/A * applied. Otherwise, the year, month, day of month, hour, minute, and
0N/A * second are interpreted in the local time zone.
0N/A *
0N/A * @param s a string to be parsed as a date.
0N/A * @return the number of milliseconds since January 1, 1970, 00:00:00 GMT
0N/A * represented by the string argument.
0N/A * @see java.text.DateFormat
0N/A * @deprecated As of JDK version 1.1,
0N/A * replaced by <code>DateFormat.parse(String s)</code>.
0N/A */
0N/A @Deprecated
0N/A public static long parse(String s) {
0N/A int year = Integer.MIN_VALUE;
0N/A int mon = -1;
0N/A int mday = -1;
0N/A int hour = -1;
0N/A int min = -1;
0N/A int sec = -1;
0N/A int millis = -1;
0N/A int c = -1;
0N/A int i = 0;
0N/A int n = -1;
0N/A int wst = -1;
0N/A int tzoffset = -1;
0N/A int prevc = 0;
0N/A syntax:
0N/A {
0N/A if (s == null)
0N/A break syntax;
0N/A int limit = s.length();
0N/A while (i < limit) {
0N/A c = s.charAt(i);
1426N/A i++;
0N/A if (c <= ' ' || c == ',')
0N/A continue;
0N/A if (c == '(') { // skip comments
0N/A int depth = 1;
0N/A while (i < limit) {
0N/A c = s.charAt(i);
1426N/A i++;
0N/A if (c == '(') depth++;
0N/A else if (c == ')')
0N/A if (--depth <= 0)
0N/A break;
0N/A }
0N/A continue;
0N/A }
0N/A if ('0' <= c && c <= '9') {
0N/A n = c - '0';
0N/A while (i < limit && '0' <= (c = s.charAt(i)) && c <= '9') {
0N/A n = n * 10 + c - '0';
0N/A i++;
0N/A }
0N/A if (prevc == '+' || prevc == '-' && year != Integer.MIN_VALUE) {
0N/A // timezone offset
0N/A if (n < 24)
0N/A n = n * 60; // EG. "GMT-3"
0N/A else
0N/A n = n % 100 + n / 100 * 60; // eg "GMT-0430"
0N/A if (prevc == '+') // plus means east of GMT
0N/A n = -n;
0N/A if (tzoffset != 0 && tzoffset != -1)
0N/A break syntax;
0N/A tzoffset = n;
0N/A } else if (n >= 70)
0N/A if (year != Integer.MIN_VALUE)
0N/A break syntax;
0N/A else if (c <= ' ' || c == ',' || c == '/' || i >= limit)
0N/A // year = n < 1900 ? n : n - 1900;
0N/A year = n;
0N/A else
0N/A break syntax;
0N/A else if (c == ':')
0N/A if (hour < 0)
0N/A hour = (byte) n;
0N/A else if (min < 0)
0N/A min = (byte) n;
0N/A else
0N/A break syntax;
0N/A else if (c == '/')
0N/A if (mon < 0)
0N/A mon = (byte) (n - 1);
0N/A else if (mday < 0)
0N/A mday = (byte) n;
0N/A else
0N/A break syntax;
0N/A else if (i < limit && c != ',' && c > ' ' && c != '-')
0N/A break syntax;
0N/A else if (hour >= 0 && min < 0)
0N/A min = (byte) n;
0N/A else if (min >= 0 && sec < 0)
0N/A sec = (byte) n;
0N/A else if (mday < 0)
0N/A mday = (byte) n;
0N/A // Handle two-digit years < 70 (70-99 handled above).
0N/A else if (year == Integer.MIN_VALUE && mon >= 0 && mday >= 0)
0N/A year = n;
0N/A else
0N/A break syntax;
0N/A prevc = 0;
0N/A } else if (c == '/' || c == ':' || c == '+' || c == '-')
0N/A prevc = c;
0N/A else {
0N/A int st = i - 1;
0N/A while (i < limit) {
0N/A c = s.charAt(i);
0N/A if (!('A' <= c && c <= 'Z' || 'a' <= c && c <= 'z'))
0N/A break;
0N/A i++;
0N/A }
0N/A if (i <= st + 1)
0N/A break syntax;
0N/A int k;
0N/A for (k = wtb.length; --k >= 0;)
0N/A if (wtb[k].regionMatches(true, 0, s, st, i - st)) {
0N/A int action = ttb[k];
0N/A if (action != 0) {
0N/A if (action == 1) { // pm
0N/A if (hour > 12 || hour < 1)
0N/A break syntax;
0N/A else if (hour < 12)
0N/A hour += 12;
0N/A } else if (action == 14) { // am
0N/A if (hour > 12 || hour < 1)
0N/A break syntax;
0N/A else if (hour == 12)
0N/A hour = 0;
0N/A } else if (action <= 13) { // month!
0N/A if (mon < 0)
0N/A mon = (byte) (action - 2);
0N/A else
0N/A break syntax;
0N/A } else {
0N/A tzoffset = action - 10000;
1255N/A }
0N/A }
1255N/A break;
1255N/A }
0N/A if (k < 0)
1255N/A break syntax;
0N/A prevc = 0;
0N/A }
0N/A }
0N/A if (year == Integer.MIN_VALUE || mon < 0 || mday < 0)
0N/A break syntax;
0N/A // Parse 2-digit years within the correct default century.
0N/A if (year < 100) {
0N/A synchronized (Date.class) {
0N/A if (defaultCenturyStart == 0) {
0N/A defaultCenturyStart = gcal.getCalendarDate().getYear() - 80;
0N/A }
0N/A }
0N/A year += (defaultCenturyStart / 100) * 100;
0N/A if (year < defaultCenturyStart) year += 100;
0N/A }
0N/A if (sec < 0)
0N/A sec = 0;
0N/A if (min < 0)
0N/A min = 0;
0N/A if (hour < 0)
0N/A hour = 0;
0N/A BaseCalendar cal = getCalendarSystem(year);
0N/A if (tzoffset == -1) { // no time zone specified, have to use local
0N/A BaseCalendar.Date ldate = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.getDefaultRef());
0N/A ldate.setDate(year, mon + 1, mday);
0N/A ldate.setTimeOfDay(hour, min, sec, 0);
0N/A return cal.getTime(ldate);
0N/A }
0N/A BaseCalendar.Date udate = (BaseCalendar.Date) cal.newCalendarDate(null); // no time zone
0N/A udate.setDate(year, mon + 1, mday);
0N/A udate.setTimeOfDay(hour, min, sec, 0);
0N/A return cal.getTime(udate) + tzoffset * (60 * 1000);
0N/A }
0N/A // syntax error
0N/A throw new IllegalArgumentException();
0N/A }
0N/A private final static String wtb[] = {
0N/A "am", "pm",
0N/A "monday", "tuesday", "wednesday", "thursday", "friday",
0N/A "saturday", "sunday",
0N/A "january", "february", "march", "april", "may", "june",
0N/A "july", "august", "september", "october", "november", "december",
0N/A "gmt", "ut", "utc", "est", "edt", "cst", "cdt",
0N/A "mst", "mdt", "pst", "pdt"
0N/A };
0N/A private final static int ttb[] = {
0N/A 14, 1, 0, 0, 0, 0, 0, 0, 0,
0N/A 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
0N/A 10000 + 0, 10000 + 0, 10000 + 0, // GMT/UT/UTC
0N/A 10000 + 5 * 60, 10000 + 4 * 60, // EST/EDT
0N/A 10000 + 6 * 60, 10000 + 5 * 60, // CST/CDT
0N/A 10000 + 7 * 60, 10000 + 6 * 60, // MST/MDT
0N/A 10000 + 8 * 60, 10000 + 7 * 60 // PST/PDT
0N/A };
0N/A
0N/A /**
0N/A * Returns a value that is the result of subtracting 1900 from the
0N/A * year that contains or begins with the instant in time represented
0N/A * by this <code>Date</code> object, as interpreted in the local
0N/A * time zone.
0N/A *
0N/A * @return the year represented by this date, minus 1900.
0N/A * @see java.util.Calendar
0N/A * @deprecated As of JDK version 1.1,
0N/A * replaced by <code>Calendar.get(Calendar.YEAR) - 1900</code>.
0N/A */
0N/A @Deprecated
0N/A public int getYear() {
0N/A return normalize().getYear() - 1900;
0N/A }
0N/A
0N/A /**
0N/A * Sets the year of this <tt>Date</tt> object to be the specified
0N/A * value plus 1900. This <code>Date</code> object is modified so
0N/A * that it represents a point in time within the specified year,
0N/A * with the month, date, hour, minute, and second the same as
0N/A * before, as interpreted in the local time zone. (Of course, if
0N/A * the date was February 29, for example, and the year is set to a
0N/A * non-leap year, then the new date will be treated as if it were
0N/A * on March 1.)
0N/A *
0N/A * @param year the year value.
0N/A * @see java.util.Calendar
0N/A * @deprecated As of JDK version 1.1,
0N/A * replaced by <code>Calendar.set(Calendar.YEAR, year + 1900)</code>.
0N/A */
0N/A @Deprecated
0N/A public void setYear(int year) {
0N/A getCalendarDate().setNormalizedYear(year + 1900);
0N/A }
0N/A
0N/A /**
0N/A * Returns a number representing the month that contains or begins
0N/A * with the instant in time represented by this <tt>Date</tt> object.
0N/A * The value returned is between <code>0</code> and <code>11</code>,
0N/A * with the value <code>0</code> representing January.
0N/A *
0N/A * @return the month represented by this date.
0N/A * @see java.util.Calendar
0N/A * @deprecated As of JDK version 1.1,
0N/A * replaced by <code>Calendar.get(Calendar.MONTH)</code>.
0N/A */
0N/A @Deprecated
0N/A public int getMonth() {
0N/A return normalize().getMonth() - 1; // adjust 1-based to 0-based
0N/A }
0N/A
0N/A /**
0N/A * Sets the month of this date to the specified value. This
0N/A * <tt>Date</tt> object is modified so that it represents a point
0N/A * in time within the specified month, with the year, date, hour,
0N/A * minute, and second the same as before, as interpreted in the
0N/A * local time zone. If the date was October 31, for example, and
0N/A * the month is set to June, then the new date will be treated as
0N/A * if it were on July 1, because June has only 30 days.
0N/A *
0N/A * @param month the month value between 0-11.
0N/A * @see java.util.Calendar
0N/A * @deprecated As of JDK version 1.1,
0N/A * replaced by <code>Calendar.set(Calendar.MONTH, int month)</code>.
0N/A */
0N/A @Deprecated
0N/A public void setMonth(int month) {
0N/A int y = 0;
0N/A if (month >= 12) {
0N/A y = month / 12;
0N/A month %= 12;
0N/A } else if (month < 0) {
0N/A y = CalendarUtils.floorDivide(month, 12);
0N/A month = CalendarUtils.mod(month, 12);
0N/A }
0N/A BaseCalendar.Date d = getCalendarDate();
0N/A if (y != 0) {
0N/A d.setNormalizedYear(d.getNormalizedYear() + y);
0N/A }
0N/A d.setMonth(month + 1); // adjust 0-based to 1-based month numbering
0N/A }
0N/A
0N/A /**
0N/A * Returns the day of the month represented by this <tt>Date</tt> object.
0N/A * The value returned is between <code>1</code> and <code>31</code>
0N/A * representing the day of the month that contains or begins with the
0N/A * instant in time represented by this <tt>Date</tt> object, as
0N/A * interpreted in the local time zone.
0N/A *
0N/A * @return the day of the month represented by this date.
0N/A * @see java.util.Calendar
0N/A * @deprecated As of JDK version 1.1,
0N/A * replaced by <code>Calendar.get(Calendar.DAY_OF_MONTH)</code>.
0N/A * @deprecated
0N/A */
0N/A @Deprecated
0N/A public int getDate() {
0N/A return normalize().getDayOfMonth();
0N/A }
0N/A
0N/A /**
0N/A * Sets the day of the month of this <tt>Date</tt> object to the
0N/A * specified value. This <tt>Date</tt> object is modified so that
0N/A * it represents a point in time within the specified day of the
0N/A * month, with the year, month, hour, minute, and second the same
0N/A * as before, as interpreted in the local time zone. If the date
0N/A * was April 30, for example, and the date is set to 31, then it
0N/A * will be treated as if it were on May 1, because April has only
0N/A * 30 days.
0N/A *
0N/A * @param date the day of the month value between 1-31.
1138N/A * @see java.util.Calendar
1138N/A * @deprecated As of JDK version 1.1,
1138N/A * replaced by <code>Calendar.set(Calendar.DAY_OF_MONTH, int date)</code>.
0N/A */
0N/A @Deprecated
0N/A public void setDate(int date) {
0N/A getCalendarDate().setDayOfMonth(date);
0N/A }
0N/A
0N/A /**
0N/A * Returns the day of the week represented by this date. The
0N/A * returned value (<tt>0</tt> = Sunday, <tt>1</tt> = Monday,
0N/A * <tt>2</tt> = Tuesday, <tt>3</tt> = Wednesday, <tt>4</tt> =
0N/A * Thursday, <tt>5</tt> = Friday, <tt>6</tt> = Saturday)
0N/A * represents the day of the week that contains or begins with
0N/A * the instant in time represented by this <tt>Date</tt> object,
0N/A * as interpreted in the local time zone.
1138N/A *
0N/A * @return the day of the week represented by this date.
1138N/A * @see java.util.Calendar
0N/A * @deprecated As of JDK version 1.1,
0N/A * replaced by <code>Calendar.get(Calendar.DAY_OF_WEEK)</code>.
0N/A */
0N/A @Deprecated
0N/A public int getDay() {
0N/A return normalize().getDayOfWeek() - gcal.SUNDAY;
0N/A }
0N/A
0N/A /**
0N/A * Returns the hour represented by this <tt>Date</tt> object. The
1138N/A * returned value is a number (<tt>0</tt> through <tt>23</tt>)
0N/A * representing the hour within the day that contains or begins
0N/A * with the instant in time represented by this <tt>Date</tt>
0N/A * object, as interpreted in the local time zone.
0N/A *
0N/A * @return the hour represented by this date.
0N/A * @see java.util.Calendar
0N/A * @deprecated As of JDK version 1.1,
0N/A * replaced by <code>Calendar.get(Calendar.HOUR_OF_DAY)</code>.
0N/A */
0N/A @Deprecated
0N/A public int getHours() {
0N/A return normalize().getHours();
0N/A }
0N/A
0N/A /**
0N/A * Sets the hour of this <tt>Date</tt> object to the specified value.
0N/A * This <tt>Date</tt> object is modified so that it represents a point
0N/A * in time within the specified hour of the day, with the year, month,
0N/A * date, minute, and second the same as before, as interpreted in the
0N/A * local time zone.
0N/A *
0N/A * @param hours the hour value.
0N/A * @see java.util.Calendar
0N/A * @deprecated As of JDK version 1.1,
0N/A * replaced by <code>Calendar.set(Calendar.HOUR_OF_DAY, int hours)</code>.
0N/A */
0N/A @Deprecated
0N/A public void setHours(int hours) {
0N/A getCalendarDate().setHours(hours);
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of minutes past the hour represented by this date,
0N/A * as interpreted in the local time zone.
0N/A * The value returned is between <code>0</code> and <code>59</code>.
0N/A *
0N/A * @return the number of minutes past the hour represented by this date.
0N/A * @see java.util.Calendar
0N/A * @deprecated As of JDK version 1.1,
0N/A * replaced by <code>Calendar.get(Calendar.MINUTE)</code>.
0N/A */
0N/A @Deprecated
0N/A public int getMinutes() {
0N/A return normalize().getMinutes();
0N/A }
0N/A
0N/A /**
0N/A * Sets the minutes of this <tt>Date</tt> object to the specified value.
0N/A * This <tt>Date</tt> object is modified so that it represents a point
0N/A * in time within the specified minute of the hour, with the year, month,
0N/A * date, hour, and second the same as before, as interpreted in the
0N/A * local time zone.
0N/A *
0N/A * @param minutes the value of the minutes.
0N/A * @see java.util.Calendar
0N/A * @deprecated As of JDK version 1.1,
0N/A * replaced by <code>Calendar.set(Calendar.MINUTE, int minutes)</code>.
0N/A */
0N/A @Deprecated
0N/A public void setMinutes(int minutes) {
0N/A getCalendarDate().setMinutes(minutes);
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of seconds past the minute represented by this date.
0N/A * The value returned is between <code>0</code> and <code>61</code>. The
0N/A * values <code>60</code> and <code>61</code> can only occur on those
0N/A * Java Virtual Machines that take leap seconds into account.
0N/A *
0N/A * @return the number of seconds past the minute represented by this date.
0N/A * @see java.util.Calendar
0N/A * @deprecated As of JDK version 1.1,
0N/A * replaced by <code>Calendar.get(Calendar.SECOND)</code>.
0N/A */
0N/A @Deprecated
0N/A public int getSeconds() {
0N/A return normalize().getSeconds();
0N/A }
0N/A
0N/A /**
0N/A * Sets the seconds of this <tt>Date</tt> to the specified value.
0N/A * This <tt>Date</tt> object is modified so that it represents a
0N/A * point in time within the specified second of the minute, with
0N/A * the year, month, date, hour, and minute the same as before, as
0N/A * interpreted in the local time zone.
0N/A *
0N/A * @param seconds the seconds value.
0N/A * @see java.util.Calendar
0N/A * @deprecated As of JDK version 1.1,
0N/A * replaced by <code>Calendar.set(Calendar.SECOND, int seconds)</code>.
0N/A */
0N/A @Deprecated
0N/A public void setSeconds(int seconds) {
0N/A getCalendarDate().setSeconds(seconds);
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT
0N/A * represented by this <tt>Date</tt> object.
0N/A *
0N/A * @return the number of milliseconds since January 1, 1970, 00:00:00 GMT
0N/A * represented by this date.
0N/A */
0N/A public long getTime() {
0N/A return getTimeImpl();
0N/A }
0N/A
0N/A private final long getTimeImpl() {
0N/A if (cdate != null && !cdate.isNormalized()) {
0N/A normalize();
0N/A }
1138N/A return fastTime;
0N/A }
0N/A
0N/A /**
0N/A * Sets this <code>Date</code> object to represent a point in time that is
0N/A * <code>time</code> milliseconds after January 1, 1970 00:00:00 GMT.
0N/A *
0N/A * @param time the number of milliseconds.
0N/A */
0N/A public void setTime(long time) {
1138N/A fastTime = time;
0N/A cdate = null;
0N/A }
0N/A
0N/A /**
0N/A * Tests if this date is before the specified date.
0N/A *
0N/A * @param when a date.
605N/A * @return <code>true</code> if and only if the instant of time
0N/A * represented by this <tt>Date</tt> object is strictly
0N/A * earlier than the instant represented by <tt>when</tt>;
0N/A * <code>false</code> otherwise.
0N/A * @exception NullPointerException if <code>when</code> is null.
0N/A */
1138N/A public boolean before(Date when) {
0N/A return getMillisOf(this) < getMillisOf(when);
0N/A }
0N/A
0N/A /**
1426N/A * Tests if this date is after the specified date.
1426N/A *
1426N/A * @param when a date.
1426N/A * @return <code>true</code> if and only if the instant represented
1426N/A * by this <tt>Date</tt> object is strictly later than the
1426N/A * instant represented by <tt>when</tt>;
0N/A * <code>false</code> otherwise.
1426N/A * @exception NullPointerException if <code>when</code> is null.
0N/A */
1426N/A public boolean after(Date when) {
0N/A return getMillisOf(this) > getMillisOf(when);
0N/A }
0N/A
1138N/A /**
1138N/A * Compares two dates for equality.
0N/A * The result is <code>true</code> if and only if the argument is
0N/A * not <code>null</code> and is a <code>Date</code> object that
0N/A * represents the same point in time, to the millisecond, as this object.
989N/A * <p>
0N/A * Thus, two <code>Date</code> objects are equal if and only if the
0N/A * <code>getTime</code> method returns the same <code>long</code>
0N/A * value for both.
0N/A *
0N/A * @param obj the object to compare with.
0N/A * @return <code>true</code> if the objects are the same;
0N/A * <code>false</code> otherwise.
0N/A * @see java.util.Date#getTime()
0N/A */
0N/A public boolean equals(Object obj) {
0N/A return obj instanceof Date && getTime() == ((Date) obj).getTime();
0N/A }
0N/A
0N/A /**
989N/A * Returns the millisecond value of this <code>Date</code> object
989N/A * without affecting its internal state.
989N/A */
0N/A static final long getMillisOf(Date date) {
0N/A if (date.cdate == null || date.cdate.isNormalized()) {
0N/A return date.fastTime;
0N/A }
0N/A BaseCalendar.Date d = (BaseCalendar.Date) date.cdate.clone();
1138N/A return gcal.getTime(d);
1138N/A }
0N/A
0N/A /**
0N/A * Compares two Dates for ordering.
0N/A *
0N/A * @param anotherDate the <code>Date</code> to be compared.
0N/A * @return the value <code>0</code> if the argument Date is equal to
0N/A * this Date; a value less than <code>0</code> if this Date
0N/A * is before the Date argument; and a value greater than
0N/A * <code>0</code> if this Date is after the Date argument.
0N/A * @since 1.2
0N/A * @exception NullPointerException if <code>anotherDate</code> is null.
0N/A */
0N/A public int compareTo(Date anotherDate) {
0N/A long thisTime = getMillisOf(this);
0N/A long anotherTime = getMillisOf(anotherDate);
0N/A return (thisTime<anotherTime ? -1 : (thisTime==anotherTime ? 0 : 1));
0N/A }
0N/A
0N/A /**
1138N/A * Returns a hash code value for this object. The result is the
0N/A * exclusive OR of the two halves of the primitive <tt>long</tt>
0N/A * value returned by the {@link Date#getTime}
0N/A * method. That is, the hash code is the value of the expression:
0N/A * <blockquote><pre>
0N/A * (int)(this.getTime()^(this.getTime() >>> 32))</pre></blockquote>
1138N/A *
0N/A * @return a hash code value for this object.
0N/A */
1138N/A public int hashCode() {
0N/A long ht = this.getTime();
0N/A return (int) ht ^ (int) (ht >> 32);
1138N/A }
0N/A
0N/A /**
0N/A * Converts this <code>Date</code> object to a <code>String</code>
0N/A * of the form:
1138N/A * <blockquote><pre>
0N/A * dow mon dd hh:mm:ss zzz yyyy</pre></blockquote>
0N/A * where:<ul>
0N/A * <li><tt>dow</tt> is the day of the week (<tt>Sun, Mon, Tue, Wed,
0N/A * Thu, Fri, Sat</tt>).
0N/A * <li><tt>mon</tt> is the month (<tt>Jan, Feb, Mar, Apr, May, Jun,
0N/A * Jul, Aug, Sep, Oct, Nov, Dec</tt>).
0N/A * <li><tt>dd</tt> is the day of the month (<tt>01</tt> through
1138N/A * <tt>31</tt>), as two decimal digits.
0N/A * <li><tt>hh</tt> is the hour of the day (<tt>00</tt> through
1138N/A * <tt>23</tt>), as two decimal digits.
0N/A * <li><tt>mm</tt> is the minute within the hour (<tt>00</tt> through
0N/A * <tt>59</tt>), as two decimal digits.
0N/A * <li><tt>ss</tt> is the second within the minute (<tt>00</tt> through
0N/A * <tt>61</tt>, as two decimal digits.
0N/A * <li><tt>zzz</tt> is the time zone (and may reflect daylight saving
0N/A * time). Standard time zone abbreviations include those
0N/A * recognized by the method <tt>parse</tt>. If time zone
0N/A * information is not available, then <tt>zzz</tt> is empty -
0N/A * that is, it consists of no characters at all.
0N/A * <li><tt>yyyy</tt> is the year, as four decimal digits.
0N/A * </ul>
0N/A *
0N/A * @return a string representation of this date.
0N/A * @see java.util.Date#toLocaleString()
0N/A * @see java.util.Date#toGMTString()
0N/A */
0N/A public String toString() {
0N/A // "EEE MMM dd HH:mm:ss zzz yyyy";
0N/A BaseCalendar.Date date = normalize();
0N/A StringBuilder sb = new StringBuilder(28);
0N/A int index = date.getDayOfWeek();
0N/A if (index == gcal.SUNDAY) {
0N/A index = 8;
0N/A }
0N/A convertToAbbr(sb, wtb[index]).append(' '); // EEE
0N/A convertToAbbr(sb, wtb[date.getMonth() - 1 + 2 + 7]).append(' '); // MMM
113N/A CalendarUtils.sprintf0d(sb, date.getDayOfMonth(), 2).append(' '); // dd
113N/A
0N/A CalendarUtils.sprintf0d(sb, date.getHours(), 2).append(':'); // HH
0N/A CalendarUtils.sprintf0d(sb, date.getMinutes(), 2).append(':'); // mm
0N/A CalendarUtils.sprintf0d(sb, date.getSeconds(), 2).append(' '); // ss
0N/A TimeZone zi = date.getZone();
0N/A if (zi != null) {
0N/A sb.append(zi.getDisplayName(date.isDaylightTime(), zi.SHORT, Locale.US)); // zzz
0N/A } else {
0N/A sb.append("GMT");
0N/A }
0N/A sb.append(' ').append(date.getYear()); // yyyy
0N/A return sb.toString();
0N/A }
0N/A
0N/A /**
0N/A * Converts the given name to its 3-letter abbreviation (e.g.,
0N/A * "monday" -> "Mon") and stored the abbreviation in the given
0N/A * <code>StringBuilder</code>.
0N/A */
0N/A private static final StringBuilder convertToAbbr(StringBuilder sb, String name) {
0N/A sb.append(Character.toUpperCase(name.charAt(0)));
0N/A sb.append(name.charAt(1)).append(name.charAt(2));
0N/A return sb;
0N/A }
0N/A
0N/A /**
0N/A * Creates a string representation of this <tt>Date</tt> object in an
0N/A * implementation-dependent form. The intent is that the form should
0N/A * be familiar to the user of the Java application, wherever it may
0N/A * happen to be running. The intent is comparable to that of the
0N/A * "<code>%c</code>" format supported by the <code>strftime()</code>
0N/A * function of ISO&nbsp;C.
0N/A *
0N/A * @return a string representation of this date, using the locale
0N/A * conventions.
0N/A * @see java.text.DateFormat
0N/A * @see java.util.Date#toString()
0N/A * @see java.util.Date#toGMTString()
0N/A * @deprecated As of JDK version 1.1,
0N/A * replaced by <code>DateFormat.format(Date date)</code>.
0N/A */
0N/A @Deprecated
0N/A public String toLocaleString() {
0N/A DateFormat formatter = DateFormat.getDateTimeInstance();
0N/A return formatter.format(this);
0N/A }
0N/A
989N/A /**
1119N/A * Creates a string representation of this <tt>Date</tt> object of
1119N/A * the form:
1119N/A * <blockquote<pre>
1119N/A * d mon yyyy hh:mm:ss GMT</pre></blockquote>
1119N/A * where:<ul>
1119N/A * <li><i>d</i> is the day of the month (<tt>1</tt> through <tt>31</tt>),
1119N/A * as one or two decimal digits.
1119N/A * <li><i>mon</i> is the month (<tt>Jan, Feb, Mar, Apr, May, Jun, Jul,
1119N/A * Aug, Sep, Oct, Nov, Dec</tt>).
1119N/A * <li><i>yyyy</i> is the year, as four decimal digits.
1119N/A * <li><i>hh</i> is the hour of the day (<tt>00</tt> through <tt>23</tt>),
1119N/A * as two decimal digits.
1119N/A * <li><i>mm</i> is the minute within the hour (<tt>00</tt> through
0N/A * <tt>59</tt>), as two decimal digits.
0N/A * <li><i>ss</i> is the second within the minute (<tt>00</tt> through
0N/A * <tt>61</tt>), as two decimal digits.
0N/A * <li><i>GMT</i> is exactly the ASCII letters "<tt>GMT</tt>" to indicate
0N/A * Greenwich Mean Time.
989N/A * </ul><p>
0N/A * The result does not depend on the local time zone.
989N/A *
0N/A * @return a string representation of this date, using the Internet GMT
0N/A * conventions.
0N/A * @see java.text.DateFormat
0N/A * @see java.util.Date#toString()
0N/A * @see java.util.Date#toLocaleString()
0N/A * @deprecated As of JDK version 1.1,
0N/A * replaced by <code>DateFormat.format(Date date)</code>, using a
0N/A * GMT <code>TimeZone</code>.
0N/A */
0N/A @Deprecated
0N/A public String toGMTString() {
0N/A // d MMM yyyy HH:mm:ss 'GMT'
0N/A long t = getTime();
0N/A BaseCalendar cal = getCalendarSystem(t);
0N/A BaseCalendar.Date date =
0N/A (BaseCalendar.Date) cal.getCalendarDate(getTime(), (TimeZone)null);
0N/A StringBuilder sb = new StringBuilder(32);
0N/A CalendarUtils.sprintf0d(sb, date.getDayOfMonth(), 1).append(' '); // d
0N/A convertToAbbr(sb, wtb[date.getMonth() - 1 + 2 + 7]).append(' '); // MMM
0N/A sb.append(date.getYear()).append(' '); // yyyy
0N/A CalendarUtils.sprintf0d(sb, date.getHours(), 2).append(':'); // HH
0N/A CalendarUtils.sprintf0d(sb, date.getMinutes(), 2).append(':'); // mm
0N/A CalendarUtils.sprintf0d(sb, date.getSeconds(), 2); // ss
0N/A sb.append(" GMT"); // ' GMT'
0N/A return sb.toString();
0N/A }
0N/A
0N/A /**
0N/A * Returns the offset, measured in minutes, for the local time zone
0N/A * relative to UTC that is appropriate for the time represented by
0N/A * this <code>Date</code> object.
0N/A * <p>
0N/A * For example, in Massachusetts, five time zones west of Greenwich:
0N/A * <blockquote><pre>
0N/A * new Date(96, 1, 14).getTimezoneOffset() returns 300</pre></blockquote>
0N/A * because on February 14, 1996, standard time (Eastern Standard Time)
0N/A * is in use, which is offset five hours from UTC; but:
0N/A * <blockquote><pre>
0N/A * new Date(96, 5, 1).getTimezoneOffset() returns 240</pre></blockquote>
0N/A * because on June 1, 1996, daylight saving time (Eastern Daylight Time)
0N/A * is in use, which is offset only four hours from UTC.<p>
0N/A * This method produces the same result as if it computed:
0N/A * <blockquote><pre>
0N/A * (this.getTime() - UTC(this.getYear(),
0N/A * this.getMonth(),
0N/A * this.getDate(),
0N/A * this.getHours(),
0N/A * this.getMinutes(),
0N/A * this.getSeconds())) / (60 * 1000)
0N/A * </pre></blockquote>
0N/A *
0N/A * @return the time-zone offset, in minutes, for the current time zone.
0N/A * @see java.util.Calendar#ZONE_OFFSET
0N/A * @see java.util.Calendar#DST_OFFSET
0N/A * @see java.util.TimeZone#getDefault
0N/A * @deprecated As of JDK version 1.1,
0N/A * replaced by <code>-(Calendar.get(Calendar.ZONE_OFFSET) +
0N/A * Calendar.get(Calendar.DST_OFFSET)) / (60 * 1000)</code>.
0N/A */
0N/A @Deprecated
0N/A public int getTimezoneOffset() {
0N/A int zoneOffset;
0N/A if (cdate == null) {
0N/A TimeZone tz = TimeZone.getDefaultRef();
0N/A if (tz instanceof ZoneInfo) {
0N/A zoneOffset = ((ZoneInfo)tz).getOffsets(fastTime, null);
0N/A } else {
0N/A zoneOffset = tz.getOffset(fastTime);
0N/A }
0N/A } else {
0N/A normalize();
0N/A zoneOffset = cdate.getZoneOffset();
0N/A }
0N/A return -zoneOffset/60000; // convert to minutes
0N/A }
0N/A
0N/A private final BaseCalendar.Date getCalendarDate() {
0N/A if (cdate == null) {
0N/A BaseCalendar cal = getCalendarSystem(fastTime);
0N/A cdate = (BaseCalendar.Date) cal.getCalendarDate(fastTime,
0N/A TimeZone.getDefaultRef());
0N/A }
0N/A return cdate;
0N/A }
0N/A
0N/A private final BaseCalendar.Date normalize() {
1426N/A if (cdate == null) {
1426N/A BaseCalendar cal = getCalendarSystem(fastTime);
1426N/A cdate = (BaseCalendar.Date) cal.getCalendarDate(fastTime,
1426N/A TimeZone.getDefaultRef());
1426N/A return cdate;
1426N/A }
1426N/A
0N/A // Normalize cdate with the TimeZone in cdate first. This is
1426N/A // required for the compatible behavior.
1426N/A if (!cdate.isNormalized()) {
1426N/A cdate = normalize(cdate);
1426N/A }
0N/A
0N/A // If the default TimeZone has changed, then recalculate the
0N/A // fields with the new TimeZone.
0N/A TimeZone tz = TimeZone.getDefaultRef();
0N/A if (tz != cdate.getZone()) {
0N/A cdate.setZone(tz);
0N/A CalendarSystem cal = getCalendarSystem(cdate);
0N/A cal.getCalendarDate(fastTime, cdate);
113N/A }
0N/A return cdate;
0N/A }
0N/A
0N/A // fastTime and the returned data are in sync upon return.
0N/A private final BaseCalendar.Date normalize(BaseCalendar.Date date) {
0N/A int y = date.getNormalizedYear();
0N/A int m = date.getMonth();
0N/A int d = date.getDayOfMonth();
0N/A int hh = date.getHours();
0N/A int mm = date.getMinutes();
0N/A int ss = date.getSeconds();
0N/A int ms = date.getMillis();
0N/A TimeZone tz = date.getZone();
0N/A
0N/A // If the specified year can't be handled using a long value
0N/A // in milliseconds, GregorianCalendar is used for full
0N/A // compatibility with underflow and overflow. This is required
0N/A // by some JCK tests. The limits are based max year values -
0N/A // years that can be represented by max values of d, hh, mm,
0N/A // ss and ms. Also, let GregorianCalendar handle the default
0N/A // cutover year so that we don't need to worry about the
0N/A // transition here.
0N/A if (y == 1582 || y > 280000000 || y < -280000000) {
0N/A if (tz == null) {
0N/A tz = TimeZone.getTimeZone("GMT");
0N/A }
0N/A GregorianCalendar gc = new GregorianCalendar(tz);
989N/A gc.clear();
0N/A gc.set(gc.MILLISECOND, ms);
0N/A gc.set(y, m-1, d, hh, mm, ss);
0N/A fastTime = gc.getTimeInMillis();
0N/A BaseCalendar cal = getCalendarSystem(fastTime);
0N/A date = (BaseCalendar.Date) cal.getCalendarDate(fastTime, tz);
0N/A return date;
0N/A }
0N/A
0N/A BaseCalendar cal = getCalendarSystem(y);
0N/A if (cal != getCalendarSystem(date)) {
0N/A date = (BaseCalendar.Date) cal.newCalendarDate(tz);
0N/A date.setNormalizedDate(y, m, d).setTimeOfDay(hh, mm, ss, ms);
0N/A }
0N/A // Perform the GregorianCalendar-style normalization.
0N/A fastTime = cal.getTime(date);
0N/A
0N/A // In case the normalized date requires the other calendar
0N/A // system, we need to recalculate it using the other one.
0N/A BaseCalendar ncal = getCalendarSystem(fastTime);
0N/A if (ncal != cal) {
0N/A date = (BaseCalendar.Date) ncal.newCalendarDate(tz);
0N/A date.setNormalizedDate(y, m, d).setTimeOfDay(hh, mm, ss, ms);
0N/A fastTime = ncal.getTime(date);
0N/A }
0N/A return date;
0N/A }
0N/A
0N/A /**
0N/A * Returns the Gregorian or Julian calendar system to use with the
0N/A * given date. Use Gregorian from October 15, 1582.
0N/A *
0N/A * @param year normalized calendar year (not -1900)
0N/A * @return the CalendarSystem to use for the specified date
0N/A */
0N/A private static final BaseCalendar getCalendarSystem(int year) {
0N/A if (year >= 1582) {
0N/A return gcal;
0N/A }
0N/A return getJulianCalendar();
0N/A }
0N/A
0N/A private static final BaseCalendar getCalendarSystem(long utc) {
0N/A // Quickly check if the time stamp given by `utc' is the Epoch
0N/A // or later. If it's before 1970, we convert the cutover to
0N/A // local time to compare.
if (utc >= 0
|| utc >= GregorianCalendar.DEFAULT_GREGORIAN_CUTOVER
- TimeZone.getDefaultRef().getOffset(utc)) {
return gcal;
}
return getJulianCalendar();
}
private static final BaseCalendar getCalendarSystem(BaseCalendar.Date cdate) {
if (jcal == null) {
return gcal;
}
if (cdate.getEra() != null) {
return jcal;
}
return gcal;
}
synchronized private static final BaseCalendar getJulianCalendar() {
if (jcal == null) {
jcal = (BaseCalendar) CalendarSystem.forName("julian");
}
return jcal;
}
/**
* Save the state of this object to a stream (i.e., serialize it).
*
* @serialData The value returned by <code>getTime()</code>
* is emitted (long). This represents the offset from
* January 1, 1970, 00:00:00 GMT in milliseconds.
*/
private void writeObject(ObjectOutputStream s)
throws IOException
{
s.writeLong(getTimeImpl());
}
/**
* Reconstitute this object from a stream (i.e., deserialize it).
*/
private void readObject(ObjectInputStream s)
throws IOException, ClassNotFoundException
{
fastTime = s.readLong();
}
}