Source Home >> Java Source 1.6.0 >> java.util.Date V 0.09
  • 0001/*
  • 0002 * @(#)Date.java 1.84 06/11/23
  • 0003 *
  • 0004 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
  • 0005 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  • 0006 */
  • 0007
  • 0008package java.util;
  • 0009
  • 0010import java.text.DateFormat;
  • 0011import java.io.IOException;
  • 0012import java.io.ObjectOutputStream;
  • 0013import java.io.ObjectInputStream;
  • 0014import java.lang.ref.SoftReference;
  • 0015import sun.util.calendar.BaseCalendar;
  • 0016import sun.util.calendar.CalendarDate;
  • 0017import sun.util.calendar.CalendarSystem;
  • 0018import sun.util.calendar.CalendarUtils;
  • 0019import sun.util.calendar.Era;
  • 0020import sun.util.calendar.Gregorian;
  • 0021import sun.util.calendar.ZoneInfo;
  • 0022
  • 0023/**
  • 0024 * The class <code>Date</code> represents a specific instant
  • 0025 * in time, with millisecond precision.
  • 0026 * <p>
  • 0027 * Prior to JDK 1.1, the class <code>Date</code> had two additional
  • 0028 * functions. It allowed the interpretation of dates as year, month, day, hour,
  • 0029 * minute, and second values. It also allowed the formatting and parsing
  • 0030 * of date strings. Unfortunately, the API for these functions was not
  • 0031 * amenable to internationalization. As of JDK 1.1, the
  • 0032 * <code>Calendar</code> class should be used to convert between dates and time
  • 0033 * fields and the <code>DateFormat</code> class should be used to format and
  • 0034 * parse date strings.
  • 0035 * The corresponding methods in <code>Date</code> are deprecated.
  • 0036 * <p>
  • 0037 * Although the <code>Date</code> class is intended to reflect
  • 0038 * coordinated universal time (UTC), it may not do so exactly,
  • 0039 * depending on the host environment of the Java Virtual Machine.
  • 0040 * Nearly all modern operating systems assume that 1 day =
  • 0041 * 24 × 60 × 60 = 86400 seconds
  • 0042 * in all cases. In UTC, however, about once every year or two there
  • 0043 * is an extra second, called a "leap second." The leap
  • 0044 * second is always added as the last second of the day, and always
  • 0045 * on December 31 or June 30. For example, the last minute of the
  • 0046 * year 1995 was 61 seconds long, thanks to an added leap second.
  • 0047 * Most computer clocks are not accurate enough to be able to reflect
  • 0048 * the leap-second distinction.
  • 0049 * <p>
  • 0050 * Some computer standards are defined in terms of Greenwich mean
  • 0051 * time (GMT), which is equivalent to universal time (UT). GMT is
  • 0052 * the "civil" name for the standard; UT is the
  • 0053 * "scientific" name for the same standard. The
  • 0054 * distinction between UTC and UT is that UTC is based on an atomic
  • 0055 * clock and UT is based on astronomical observations, which for all
  • 0056 * practical purposes is an invisibly fine hair to split. Because the
  • 0057 * earth's rotation is not uniform (it slows down and speeds up
  • 0058 * in complicated ways), UT does not always flow uniformly. Leap
  • 0059 * seconds are introduced as needed into UTC so as to keep UTC within
  • 0060 * 0.9 seconds of UT1, which is a version of UT with certain
  • 0061 * corrections applied. There are other time and date systems as
  • 0062 * well; for example, the time scale used by the satellite-based
  • 0063 * global positioning system (GPS) is synchronized to UTC but is
  • 0064 * <i>not</i> adjusted for leap seconds. An interesting source of
  • 0065 * further information is the U.S. Naval Observatory, particularly
  • 0066 * the Directorate of Time at:
  • 0067 * <blockquote><pre>
  • 0068 * <a href=http://tycho.usno.navy.mil>http://tycho.usno.navy.mil</a>
  • 0069 * </pre></blockquote>
  • 0070 * <p>
  • 0071 * and their definitions of "Systems of Time" at:
  • 0072 * <blockquote><pre>
  • 0073 * <a href=http://tycho.usno.navy.mil/systime.html>http://tycho.usno.navy.mil/systime.html</a>
  • 0074 * </pre></blockquote>
  • 0075 * <p>
  • 0076 * In all methods of class <code>Date</code> that accept or return
  • 0077 * year, month, date, hours, minutes, and seconds values, the
  • 0078 * following representations are used:
  • 0079 * <ul>
  • 0080 * <li>A year <i>y</i> is represented by the integer
  • 0081 * <i>y</i> <code>- 1900</code>.
  • 0082 * <li>A month is represented by an integer from 0 to 11; 0 is January,
  • 0083 * 1 is February, and so forth; thus 11 is December.
  • 0084 * <li>A date (day of month) is represented by an integer from 1 to 31
  • 0085 * in the usual manner.
  • 0086 * <li>An hour is represented by an integer from 0 to 23. Thus, the hour
  • 0087 * from midnight to 1 a.m. is hour 0, and the hour from noon to 1
  • 0088 * p.m. is hour 12.
  • 0089 * <li>A minute is represented by an integer from 0 to 59 in the usual manner.
  • 0090 * <li>A second is represented by an integer from 0 to 61; the values 60 and
  • 0091 * 61 occur only for leap seconds and even then only in Java
  • 0092 * implementations that actually track leap seconds correctly. Because
  • 0093 * of the manner in which leap seconds are currently introduced, it is
  • 0094 * extremely unlikely that two leap seconds will occur in the same
  • 0095 * minute, but this specification follows the date and time conventions
  • 0096 * for ISO C.
  • 0097 * </ul>
  • 0098 * <p>
  • 0099 * In all cases, arguments given to methods for these purposes need
  • 0100 * not fall within the indicated ranges; for example, a date may be
  • 0101 * specified as January 32 and is interpreted as meaning February 1.
  • 0102 *
  • 0103 * @author James Gosling
  • 0104 * @author Arthur van Hoff
  • 0105 * @author Alan Liu
  • 0106 * @version 1.84, 11/23/06
  • 0107 * @see java.text.DateFormat
  • 0108 * @see java.util.Calendar
  • 0109 * @see java.util.TimeZone
  • 0110 * @since JDK1.0
  • 0111 */
  • 0112public class Date
  • 0113 implements java.io.Serializable, Cloneable, Comparable<Date>
  • 0114{
  • 0115 private static final BaseCalendar gcal =
  • 0116 CalendarSystem.getGregorianCalendar();
  • 0117 private static BaseCalendar jcal;
  • 0118
  • 0119 private transient long fastTime;
  • 0120
  • 0121 /*
  • 0122 * If cdate is null, then fastTime indicates the time in millis.
  • 0123 * If cdate.isNormalized() is true, then fastTime and cdate are in
  • 0124 * synch. Otherwise, fastTime is ignored, and cdate indicates the
  • 0125 * time.
  • 0126 */
  • 0127 private transient BaseCalendar.Date cdate;
  • 0128
  • 0129 // Initialized just before the value is used. See parse().
  • 0130 private static int defaultCenturyStart;
  • 0131
  • 0132 /* use serialVersionUID from modified java.util.Date for
  • 0133 * interoperability with JDK1.1. The Date was modified to write
  • 0134 * and read only the UTC time.
  • 0135 */
  • 0136 private static final long serialVersionUID = 7523967970034938905L;
  • 0137
  • 0138 /**
  • 0139 * Allocates a <code>Date</code> object and initializes it so that
  • 0140 * it represents the time at which it was allocated, measured to the
  • 0141 * nearest millisecond.
  • 0142 *
  • 0143 * @see java.lang.System#currentTimeMillis()
  • 0144 */
  • 0145 public Date() {
  • 0146 this(System.currentTimeMillis());
  • 0147 }
  • 0148
  • 0149 /**
  • 0150 * Allocates a <code>Date</code> object and initializes it to
  • 0151 * represent the specified number of milliseconds since the
  • 0152 * standard base time known as "the epoch", namely January 1,
  • 0153 * 1970, 00:00:00 GMT.
  • 0154 *
  • 0155 * @param date the milliseconds since January 1, 1970, 00:00:00 GMT.
  • 0156 * @see java.lang.System#currentTimeMillis()
  • 0157 */
  • 0158 public Date(long date) {
  • 0159 fastTime = date;
  • 0160 }
  • 0161
  • 0162 /**
  • 0163 * Allocates a <code>Date</code> object and initializes it so that
  • 0164 * it represents midnight, local time, at the beginning of the day
  • 0165 * specified by the <code>year</code>, <code>month</code>, and
  • 0166 * <code>date</code> arguments.
  • 0167 *
  • 0168 * @param year the year minus 1900.
  • 0169 * @param month the month between 0-11.
  • 0170 * @param date the day of the month between 1-31.
  • 0171 * @see java.util.Calendar
  • 0172 * @deprecated As of JDK version 1.1,
  • 0173 * replaced by <code>Calendar.set(year + 1900, month, date)</code>
  • 0174 * or <code>GregorianCalendar(year + 1900, month, date)</code>.
  • 0175 */
  • 0176 @Deprecated
  • 0177 public Date(int year, int month, int date) {
  • 0178 this(year, month, date, 0, 0, 0);
  • 0179 }
  • 0180
  • 0181 /**
  • 0182 * Allocates a <code>Date</code> object and initializes it so that
  • 0183 * it represents the instant at the start of the minute specified by
  • 0184 * the <code>year</code>, <code>month</code>, <code>date</code>,
  • 0185 * <code>hrs</code>, and <code>min</code> arguments, in the local
  • 0186 * time zone.
  • 0187 *
  • 0188 * @param year the year minus 1900.
  • 0189 * @param month the month between 0-11.
  • 0190 * @param date the day of the month between 1-31.
  • 0191 * @param hrs the hours between 0-23.
  • 0192 * @param min the minutes between 0-59.
  • 0193 * @see java.util.Calendar
  • 0194 * @deprecated As of JDK version 1.1,
  • 0195 * replaced by <code>Calendar.set(year + 1900, month, date,
  • 0196 * hrs, min)</code> or <code>GregorianCalendar(year + 1900,
  • 0197 * month, date, hrs, min)</code>.
  • 0198 */
  • 0199 @Deprecated
  • 0200 public Date(int year, int month, int date, int hrs, int min) {
  • 0201 this(year, month, date, hrs, min, 0);
  • 0202 }
  • 0203
  • 0204 /**
  • 0205 * Allocates a <code>Date</code> object and initializes it so that
  • 0206 * it represents the instant at the start of the second specified
  • 0207 * by the <code>year</code>, <code>month</code>, <code>date</code>,
  • 0208 * <code>hrs</code>, <code>min</code>, and <code>sec</code> arguments,
  • 0209 * in the local time zone.
  • 0210 *
  • 0211 * @param year the year minus 1900.
  • 0212 * @param month the month between 0-11.
  • 0213 * @param date the day of the month between 1-31.
  • 0214 * @param hrs the hours between 0-23.
  • 0215 * @param min the minutes between 0-59.
  • 0216 * @param sec the seconds between 0-59.
  • 0217 * @see java.util.Calendar
  • 0218 * @deprecated As of JDK version 1.1,
  • 0219 * replaced by <code>Calendar.set(year + 1900, month, date,
  • 0220 * hrs, min, sec)</code> or <code>GregorianCalendar(year + 1900,
  • 0221 * month, date, hrs, min, sec)</code>.
  • 0222 */
  • 0223 @Deprecated
  • 0224 public Date(int year, int month, int date, int hrs, int min, int sec) {
  • 0225 int y = year + 1900;
  • 0226 // month is 0-based. So we have to normalize month to support Long.MAX_VALUE.
  • 0227 if (month >= 12) {
  • 0228 y += month / 12;
  • 0229 month %= 12;
  • 0230 } else if (month < 0) {
  • 0231 y += CalendarUtils.floorDivide(month, 12);
  • 0232 month = CalendarUtils.mod(month, 12);
  • 0233 }
  • 0234 BaseCalendar cal = getCalendarSystem(y);
  • 0235 cdate = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.getDefaultRef());
  • 0236 cdate.setNormalizedDate(y, month + 1, date).setTimeOfDay(hrs, min, sec, 0);
  • 0237 getTimeImpl();
  • 0238 cdate = null;
  • 0239 }
  • 0240
  • 0241 /**
  • 0242 * Allocates a <code>Date</code> object and initializes it so that
  • 0243 * it represents the date and time indicated by the string
  • 0244 * <code>s</code>, which is interpreted as if by the
  • 0245 * {@link Date#parse} method.
  • 0246 *
  • 0247 * @param s a string representation of the date.
  • 0248 * @see java.text.DateFormat
  • 0249 * @see java.util.Date#parse(java.lang.String)
  • 0250 * @deprecated As of JDK version 1.1,
  • 0251 * replaced by <code>DateFormat.parse(String s)</code>.
  • 0252 */
  • 0253 @Deprecated
  • 0254 public Date(String s) {
  • 0255 this(parse(s));
  • 0256 }
  • 0257
  • 0258 /**
  • 0259 * Return a copy of this object.
  • 0260 */
  • 0261 public Object clone() {
  • 0262 Date d = null;
  • 0263 try {
  • 0264 d = (Date)super.clone();
  • 0265 if (cdate != null) {
  • 0266 d.cdate = (BaseCalendar.Date) cdate.clone();
  • 0267 }
  • 0268 } catch (CloneNotSupportedException e) {} // Won't happen
  • 0269 return d;
  • 0270 }
  • 0271
  • 0272 /**
  • 0273 * Determines the date and time based on the arguments. The
  • 0274 * arguments are interpreted as a year, month, day of the month,
  • 0275 * hour of the day, minute within the hour, and second within the
  • 0276 * minute, exactly as for the <tt>Date</tt> constructor with six
  • 0277 * arguments, except that the arguments are interpreted relative
  • 0278 * to UTC rather than to the local time zone. The time indicated is
  • 0279 * returned represented as the distance, measured in milliseconds,
  • 0280 * of that time from the epoch (00:00:00 GMT on January 1, 1970).
  • 0281 *
  • 0282 * @param year the year minus 1900.
  • 0283 * @param month the month between 0-11.
  • 0284 * @param date the day of the month between 1-31.
  • 0285 * @param hrs the hours between 0-23.
  • 0286 * @param min the minutes between 0-59.
  • 0287 * @param sec the seconds between 0-59.
  • 0288 * @return the number of milliseconds since January 1, 1970, 00:00:00 GMT for
  • 0289 * the date and time specified by the arguments.
  • 0290 * @see java.util.Calendar
  • 0291 * @deprecated As of JDK version 1.1,
  • 0292 * replaced by <code>Calendar.set(year + 1900, month, date,
  • 0293 * hrs, min, sec)</code> or <code>GregorianCalendar(year + 1900,
  • 0294 * month, date, hrs, min, sec)</code>, using a UTC
  • 0295 * <code>TimeZone</code>, followed by <code>Calendar.getTime().getTime()</code>.
  • 0296 */
  • 0297 @Deprecated
  • 0298 public static long UTC(int year, int month, int date,
  • 0299 int hrs, int min, int sec) {
  • 0300 int y = year + 1900;
  • 0301 // month is 0-based. So we have to normalize month to support Long.MAX_VALUE.
  • 0302 if (month >= 12) {
  • 0303 y += month / 12;
  • 0304 month %= 12;
  • 0305 } else if (month < 0) {
  • 0306 y += CalendarUtils.floorDivide(month, 12);
  • 0307 month = CalendarUtils.mod(month, 12);
  • 0308 }
  • 0309 int m = month + 1;
  • 0310 BaseCalendar cal = getCalendarSystem(y);
  • 0311 BaseCalendar.Date udate = (BaseCalendar.Date) cal.newCalendarDate(null);
  • 0312 udate.setNormalizedDate(y, m, date).setTimeOfDay(hrs, min, sec, 0);
  • 0313
  • 0314 // Use a Date instance to perform normalization. Its fastTime
  • 0315 // is the UTC value after the normalization.
  • 0316 Date d = new Date(0);
  • 0317 d.normalize(udate);
  • 0318 return d.fastTime;
  • 0319 }
  • 0320
  • 0321 /**
  • 0322 * Attempts to interpret the string <tt>s</tt> as a representation
  • 0323 * of a date and time. If the attempt is successful, the time
  • 0324 * indicated is returned represented as the distance, measured in
  • 0325 * milliseconds, of that time from the epoch (00:00:00 GMT on
  • 0326 * January 1, 1970). If the attempt fails, an
  • 0327 * <tt>IllegalArgumentException</tt> is thrown.
  • 0328 * <p>
  • 0329 * It accepts many syntaxes; in particular, it recognizes the IETF
  • 0330 * standard date syntax: "Sat, 12 Aug 1995 13:30:00 GMT". It also
  • 0331 * understands the continental U.S. time-zone abbreviations, but for
  • 0332 * general use, a time-zone offset should be used: "Sat, 12 Aug 1995
  • 0333 * 13:30:00 GMT+0430" (4 hours, 30 minutes west of the Greenwich
  • 0334 * meridian). If no time zone is specified, the local time zone is
  • 0335 * assumed. GMT and UTC are considered equivalent.
  • 0336 * <p>
  • 0337 * The string <tt>s</tt> is processed from left to right, looking for
  • 0338 * data of interest. Any material in <tt>s</tt> that is within the
  • 0339 * ASCII parenthesis characters <tt>(</tt> and <tt>)</tt> is ignored.
  • 0340 * Parentheses may be nested. Otherwise, the only characters permitted
  • 0341 * within <tt>s</tt> are these ASCII characters:
  • 0342 * <blockquote><pre>
  • 0343 * abcdefghijklmnopqrstuvwxyz
  • 0344 * ABCDEFGHIJKLMNOPQRSTUVWXYZ
  • 0345 * 0123456789,+-:/</pre></blockquote>
  • 0346 * and whitespace characters.<p>
  • 0347 * A consecutive sequence of decimal digits is treated as a decimal
  • 0348 * number:<ul>
  • 0349 * <li>If a number is preceded by <tt>+</tt> or <tt>-</tt> and a year
  • 0350 * has already been recognized, then the number is a time-zone
  • 0351 * offset. If the number is less than 24, it is an offset measured
  • 0352 * in hours. Otherwise, it is regarded as an offset in minutes,
  • 0353 * expressed in 24-hour time format without punctuation. A
  • 0354 * preceding <tt>-</tt> means a westward offset. Time zone offsets
  • 0355 * are always relative to UTC (Greenwich). Thus, for example,
  • 0356 * <tt>-5</tt> occurring in the string would mean "five hours west
  • 0357 * of Greenwich" and <tt>+0430</tt> would mean "four hours and
  • 0358 * thirty minutes east of Greenwich." It is permitted for the
  • 0359 * string to specify <tt>GMT</tt>, <tt>UT</tt>, or <tt>UTC</tt>
  • 0360 * redundantly-for example, <tt>GMT-5</tt> or <tt>utc+0430</tt>.
  • 0361 * <li>The number is regarded as a year number if one of the
  • 0362 * following conditions is true:
  • 0363 * <ul>
  • 0364 * <li>The number is equal to or greater than 70 and followed by a
  • 0365 * space, comma, slash, or end of string
  • 0366 * <li>The number is less than 70, and both a month and a day of
  • 0367 * the month have already been recognized</li>
  • 0368 * </ul>
  • 0369 * If the recognized year number is less than 100, it is
  • 0370 * interpreted as an abbreviated year relative to a century of
  • 0371 * which dates are within 80 years before and 19 years after
  • 0372 * the time when the Date class is initialized.
  • 0373 * After adjusting the year number, 1900 is subtracted from
  • 0374 * it. For example, if the current year is 1999 then years in
  • 0375 * the range 19 to 99 are assumed to mean 1919 to 1999, while
  • 0376 * years from 0 to 18 are assumed to mean 2000 to 2018. Note
  • 0377 * that this is slightly different from the interpretation of
  • 0378 * years less than 100 that is used in {@link java.text.SimpleDateFormat}.
  • 0379 * <li>If the number is followed by a colon, it is regarded as an hour,
  • 0380 * unless an hour has already been recognized, in which case it is
  • 0381 * regarded as a minute.
  • 0382 * <li>If the number is followed by a slash, it is regarded as a month
  • 0383 * (it is decreased by 1 to produce a number in the range <tt>0</tt>
  • 0384 * to <tt>11</tt>), unless a month has already been recognized, in
  • 0385 * which case it is regarded as a day of the month.
  • 0386 * <li>If the number is followed by whitespace, a comma, a hyphen, or
  • 0387 * end of string, then if an hour has been recognized but not a
  • 0388 * minute, it is regarded as a minute; otherwise, if a minute has
  • 0389 * been recognized but not a second, it is regarded as a second;
  • 0390 * otherwise, it is regarded as a day of the month. </ul><p>
  • 0391 * A consecutive sequence of letters is regarded as a word and treated
  • 0392 * as follows:<ul>
  • 0393 * <li>A word that matches <tt>AM</tt>, ignoring case, is ignored (but
  • 0394 * the parse fails if an hour has not been recognized or is less
  • 0395 * than <tt>1</tt> or greater than <tt>12</tt>).
  • 0396 * <li>A word that matches <tt>PM</tt>, ignoring case, adds <tt>12</tt>
  • 0397 * to the hour (but the parse fails if an hour has not been
  • 0398 * recognized or is less than <tt>1</tt> or greater than <tt>12</tt>).
  • 0399 * <li>Any word that matches any prefix of <tt>SUNDAY, MONDAY, TUESDAY,
  • 0400 * WEDNESDAY, THURSDAY, FRIDAY</tt>, or <tt>SATURDAY</tt>, ignoring
  • 0401 * case, is ignored. For example, <tt>sat, Friday, TUE</tt>, and
  • 0402 * <tt>Thurs</tt> are ignored.
  • 0403 * <li>Otherwise, any word that matches any prefix of <tt>JANUARY,
  • 0404 * FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER,
  • 0405 * OCTOBER, NOVEMBER</tt>, or <tt>DECEMBER</tt>, ignoring case, and
  • 0406 * considering them in the order given here, is recognized as
  • 0407 * specifying a month and is converted to a number (<tt>0</tt> to
  • 0408 * <tt>11</tt>). For example, <tt>aug, Sept, april</tt>, and
  • 0409 * <tt>NOV</tt> are recognized as months. So is <tt>Ma</tt>, which
  • 0410 * is recognized as <tt>MARCH</tt>, not <tt>MAY</tt>.
  • 0411 * <li>Any word that matches <tt>GMT, UT</tt>, or <tt>UTC</tt>, ignoring
  • 0412 * case, is treated as referring to UTC.
  • 0413 * <li>Any word that matches <tt>EST, CST, MST</tt>, or <tt>PST</tt>,
  • 0414 * ignoring case, is recognized as referring to the time zone in
  • 0415 * North America that is five, six, seven, or eight hours west of
  • 0416 * Greenwich, respectively. Any word that matches <tt>EDT, CDT,
  • 0417 * MDT</tt>, or <tt>PDT</tt>, ignoring case, is recognized as
  • 0418 * referring to the same time zone, respectively, during daylight
  • 0419 * saving time.</ul><p>
  • 0420 * Once the entire string s has been scanned, it is converted to a time
  • 0421 * result in one of two ways. If a time zone or time-zone offset has been
  • 0422 * recognized, then the year, month, day of month, hour, minute, and
  • 0423 * second are interpreted in UTC and then the time-zone offset is
  • 0424 * applied. Otherwise, the year, month, day of month, hour, minute, and
  • 0425 * second are interpreted in the local time zone.
  • 0426 *
  • 0427 * @param s a string to be parsed as a date.
  • 0428 * @return the number of milliseconds since January 1, 1970, 00:00:00 GMT
  • 0429 * represented by the string argument.
  • 0430 * @see java.text.DateFormat
  • 0431 * @deprecated As of JDK version 1.1,
  • 0432 * replaced by <code>DateFormat.parse(String s)</code>.
  • 0433 */
  • 0434 @Deprecated
  • 0435 public static long parse(String s) {
  • 0436 int year = Integer.MIN_VALUE;
  • 0437 int mon = -1;
  • 0438 int mday = -1;
  • 0439 int hour = -1;
  • 0440 int min = -1;
  • 0441 int sec = -1;
  • 0442 int millis = -1;
  • 0443 int c = -1;
  • 0444 int i = 0;
  • 0445 int n = -1;
  • 0446 int wst = -1;
  • 0447 int tzoffset = -1;
  • 0448 int prevc = 0;
  • 0449 syntax:
  • 0450 {
  • 0451 if (s == null)
  • 0452 break syntax;
  • 0453 int limit = s.length();
  • 0454 while (i < limit) {
  • 0455 c = s.charAt(i);
  • 0456 i++;
  • 0457 if (c <= ' ' || c == ',')
  • 0458 continue;
  • 0459 if (c == '(') { // skip comments
  • 0460 int depth = 1;
  • 0461 while (i < limit) {
  • 0462 c = s.charAt(i);
  • 0463 i++;
  • 0464 if (c == '(') depth++;
  • 0465 else if (c == ')')
  • 0466 if (--depth <= 0)
  • 0467 break;
  • 0468 }
  • 0469 continue;
  • 0470 }
  • 0471 if ('0' <= c && c <= '9') {
  • 0472 n = c - '0';
  • 0473 while (i < limit && '0' <= (c = s.charAt(i)) && c <= '9') {
  • 0474 n = n * 10 + c - '0';
  • 0475 i++;
  • 0476 }
  • 0477 if (prevc == '+' || prevc == '-' && year != Integer.MIN_VALUE) {
  • 0478 // timezone offset
  • 0479 if (n < 24)
  • 0480 n = n * 60; // EG. "GMT-3"
  • 0481 else
  • 0482 n = n % 100 + n / 100 * 60; // eg "GMT-0430"
  • 0483 if (prevc == '+') // plus means east of GMT
  • 0484 n = -n;
  • 0485 if (tzoffset != 0 && tzoffset != -1)
  • 0486 break syntax;
  • 0487 tzoffset = n;
  • 0488 } else if (n >= 70)
  • 0489 if (year != Integer.MIN_VALUE)
  • 0490 break syntax;
  • 0491 else if (c <= ' ' || c == ',' || c == '/' || i >= limit)
  • 0492 // year = n < 1900 ? n : n - 1900;
  • 0493 year = n;
  • 0494 else
  • 0495 break syntax;
  • 0496 else if (c == ':')
  • 0497 if (hour < 0)
  • 0498 hour = (byte) n;
  • 0499 else if (min < 0)
  • 0500 min = (byte) n;
  • 0501 else
  • 0502 break syntax;
  • 0503 else if (c == '/')
  • 0504 if (mon < 0)
  • 0505 mon = (byte) (n - 1);
  • 0506 else if (mday < 0)
  • 0507 mday = (byte) n;
  • 0508 else
  • 0509 break syntax;
  • 0510 else if (i < limit && c != ',' && c > ' ' && c != '-')
  • 0511 break syntax;
  • 0512 else if (hour >= 0 && min < 0)
  • 0513 min = (byte) n;
  • 0514 else if (min >= 0 && sec < 0)
  • 0515 sec = (byte) n;
  • 0516 else if (mday < 0)
  • 0517 mday = (byte) n;
  • 0518 // Handle two-digit years < 70 (70-99 handled above).
  • 0519 else if (year == Integer.MIN_VALUE && mon >= 0 && mday >= 0)
  • 0520 year = n;
  • 0521 else
  • 0522 break syntax;
  • 0523 prevc = 0;
  • 0524 } else if (c == '/' || c == ':' || c == '+' || c == '-')
  • 0525 prevc = c;
  • 0526 else {
  • 0527 int st = i - 1;
  • 0528 while (i < limit) {
  • 0529 c = s.charAt(i);
  • 0530 if (!('A' <= c && c <= 'Z' || 'a' <= c && c <= 'z'))
  • 0531 break;
  • 0532 i++;
  • 0533 }
  • 0534 if (i <= st + 1)
  • 0535 break syntax;
  • 0536 int k;
  • 0537 for (k = wtb.length; --k >= 0;)
  • 0538 if (wtb[k].regionMatches(true, 0, s, st, i - st)) {
  • 0539 int action = ttb[k];
  • 0540 if (action != 0) {
  • 0541 if (action == 1) { // pm
  • 0542 if (hour > 12 || hour < 1)
  • 0543 break syntax;
  • 0544 else if (hour < 12)
  • 0545 hour += 12;
  • 0546 } else if (action == 14) { // am
  • 0547 if (hour > 12 || hour < 1)
  • 0548 break syntax;
  • 0549 else if (hour == 12)
  • 0550 hour = 0;
  • 0551 } else if (action <= 13) { // month!
  • 0552 if (mon < 0)
  • 0553 mon = (byte) (action - 2);
  • 0554 else
  • 0555 break syntax;
  • 0556 } else {
  • 0557 tzoffset = action - 10000;
  • 0558 }
  • 0559 }
  • 0560 break;
  • 0561 }
  • 0562 if (k < 0)
  • 0563 break syntax;
  • 0564 prevc = 0;
  • 0565 }
  • 0566 }
  • 0567 if (year == Integer.MIN_VALUE || mon < 0 || mday < 0)
  • 0568 break syntax;
  • 0569 // Parse 2-digit years within the correct default century.
  • 0570 if (year < 100) {
  • 0571 synchronized (Date.class) {
  • 0572 if (defaultCenturyStart == 0) {
  • 0573 defaultCenturyStart = gcal.getCalendarDate().getYear() - 80;
  • 0574 }
  • 0575 }
  • 0576 year += (defaultCenturyStart / 100) * 100;
  • 0577 if (year < defaultCenturyStart) year += 100;
  • 0578 }
  • 0579 if (sec < 0)
  • 0580 sec = 0;
  • 0581 if (min < 0)
  • 0582 min = 0;
  • 0583 if (hour < 0)
  • 0584 hour = 0;
  • 0585 BaseCalendar cal = getCalendarSystem(year);
  • 0586 if (tzoffset == -1) { // no time zone specified, have to use local
  • 0587 BaseCalendar.Date ldate = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.getDefaultRef());
  • 0588 ldate.setDate(year, mon + 1, mday);
  • 0589 ldate.setTimeOfDay(hour, min, sec, 0);
  • 0590 return cal.getTime(ldate);
  • 0591 }
  • 0592 BaseCalendar.Date udate = (BaseCalendar.Date) cal.newCalendarDate(null); // no time zone
  • 0593 udate.setDate(year, mon + 1, mday);
  • 0594 udate.setTimeOfDay(hour, min, sec, 0);
  • 0595 return cal.getTime(udate) + tzoffset * (60 * 1000);
  • 0596 }
  • 0597 // syntax error
  • 0598 throw new IllegalArgumentException();
  • 0599 }
  • 0600 private final static String wtb[] = {
  • 0601 "am", "pm",
  • 0602 "monday", "tuesday", "wednesday", "thursday", "friday",
  • 0603 "saturday", "sunday",
  • 0604 "january", "february", "march", "april", "may", "june",
  • 0605 "july", "august", "september", "october", "november", "december",
  • 0606 "gmt", "ut", "utc", "est", "edt", "cst", "cdt",
  • 0607 "mst", "mdt", "pst", "pdt"
  • 0608 };
  • 0609 private final static int ttb[] = {
  • 0610 14, 1, 0, 0, 0, 0, 0, 0, 0,
  • 0611 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
  • 0612 10000 + 0, 10000 + 0, 10000 + 0, // GMT/UT/UTC
  • 0613 10000 + 5 * 60, 10000 + 4 * 60, // EST/EDT
  • 0614 10000 + 6 * 60, 10000 + 5 * 60, // CST/CDT
  • 0615 10000 + 7 * 60, 10000 + 6 * 60, // MST/MDT
  • 0616 10000 + 8 * 60, 10000 + 7 * 60 // PST/PDT
  • 0617 };
  • 0618
  • 0619 /**
  • 0620 * Returns a value that is the result of subtracting 1900 from the
  • 0621 * year that contains or begins with the instant in time represented
  • 0622 * by this <code>Date</code> object, as interpreted in the local
  • 0623 * time zone.
  • 0624 *
  • 0625 * @return the year represented by this date, minus 1900.
  • 0626 * @see java.util.Calendar
  • 0627 * @deprecated As of JDK version 1.1,
  • 0628 * replaced by <code>Calendar.get(Calendar.YEAR) - 1900</code>.
  • 0629 */
  • 0630 @Deprecated
  • 0631 public int getYear() {
  • 0632 return normalize().getYear() - 1900;
  • 0633 }
  • 0634
  • 0635 /**
  • 0636 * Sets the year of this <tt>Date</tt> object to be the specified
  • 0637 * value plus 1900. This <code>Date</code> object is modified so
  • 0638 * that it represents a point in time within the specified year,
  • 0639 * with the month, date, hour, minute, and second the same as
  • 0640 * before, as interpreted in the local time zone. (Of course, if
  • 0641 * the date was February 29, for example, and the year is set to a
  • 0642 * non-leap year, then the new date will be treated as if it were
  • 0643 * on March 1.)
  • 0644 *
  • 0645 * @param year the year value.
  • 0646 * @see java.util.Calendar
  • 0647 * @deprecated As of JDK version 1.1,
  • 0648 * replaced by <code>Calendar.set(Calendar.YEAR, year + 1900)</code>.
  • 0649 */
  • 0650 @Deprecated
  • 0651 public void setYear(int year) {
  • 0652 getCalendarDate().setNormalizedYear(year + 1900);
  • 0653 }
  • 0654
  • 0655 /**
  • 0656 * Returns a number representing the month that contains or begins
  • 0657 * with the instant in time represented by this <tt>Date</tt> object.
  • 0658 * The value returned is between <code>0</code> and <code>11</code>,
  • 0659 * with the value <code>0</code> representing January.
  • 0660 *
  • 0661 * @return the month represented by this date.
  • 0662 * @see java.util.Calendar
  • 0663 * @deprecated As of JDK version 1.1,
  • 0664 * replaced by <code>Calendar.get(Calendar.MONTH)</code>.
  • 0665 */
  • 0666 @Deprecated
  • 0667 public int getMonth() {
  • 0668 return normalize().getMonth() - 1; // adjust 1-based to 0-based
  • 0669 }
  • 0670
  • 0671 /**
  • 0672 * Sets the month of this date to the specified value. This
  • 0673 * <tt>Date</tt> object is modified so that it represents a point
  • 0674 * in time within the specified month, with the year, date, hour,
  • 0675 * minute, and second the same as before, as interpreted in the
  • 0676 * local time zone. If the date was October 31, for example, and
  • 0677 * the month is set to June, then the new date will be treated as
  • 0678 * if it were on July 1, because June has only 30 days.
  • 0679 *
  • 0680 * @param month the month value between 0-11.
  • 0681 * @see java.util.Calendar
  • 0682 * @deprecated As of JDK version 1.1,
  • 0683 * replaced by <code>Calendar.set(Calendar.MONTH, int month)</code>.
  • 0684 */
  • 0685 @Deprecated
  • 0686 public void setMonth(int month) {
  • 0687 int y = 0;
  • 0688 if (month >= 12) {
  • 0689 y = month / 12;
  • 0690 month %= 12;
  • 0691 } else if (month < 0) {
  • 0692 y = CalendarUtils.floorDivide(month, 12);
  • 0693 month = CalendarUtils.mod(month, 12);
  • 0694 }
  • 0695 BaseCalendar.Date d = getCalendarDate();
  • 0696 if (y != 0) {
  • 0697 d.setNormalizedYear(d.getNormalizedYear() + y);
  • 0698 }
  • 0699 d.setMonth(month + 1); // adjust 0-based to 1-based month numbering
  • 0700 }
  • 0701
  • 0702 /**
  • 0703 * Returns the day of the month represented by this <tt>Date</tt> object.
  • 0704 * The value returned is between <code>1</code> and <code>31</code>
  • 0705 * representing the day of the month that contains or begins with the
  • 0706 * instant in time represented by this <tt>Date</tt> object, as
  • 0707 * interpreted in the local time zone.
  • 0708 *
  • 0709 * @return the day of the month represented by this date.
  • 0710 * @see java.util.Calendar
  • 0711 * @deprecated As of JDK version 1.1,
  • 0712 * replaced by <code>Calendar.get(Calendar.DAY_OF_MONTH)</code>.
  • 0713 * @deprecated
  • 0714 */
  • 0715 @Deprecated
  • 0716 public int getDate() {
  • 0717 return normalize().getDayOfMonth();
  • 0718 }
  • 0719
  • 0720 /**
  • 0721 * Sets the day of the month of this <tt>Date</tt> object to the
  • 0722 * specified value. This <tt>Date</tt> object is modified so that
  • 0723 * it represents a point in time within the specified day of the
  • 0724 * month, with the year, month, hour, minute, and second the same
  • 0725 * as before, as interpreted in the local time zone. If the date
  • 0726 * was April 30, for example, and the date is set to 31, then it
  • 0727 * will be treated as if it were on May 1, because April has only
  • 0728 * 30 days.
  • 0729 *
  • 0730 * @param date the day of the month value between 1-31.
  • 0731 * @see java.util.Calendar
  • 0732 * @deprecated As of JDK version 1.1,
  • 0733 * replaced by <code>Calendar.set(Calendar.DAY_OF_MONTH, int date)</code>.
  • 0734 */
  • 0735 @Deprecated
  • 0736 public void setDate(int date) {
  • 0737 getCalendarDate().setDayOfMonth(date);
  • 0738 }
  • 0739
  • 0740 /**
  • 0741 * Returns the day of the week represented by this date. The
  • 0742 * returned value (<tt>0</tt> = Sunday, <tt>1</tt> = Monday,
  • 0743 * <tt>2</tt> = Tuesday, <tt>3</tt> = Wednesday, <tt>4</tt> =
  • 0744 * Thursday, <tt>5</tt> = Friday, <tt>6</tt> = Saturday)
  • 0745 * represents the day of the week that contains or begins with
  • 0746 * the instant in time represented by this <tt>Date</tt> object,
  • 0747 * as interpreted in the local time zone.
  • 0748 *
  • 0749 * @return the day of the week represented by this date.
  • 0750 * @see java.util.Calendar
  • 0751 * @deprecated As of JDK version 1.1,
  • 0752 * replaced by <code>Calendar.get(Calendar.DAY_OF_WEEK)</code>.
  • 0753 */
  • 0754 @Deprecated
  • 0755 public int getDay() {
  • 0756 return normalize().getDayOfWeek() - gcal.SUNDAY;
  • 0757 }
  • 0758
  • 0759 /**
  • 0760 * Returns the hour represented by this <tt>Date</tt> object. The
  • 0761 * returned value is a number (<tt>0</tt> through <tt>23</tt>)
  • 0762 * representing the hour within the day that contains or begins
  • 0763 * with the instant in time represented by this <tt>Date</tt>
  • 0764 * object, as interpreted in the local time zone.
  • 0765 *
  • 0766 * @return the hour represented by this date.
  • 0767 * @see java.util.Calendar
  • 0768 * @deprecated As of JDK version 1.1,
  • 0769 * replaced by <code>Calendar.get(Calendar.HOUR_OF_DAY)</code>.
  • 0770 */
  • 0771 @Deprecated
  • 0772 public int getHours() {
  • 0773 return normalize().getHours();
  • 0774 }
  • 0775
  • 0776 /**
  • 0777 * Sets the hour of this <tt>Date</tt> object to the specified value.
  • 0778 * This <tt>Date</tt> object is modified so that it represents a point
  • 0779 * in time within the specified hour of the day, with the year, month,
  • 0780 * date, minute, and second the same as before, as interpreted in the
  • 0781 * local time zone.
  • 0782 *
  • 0783 * @param hours the hour value.
  • 0784 * @see java.util.Calendar
  • 0785 * @deprecated As of JDK version 1.1,
  • 0786 * replaced by <code>Calendar.set(Calendar.HOUR_OF_DAY, int hours)</code>.
  • 0787 */
  • 0788 @Deprecated
  • 0789 public void setHours(int hours) {
  • 0790 getCalendarDate().setHours(hours);
  • 0791 }
  • 0792
  • 0793 /**
  • 0794 * Returns the number of minutes past the hour represented by this date,
  • 0795 * as interpreted in the local time zone.
  • 0796 * The value returned is between <code>0</code> and <code>59</code>.
  • 0797 *
  • 0798 * @return the number of minutes past the hour represented by this date.
  • 0799 * @see java.util.Calendar
  • 0800 * @deprecated As of JDK version 1.1,
  • 0801 * replaced by <code>Calendar.get(Calendar.MINUTE)</code>.
  • 0802 */
  • 0803 @Deprecated
  • 0804 public int getMinutes() {
  • 0805 return normalize().getMinutes();
  • 0806 }
  • 0807
  • 0808 /**
  • 0809 * Sets the minutes of this <tt>Date</tt> object to the specified value.
  • 0810 * This <tt>Date</tt> object is modified so that it represents a point
  • 0811 * in time within the specified minute of the hour, with the year, month,
  • 0812 * date, hour, and second the same as before, as interpreted in the
  • 0813 * local time zone.
  • 0814 *
  • 0815 * @param minutes the value of the minutes.
  • 0816 * @see java.util.Calendar
  • 0817 * @deprecated As of JDK version 1.1,
  • 0818 * replaced by <code>Calendar.set(Calendar.MINUTE, int minutes)</code>.
  • 0819 */
  • 0820 @Deprecated
  • 0821 public void setMinutes(int minutes) {
  • 0822 getCalendarDate().setMinutes(minutes);
  • 0823 }
  • 0824
  • 0825 /**
  • 0826 * Returns the number of seconds past the minute represented by this date.
  • 0827 * The value returned is between <code>0</code> and <code>61</code>. The
  • 0828 * values <code>60</code> and <code>61</code> can only occur on those
  • 0829 * Java Virtual Machines that take leap seconds into account.
  • 0830 *
  • 0831 * @return the number of seconds past the minute represented by this date.
  • 0832 * @see java.util.Calendar
  • 0833 * @deprecated As of JDK version 1.1,
  • 0834 * replaced by <code>Calendar.get(Calendar.SECOND)</code>.
  • 0835 */
  • 0836 @Deprecated
  • 0837 public int getSeconds() {
  • 0838 return normalize().getSeconds();
  • 0839 }
  • 0840
  • 0841 /**
  • 0842 * Sets the seconds of this <tt>Date</tt> to the specified value.
  • 0843 * This <tt>Date</tt> object is modified so that it represents a
  • 0844 * point in time within the specified second of the minute, with
  • 0845 * the year, month, date, hour, and minute the same as before, as
  • 0846 * interpreted in the local time zone.
  • 0847 *
  • 0848 * @param seconds the seconds value.
  • 0849 * @see java.util.Calendar
  • 0850 * @deprecated As of JDK version 1.1,
  • 0851 * replaced by <code>Calendar.set(Calendar.SECOND, int seconds)</code>.
  • 0852 */
  • 0853 @Deprecated
  • 0854 public void setSeconds(int seconds) {
  • 0855 getCalendarDate().setSeconds(seconds);
  • 0856 }
  • 0857
  • 0858 /**
  • 0859 * Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT
  • 0860 * represented by this <tt>Date</tt> object.
  • 0861 *
  • 0862 * @return the number of milliseconds since January 1, 1970, 00:00:00 GMT
  • 0863 * represented by this date.
  • 0864 */
  • 0865 public long getTime() {
  • 0866 return getTimeImpl();
  • 0867 }
  • 0868
  • 0869 private final long getTimeImpl() {
  • 0870 if (cdate != null && !cdate.isNormalized()) {
  • 0871 normalize();
  • 0872 }
  • 0873 return fastTime;
  • 0874 }
  • 0875
  • 0876 /**
  • 0877 * Sets this <code>Date</code> object to represent a point in time that is
  • 0878 * <code>time</code> milliseconds after January 1, 1970 00:00:00 GMT.
  • 0879 *
  • 0880 * @param time the number of milliseconds.
  • 0881 */
  • 0882 public void setTime(long time) {
  • 0883 fastTime = time;
  • 0884 cdate = null;
  • 0885 }
  • 0886
  • 0887 /**
  • 0888 * Tests if this date is before the specified date.
  • 0889 *
  • 0890 * @param when a date.
  • 0891 * @return <code>true</code> if and only if the instant of time
  • 0892 * represented by this <tt>Date</tt> object is strictly
  • 0893 * earlier than the instant represented by <tt>when</tt>;
  • 0894 * <code>false</code> otherwise.
  • 0895 * @exception NullPointerException if <code>when</code> is null.
  • 0896 */
  • 0897 public boolean before(Date when) {
  • 0898 return getMillisOf(this) < getMillisOf(when);
  • 0899 }
  • 0900
  • 0901 /**
  • 0902 * Tests if this date is after the specified date.
  • 0903 *
  • 0904 * @param when a date.
  • 0905 * @return <code>true</code> if and only if the instant represented
  • 0906 * by this <tt>Date</tt> object is strictly later than the
  • 0907 * instant represented by <tt>when</tt>;
  • 0908 * <code>false</code> otherwise.
  • 0909 * @exception NullPointerException if <code>when</code> is null.
  • 0910 */
  • 0911 public boolean after(Date when) {
  • 0912 return getMillisOf(this) > getMillisOf(when);
  • 0913 }
  • 0914
  • 0915 /**
  • 0916 * Compares two dates for equality.
  • 0917 * The result is <code>true</code> if and only if the argument is
  • 0918 * not <code>null</code> and is a <code>Date</code> object that
  • 0919 * represents the same point in time, to the millisecond, as this object.
  • 0920 * <p>
  • 0921 * Thus, two <code>Date</code> objects are equal if and only if the
  • 0922 * <code>getTime</code> method returns the same <code>long</code>
  • 0923 * value for both.
  • 0924 *
  • 0925 * @param obj the object to compare with.
  • 0926 * @return <code>true</code> if the objects are the same;
  • 0927 * <code>false</code> otherwise.
  • 0928 * @see java.util.Date#getTime()
  • 0929 */
  • 0930 public boolean equals(Object obj) {
  • 0931 return obj instanceof Date && getTime() == ((Date) obj).getTime();
  • 0932 }
  • 0933
  • 0934 /**
  • 0935 * Returns the millisecond value of this <code>Date</code> object
  • 0936 * without affecting its internal state.
  • 0937 */
  • 0938 static final long getMillisOf(Date date) {
  • 0939 if (date.cdate == null) {
  • 0940 return date.fastTime;
  • 0941 }
  • 0942 BaseCalendar.Date d = (BaseCalendar.Date) date.cdate.clone();
  • 0943 return gcal.getTime(d);
  • 0944 }
  • 0945
  • 0946 /**
  • 0947 * Compares two Dates for ordering.
  • 0948 *
  • 0949 * @param anotherDate the <code>Date</code> to be compared.
  • 0950 * @return the value <code>0</code> if the argument Date is equal to
  • 0951 * this Date; a value less than <code>0</code> if this Date
  • 0952 * is before the Date argument; and a value greater than
  • 0953 * <code>0</code> if this Date is after the Date argument.
  • 0954 * @since 1.2
  • 0955 * @exception NullPointerException if <code>anotherDate</code> is null.
  • 0956 */
  • 0957 public int compareTo(Date anotherDate) {
  • 0958 long thisTime = getMillisOf(this);
  • 0959 long anotherTime = getMillisOf(anotherDate);
  • 0960 return (thisTime<anotherTime ? -1 : (thisTime==anotherTime ? 0 : 1));
  • 0961 }
  • 0962
  • 0963 /**
  • 0964 * Returns a hash code value for this object. The result is the
  • 0965 * exclusive OR of the two halves of the primitive <tt>long</tt>
  • 0966 * value returned by the {@link Date#getTime}
  • 0967 * method. That is, the hash code is the value of the expression:
  • 0968 * <blockquote><pre>
  • 0969 * (int)(this.getTime()^(this.getTime() >>> 32))</pre></blockquote>
  • 0970 *
  • 0971 * @return a hash code value for this object.
  • 0972 */
  • 0973 public int hashCode() {
  • 0974 long ht = this.getTime();
  • 0975 return (int) ht ^ (int) (ht >> 32);
  • 0976 }
  • 0977
  • 0978 /**
  • 0979 * Converts this <code>Date</code> object to a <code>String</code>
  • 0980 * of the form:
  • 0981 * <blockquote><pre>
  • 0982 * dow mon dd hh:mm:ss zzz yyyy</pre></blockquote>
  • 0983 * where:<ul>
  • 0984 * <li><tt>dow</tt> is the day of the week (<tt>Sun, Mon, Tue, Wed,
  • 0985 * Thu, Fri, Sat</tt>).
  • 0986 * <li><tt>mon</tt> is the month (<tt>Jan, Feb, Mar, Apr, May, Jun,
  • 0987 * Jul, Aug, Sep, Oct, Nov, Dec</tt>).
  • 0988 * <li><tt>dd</tt> is the day of the month (<tt>01</tt> through
  • 0989 * <tt>31</tt>), as two decimal digits.
  • 0990 * <li><tt>hh</tt> is the hour of the day (<tt>00</tt> through
  • 0991 * <tt>23</tt>), as two decimal digits.
  • 0992 * <li><tt>mm</tt> is the minute within the hour (<tt>00</tt> through
  • 0993 * <tt>59</tt>), as two decimal digits.
  • 0994 * <li><tt>ss</tt> is the second within the minute (<tt>00</tt> through
  • 0995 * <tt>61</tt>, as two decimal digits.
  • 0996 * <li><tt>zzz</tt> is the time zone (and may reflect daylight saving
  • 0997 * time). Standard time zone abbreviations include those
  • 0998 * recognized by the method <tt>parse</tt>. If time zone
  • 0999 * information is not available, then <tt>zzz</tt> is empty -
  • 1000 * that is, it consists of no characters at all.
  • 1001 * <li><tt>yyyy</tt> is the year, as four decimal digits.
  • 1002 * </ul>
  • 1003 *
  • 1004 * @return a string representation of this date.
  • 1005 * @see java.util.Date#toLocaleString()
  • 1006 * @see java.util.Date#toGMTString()
  • 1007 */
  • 1008 public String toString() {
  • 1009 // "EEE MMM dd HH:mm:ss zzz yyyy";
  • 1010 BaseCalendar.Date date = normalize();
  • 1011 StringBuilder sb = new StringBuilder(28);
  • 1012 int index = date.getDayOfWeek();
  • 1013 if (index == gcal.SUNDAY) {
  • 1014 index = 8;
  • 1015 }
  • 1016 convertToAbbr(sb, wtb[index]).append(' '); // EEE
  • 1017 convertToAbbr(sb, wtb[date.getMonth() - 1 + 2 + 7]).append(' '); // MMM
  • 1018 CalendarUtils.sprintf0d(sb, date.getDayOfMonth(), 2).append(' '); // dd
  • 1019
  • 1020 CalendarUtils.sprintf0d(sb, date.getHours(), 2).append(':'); // HH
  • 1021 CalendarUtils.sprintf0d(sb, date.getMinutes(), 2).append(':'); // mm
  • 1022 CalendarUtils.sprintf0d(sb, date.getSeconds(), 2).append(' '); // ss
  • 1023 TimeZone zi = date.getZone();
  • 1024 if (zi != null) {
  • 1025 sb.append(zi.getDisplayName(date.isDaylightTime(), zi.SHORT, Locale.US)); // zzz
  • 1026 } else {
  • 1027 sb.append("GMT");
  • 1028 }
  • 1029 sb.append(' ').append(date.getYear()); // yyyy
  • 1030 return sb.toString();
  • 1031 }
  • 1032
  • 1033 /**
  • 1034 * Converts the given name to its 3-letter abbreviation (e.g.,
  • 1035 * "monday" -> "Mon") and stored the abbreviation in the given
  • 1036 * <code>StringBuilder</code>.
  • 1037 */
  • 1038 private static final StringBuilder convertToAbbr(StringBuilder sb, String name) {
  • 1039 sb.append(Character.toUpperCase(name.charAt(0)));
  • 1040 sb.append(name.charAt(1)).append(name.charAt(2));
  • 1041 return sb;
  • 1042 }
  • 1043
  • 1044 /**
  • 1045 * Creates a string representation of this <tt>Date</tt> object in an
  • 1046 * implementation-dependent form. The intent is that the form should
  • 1047 * be familiar to the user of the Java application, wherever it may
  • 1048 * happen to be running. The intent is comparable to that of the
  • 1049 * "<code>%c</code>" format supported by the <code>strftime()</code>
  • 1050 * function of ISO C.
  • 1051 *
  • 1052 * @return a string representation of this date, using the locale
  • 1053 * conventions.
  • 1054 * @see java.text.DateFormat
  • 1055 * @see java.util.Date#toString()
  • 1056 * @see java.util.Date#toGMTString()
  • 1057 * @deprecated As of JDK version 1.1,
  • 1058 * replaced by <code>DateFormat.format(Date date)</code>.
  • 1059 */
  • 1060 @Deprecated
  • 1061 public String toLocaleString() {
  • 1062 DateFormat formatter = DateFormat.getDateTimeInstance();
  • 1063 return formatter.format(this);
  • 1064 }
  • 1065
  • 1066 /**
  • 1067 * Creates a string representation of this <tt>Date</tt> object of
  • 1068 * the form:
  • 1069 * <blockquote<pre>
  • 1070 * d mon yyyy hh:mm:ss GMT</pre></blockquote>
  • 1071 * where:<ul>
  • 1072 * <li><i>d</i> is the day of the month (<tt>1</tt> through <tt>31</tt>),
  • 1073 * as one or two decimal digits.
  • 1074 * <li><i>mon</i> is the month (<tt>Jan, Feb, Mar, Apr, May, Jun, Jul,
  • 1075 * Aug, Sep, Oct, Nov, Dec</tt>).
  • 1076 * <li><i>yyyy</i> is the year, as four decimal digits.
  • 1077 * <li><i>hh</i> is the hour of the day (<tt>00</tt> through <tt>23</tt>),
  • 1078 * as two decimal digits.
  • 1079 * <li><i>mm</i> is the minute within the hour (<tt>00</tt> through
  • 1080 * <tt>59</tt>), as two decimal digits.
  • 1081 * <li><i>ss</i> is the second within the minute (<tt>00</tt> through
  • 1082 * <tt>61</tt>), as two decimal digits.
  • 1083 * <li><i>GMT</i> is exactly the ASCII letters "<tt>GMT</tt>" to indicate
  • 1084 * Greenwich Mean Time.
  • 1085 * </ul><p>
  • 1086 * The result does not depend on the local time zone.
  • 1087 *
  • 1088 * @return a string representation of this date, using the Internet GMT
  • 1089 * conventions.
  • 1090 * @see java.text.DateFormat
  • 1091 * @see java.util.Date#toString()
  • 1092 * @see java.util.Date#toLocaleString()
  • 1093 * @deprecated As of JDK version 1.1,
  • 1094 * replaced by <code>DateFormat.format(Date date)</code>, using a
  • 1095 * GMT <code>TimeZone</code>.
  • 1096 */
  • 1097 @Deprecated
  • 1098 public String toGMTString() {
  • 1099 // d MMM yyyy HH:mm:ss 'GMT'
  • 1100 long t = getTime();
  • 1101 BaseCalendar cal = getCalendarSystem(t);
  • 1102 BaseCalendar.Date date =
  • 1103 (BaseCalendar.Date) cal.getCalendarDate(getTime(), (TimeZone)null);
  • 1104 StringBuilder sb = new StringBuilder(32);
  • 1105 CalendarUtils.sprintf0d(sb, date.getDayOfMonth(), 1).append(' '); // d
  • 1106 convertToAbbr(sb, wtb[date.getMonth() - 1 + 2 + 7]).append(' '); // MMM
  • 1107 sb.append(date.getYear()).append(' '); // yyyy
  • 1108 CalendarUtils.sprintf0d(sb, date.getHours(), 2).append(':'); // HH
  • 1109 CalendarUtils.sprintf0d(sb, date.getMinutes(), 2).append(':'); // mm
  • 1110 CalendarUtils.sprintf0d(sb, date.getSeconds(), 2); // ss
  • 1111 sb.append(" GMT"); // ' GMT'
  • 1112 return sb.toString();
  • 1113 }
  • 1114
  • 1115 /**
  • 1116 * Returns the offset, measured in minutes, for the local time zone
  • 1117 * relative to UTC that is appropriate for the time represented by
  • 1118 * this <code>Date</code> object.
  • 1119 * <p>
  • 1120 * For example, in Massachusetts, five time zones west of Greenwich:
  • 1121 * <blockquote><pre>
  • 1122 * new Date(96, 1, 14).getTimezoneOffset() returns 300</pre></blockquote>
  • 1123 * because on February 14, 1996, standard time (Eastern Standard Time)
  • 1124 * is in use, which is offset five hours from UTC; but:
  • 1125 * <blockquote><pre>
  • 1126 * new Date(96, 5, 1).getTimezoneOffset() returns 240</pre></blockquote>
  • 1127 * because on June 1, 1996, daylight saving time (Eastern Daylight Time)
  • 1128 * is in use, which is offset only four hours from UTC.<p>
  • 1129 * This method produces the same result as if it computed:
  • 1130 * <blockquote><pre>
  • 1131 * (this.getTime() - UTC(this.getYear(),
  • 1132 * this.getMonth(),
  • 1133 * this.getDate(),
  • 1134 * this.getHours(),
  • 1135 * this.getMinutes(),
  • 1136 * this.getSeconds())) / (60 * 1000)
  • 1137 * </pre></blockquote>
  • 1138 *
  • 1139 * @return the time-zone offset, in minutes, for the current time zone.
  • 1140 * @see java.util.Calendar#ZONE_OFFSET
  • 1141 * @see java.util.Calendar#DST_OFFSET
  • 1142 * @see java.util.TimeZone#getDefault
  • 1143 * @deprecated As of JDK version 1.1,
  • 1144 * replaced by <code>-(Calendar.get(Calendar.ZONE_OFFSET) +
  • 1145 * Calendar.get(Calendar.DST_OFFSET)) / (60 * 1000)</code>.
  • 1146 */
  • 1147 @Deprecated
  • 1148 public int getTimezoneOffset() {
  • 1149 int zoneOffset;
  • 1150 if (cdate == null) {
  • 1151 TimeZone tz = TimeZone.getDefaultRef();
  • 1152 if (tz instanceof ZoneInfo) {
  • 1153 zoneOffset = ((ZoneInfo)tz).getOffsets(fastTime, null);
  • 1154 } else {
  • 1155 zoneOffset = tz.getOffset(fastTime);
  • 1156 }
  • 1157 } else {
  • 1158 normalize();
  • 1159 zoneOffset = cdate.getZoneOffset();
  • 1160 }
  • 1161 return -zoneOffset/60000; // convert to minutes
  • 1162 }
  • 1163
  • 1164 private final BaseCalendar.Date getCalendarDate() {
  • 1165 if (cdate == null) {
  • 1166 BaseCalendar cal = getCalendarSystem(fastTime);
  • 1167 cdate = (BaseCalendar.Date) cal.getCalendarDate(fastTime,
  • 1168 TimeZone.getDefaultRef());
  • 1169 }
  • 1170 return cdate;
  • 1171 }
  • 1172
  • 1173 private final BaseCalendar.Date normalize() {
  • 1174 if (cdate == null) {
  • 1175 BaseCalendar cal = getCalendarSystem(fastTime);
  • 1176 cdate = (BaseCalendar.Date) cal.getCalendarDate(fastTime,
  • 1177 TimeZone.getDefaultRef());
  • 1178 return cdate;
  • 1179 }
  • 1180
  • 1181 // Normalize cdate with the TimeZone in cdate first. This is
  • 1182 // required for the compatible behavior.
  • 1183 if (!cdate.isNormalized()) {
  • 1184 cdate = normalize(cdate);
  • 1185 }
  • 1186
  • 1187 // If the default TimeZone has changed, then recalculate the
  • 1188 // fields with the new TimeZone.
  • 1189 TimeZone tz = TimeZone.getDefaultRef();
  • 1190 if (tz != cdate.getZone()) {
  • 1191 cdate.setZone(tz);
  • 1192 CalendarSystem cal = getCalendarSystem(cdate);
  • 1193 cal.getCalendarDate(fastTime, cdate);
  • 1194 }
  • 1195 return cdate;
  • 1196 }
  • 1197
  • 1198 // fastTime and the returned data are in sync upon return.
  • 1199 private final BaseCalendar.Date normalize(BaseCalendar.Date date) {
  • 1200 int y = date.getNormalizedYear();
  • 1201 int m = date.getMonth();
  • 1202 int d = date.getDayOfMonth();
  • 1203 int hh = date.getHours();
  • 1204 int mm = date.getMinutes();
  • 1205 int ss = date.getSeconds();
  • 1206 int ms = date.getMillis();
  • 1207 TimeZone tz = date.getZone();
  • 1208
  • 1209 // If the specified year can't be handled using a long value
  • 1210 // in milliseconds, GregorianCalendar is used for full
  • 1211 // compatibility with underflow and overflow. This is required
  • 1212 // by some JCK tests. The limits are based max year values -
  • 1213 // years that can be represented by max values of d, hh, mm,
  • 1214 // ss and ms. Also, let GregorianCalendar handle the default
  • 1215 // cutover year so that we don't need to worry about the
  • 1216 // transition here.
  • 1217 if (y == 1582 || y > 280000000 || y < -280000000) {
  • 1218 if (tz == null) {
  • 1219 tz = TimeZone.getTimeZone("GMT");
  • 1220 }
  • 1221 GregorianCalendar gc = new GregorianCalendar(tz);
  • 1222 gc.clear();
  • 1223 gc.set(gc.MILLISECOND, ms);
  • 1224 gc.set(y, m-1, d, hh, mm, ss);
  • 1225 fastTime = gc.getTimeInMillis();
  • 1226 BaseCalendar cal = getCalendarSystem(fastTime);
  • 1227 date = (BaseCalendar.Date) cal.getCalendarDate(fastTime, tz);
  • 1228 return date;
  • 1229 }
  • 1230
  • 1231 BaseCalendar cal = getCalendarSystem(y);
  • 1232 if (cal != getCalendarSystem(date)) {
  • 1233 date = (BaseCalendar.Date) cal.newCalendarDate(tz);
  • 1234 date.setNormalizedDate(y, m, d).setTimeOfDay(hh, mm, ss, ms);
  • 1235 }
  • 1236 // Perform the GregorianCalendar-style normalization.
  • 1237 fastTime = cal.getTime(date);
  • 1238
  • 1239 // In case the normalized date requires the other calendar
  • 1240 // system, we need to recalculate it using the other one.
  • 1241 BaseCalendar ncal = getCalendarSystem(fastTime);
  • 1242 if (ncal != cal) {
  • 1243 date = (BaseCalendar.Date) ncal.newCalendarDate(tz);
  • 1244 date.setNormalizedDate(y, m, d).setTimeOfDay(hh, mm, ss, ms);
  • 1245 fastTime = ncal.getTime(date);
  • 1246 }
  • 1247 return date;
  • 1248 }
  • 1249
  • 1250 /**
  • 1251 * Returns the Gregorian or Julian calendar system to use with the
  • 1252 * given date. Use Gregorian from October 15, 1582.
  • 1253 *
  • 1254 * @param year normalized calendar year (not -1900)
  • 1255 * @return the CalendarSystem to use for the specified date
  • 1256 */
  • 1257 private static final BaseCalendar getCalendarSystem(int year) {
  • 1258 if (year >= 1582) {
  • 1259 return gcal;
  • 1260 }
  • 1261 return getJulianCalendar();
  • 1262 }
  • 1263
  • 1264 private static final BaseCalendar getCalendarSystem(long utc) {
  • 1265 // Quickly check if the time stamp given by `utc' is the Epoch
  • 1266 // or later. If it's before 1970, we convert the cutover to
  • 1267 // local time to compare.
  • 1268 if (utc >= 0
  • 1269 || utc >= GregorianCalendar.DEFAULT_GREGORIAN_CUTOVER
  • 1270 - TimeZone.getDefaultRef().getOffset(utc)) {
  • 1271 return gcal;
  • 1272 }
  • 1273 return getJulianCalendar();
  • 1274 }
  • 1275
  • 1276 private static final BaseCalendar getCalendarSystem(BaseCalendar.Date cdate) {
  • 1277 if (jcal == null) {
  • 1278 return gcal;
  • 1279 }
  • 1280 if (cdate.getEra() != null) {
  • 1281 return jcal;
  • 1282 }
  • 1283 return gcal;
  • 1284 }
  • 1285
  • 1286 synchronized private static final BaseCalendar getJulianCalendar() {
  • 1287 if (jcal == null) {
  • 1288 jcal = (BaseCalendar) CalendarSystem.forName("julian");
  • 1289 }
  • 1290 return jcal;
  • 1291 }
  • 1292
  • 1293 /**
  • 1294 * Save the state of this object to a stream (i.e., serialize it).
  • 1295 *
  • 1296 * @serialData The value returned by <code>getTime()</code>
  • 1297 * is emitted (long). This represents the offset from
  • 1298 * January 1, 1970, 00:00:00 GMT in milliseconds.
  • 1299 */
  • 1300 private void writeObject(ObjectOutputStream s)
  • 1301 throws IOException
  • 1302 {
  • 1303 s.writeLong(getTimeImpl());
  • 1304 }
  • 1305
  • 1306 /**
  • 1307 * Reconstitute this object from a stream (i.e., deserialize it).
  • 1308 */
  • 1309 private void readObject(ObjectInputStream s)
  • 1310 throws IOException, ClassNotFoundException
  • 1311 {
  • 1312 fastTime = s.readLong();
  • 1313 }
  • 1314}

文件:Date.java
包名:java.util
类名:Date
继承:
接口:[java.io.Serializable][Cloneable][Comparable]