1. /*
  2. * @(#)Date.java 1.69 01/02/09
  3. *
  4. * Copyright 1994-2001 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.util;
  11. import java.util.Calendar;
  12. import java.util.GregorianCalendar;
  13. import java.util.TimeZone;
  14. import java.text.DateFormat;
  15. import java.text.SimpleDateFormat;
  16. import java.io.IOException;
  17. import java.io.ObjectOutputStream;
  18. import java.io.ObjectInputStream;
  19. import java.lang.ref.SoftReference;
  20. /**
  21. * The class <code>Date</code> represents a specific instant
  22. * in time, with millisecond precision.
  23. * <p>
  24. * Prior to JDK 1.1, the class <code>Date</code> had two additional
  25. * functions. It allowed the interpretation of dates as year, month, day, hour,
  26. * minute, and second values. It also allowed the formatting and parsing
  27. * of date strings. Unfortunately, the API for these functions was not
  28. * amenable to internationalization. As of JDK 1.1, the
  29. * <code>Calendar</code> class should be used to convert between dates and time
  30. * fields and the <code>DateFormat</code> class should be used to format and
  31. * parse date strings.
  32. * The corresponding methods in <code>Date</code> are deprecated.
  33. * <p>
  34. * Although the <code>Date</code> class is intended to reflect
  35. * coordinated universal time (UTC), it may not do so exactly,
  36. * depending on the host environment of the Java Virtual Machine.
  37. * Nearly all modern operating systems assume that 1 day =
  38. * 24 × 60 × 60 = 86400 seconds
  39. * in all cases. In UTC, however, about once every year or two there
  40. * is an extra second, called a "leap second." The leap
  41. * second is always added as the last second of the day, and always
  42. * on December 31 or June 30. For example, the last minute of the
  43. * year 1995 was 61 seconds long, thanks to an added leap second.
  44. * Most computer clocks are not accurate enough to be able to reflect
  45. * the leap-second distinction.
  46. * <p>
  47. * Some computer standards are defined in terms of Greenwich mean
  48. * time (GMT), which is equivalent to universal time (UT). GMT is
  49. * the "civil" name for the standard; UT is the
  50. * "scientific" name for the same standard. The
  51. * distinction between UTC and UT is that UTC is based on an atomic
  52. * clock and UT is based on astronomical observations, which for all
  53. * practical purposes is an invisibly fine hair to split. Because the
  54. * earth's rotation is not uniform (it slows down and speeds up
  55. * in complicated ways), UT does not always flow uniformly. Leap
  56. * seconds are introduced as needed into UTC so as to keep UTC within
  57. * 0.9 seconds of UT1, which is a version of UT with certain
  58. * corrections applied. There are other time and date systems as
  59. * well; for example, the time scale used by the satellite-based
  60. * global positioning system (GPS) is synchronized to UTC but is
  61. * <i>not</i> adjusted for leap seconds. An interesting source of
  62. * further information is the U.S. Naval Observatory, particularly
  63. * the Directorate of Time at:
  64. * <blockquote><pre>
  65. * <a href=http://tycho.usno.navy.mil>http://tycho.usno.navy.mil</a>
  66. * </pre></blockquote>
  67. * <p>
  68. * and their definitions of "Systems of Time" at:
  69. * <blockquote><pre>
  70. * <a href=http://tycho.usno.navy.mil/systime.html>http://tycho.usno.navy.mil/systime.html</a>
  71. * </pre></blockquote>
  72. * <p>
  73. * In all methods of class <code>Date</code> that accept or return
  74. * year, month, date, hours, minutes, and seconds values, the
  75. * following representations are used:
  76. * <ul>
  77. * <li>A year <i>y</i> is represented by the integer
  78. * <i>y</i> <code>- 1900</code>.
  79. * <li>A month is represented by an integer form 0 to 11; 0 is January,
  80. * 1 is February, and so forth; thus 11 is December.
  81. * <li>A date (day of month) is represented by an integer from 1 to 31
  82. * in the usual manner.
  83. * <li>An hour is represented by an integer from 0 to 23. Thus, the hour
  84. * from midnight to 1 a.m. is hour 0, and the hour from noon to 1
  85. * p.m. is hour 12.
  86. * <li>A minute is represented by an integer from 0 to 59 in the usual manner.
  87. * <li>A second is represented by an integer from 0 to 61; the values 60 and
  88. * 61 occur only for leap seconds and even then only in Java
  89. * implementations that actually track leap seconds correctly. Because
  90. * of the manner in which leap seconds are currently introduced, it is
  91. * extremely unlikely that two leap seconds will occur in the same
  92. * minute, but this specification follows the date and time conventions
  93. * for ISO C.
  94. * </ul>
  95. * <p>
  96. * In all cases, arguments given to methods for these purposes need
  97. * not fall within the indicated ranges; for example, a date may be
  98. * specified as January 32 and is interpreted as meaning February 1.
  99. *
  100. * @author James Gosling
  101. * @author Arthur van Hoff
  102. * @author Alan Liu
  103. * @version 1.69, 02/09/01
  104. * @see java.text.DateFormat
  105. * @see java.util.Calendar
  106. * @see java.util.TimeZone
  107. * @since JDK1.0
  108. */
  109. public class Date implements java.io.Serializable, Cloneable, Comparable {
  110. /* DEFAULT ZONE SYNCHRONIZATION: Part of the usage model of Date
  111. * is that a Date object behaves like a Calendar object whose zone
  112. * is the current default TimeZone. As a result, we must be
  113. * careful about keeping this phantom calendar in sync with the
  114. * default TimeZone. There are three class and instance variables
  115. * to watch out for to achieve this. (1)staticCal. Whenever this
  116. * object is used, it must be reset to the default zone. This is a
  117. * cheap operation which can be done directly (just a reference
  118. * assignment), so we just do it every time. (2)simpleFormatter.
  119. * Likewise, the DateFormat object we use to implement toString()
  120. * must be reset to the current default zone before use. Again,
  121. * this is a cheap reference assignment. (3)cal. This is a little
  122. * more tricky. Unlike the other cached static objects, cal has
  123. * state, and we don't want to monkey with it willy-nilly. The
  124. * correct procedure is to change the zone in a way that doesn't
  125. * alter the time of this object. This means getting the millis
  126. * (forcing a fields->time conversion), setting the zone, and then
  127. * restoring the millis. The zone must be set before restoring
  128. * the millis. Since this is an expensive operation, we only do
  129. * this when we have to. - liu 1.2b4 */
  130. /* If cal is null, then fastTime indicates the time in millis.
  131. * Otherwise, fastTime is ignored, and cal indicates the time.
  132. * The cal object is only created if a setXxx call is made to
  133. * set a field. For other operations, staticCal is used instead.
  134. */
  135. private transient Calendar cal;
  136. private transient long fastTime;
  137. private static Calendar staticCal = null;
  138. private static Calendar utcCal = null;
  139. private static int defaultCenturyStart = 0;
  140. /* use serialVersionUID from modified java.util.Date for
  141. * interoperability with JDK1.1. The Date was modified to write
  142. * and read only the UTC time.
  143. */
  144. private static final long serialVersionUID = 7523967970034938905L;
  145. /**
  146. * Caches for the DateFormatters used by various toString methods.
  147. */
  148. private static SoftReference simpleFormatter = null;
  149. private static SoftReference gmtFormatter = null;
  150. /**
  151. * Allocates a <code>Date</code> object and initializes it so that
  152. * it represents the time at which it was allocated, measured to the
  153. * nearest millisecond.
  154. *
  155. * @see java.lang.System#currentTimeMillis()
  156. */
  157. public Date() {
  158. this(System.currentTimeMillis());
  159. }
  160. /**
  161. * Allocates a <code>Date</code> object and initializes it to
  162. * represent the specified number of milliseconds since the
  163. * standard base time known as "the epoch", namely January 1,
  164. * 1970, 00:00:00 GMT.
  165. *
  166. * @param date the milliseconds since January 1, 1970, 00:00:00 GMT.
  167. * @see java.lang.System#currentTimeMillis()
  168. */
  169. public Date(long date) {
  170. cal = null;
  171. fastTime = date;
  172. }
  173. /**
  174. * Allocates a <code>Date</code> object and initializes it so that
  175. * it represents midnight, local time, at the beginning of the day
  176. * specified by the <code>year</code>, <code>month</code>, and
  177. * <code>date</code> arguments.
  178. *
  179. * @param year the year minus 1900.
  180. * @param month the month between 0-11.
  181. * @param date the day of the month between 1-31.
  182. * @see java.util.Calendar
  183. * @deprecated As of JDK version 1.1,
  184. * replaced by <code>Calendar.set(year + 1900, month, date)</code>
  185. * or <code>GregorianCalendar(year + 1900, month, date)</code>.
  186. */
  187. public Date(int year, int month, int date) {
  188. this(year, month, date, 0, 0, 0);
  189. }
  190. /**
  191. * Allocates a <code>Date</code> object and initializes it so that
  192. * it represents the instant at the start of the minute specified by
  193. * the <code>year</code>, <code>month</code>, <code>date</code>,
  194. * <code>hrs</code>, and <code>min</code> arguments, in the local
  195. * time zone.
  196. *
  197. * @param year the year minus 1900.
  198. * @param month the month between 0-11.
  199. * @param date the day of the month between 1-31.
  200. * @param hrs the hours between 0-23.
  201. * @param min the minutes between 0-59.
  202. * @see java.util.Calendar
  203. * @deprecated As of JDK version 1.1,
  204. * replaced by <code>Calendar.set(year + 1900, month, date,
  205. * hrs, min)</code> or <code>GregorianCalendar(year + 1900,
  206. * month, date, hrs, min)</code>.
  207. */
  208. public Date(int year, int month, int date, int hrs, int min) {
  209. this(year, month, date, hrs, min, 0);
  210. }
  211. /**
  212. * Allocates a <code>Date</code> object and initializes it so that
  213. * it represents the instant at the start of the second specified
  214. * by the <code>year</code>, <code>month</code>, <code>date</code>,
  215. * <code>hrs</code>, <code>min</code>, and <code>sec</code> arguments,
  216. * in the local time zone.
  217. *
  218. * @param year the year minus 1900.
  219. * @param month the month between 0-11.
  220. * @param date the day of the month between 1-31.
  221. * @param hrs the hours between 0-23.
  222. * @param min the minutes between 0-59.
  223. * @param sec the seconds between 0-59.
  224. * @see java.util.Calendar
  225. * @deprecated As of JDK version 1.1,
  226. * replaced by <code>Calendar.set(year + 1900, month, date,
  227. * hrs, min, sec)</code> or <code>GregorianCalendar(year + 1900,
  228. * month, date, hrs, min, sec)</code>.
  229. */
  230. public Date(int year, int month, int date, int hrs, int min, int sec) {
  231. cal = null;
  232. if (staticCal == null)
  233. makeStaticCalendars();
  234. synchronized (staticCal) {
  235. staticCal.setTimeZone(TimeZone.getDefault());
  236. staticCal.clear();
  237. staticCal.set(year + 1900, month, date, hrs, min, sec);
  238. fastTime = staticCal.getTimeInMillis();
  239. }
  240. }
  241. /**
  242. * Allocates a <code>Date</code> object and initializes it so that
  243. * it represents the date and time indicated by the string
  244. * <code>s</code>, which is interpreted as if by the
  245. * {@link Date#parse} method.
  246. *
  247. * @param s a string representation of the date.
  248. * @see java.text.DateFormat
  249. * @see java.util.Date#parse(java.lang.String)
  250. * @deprecated As of JDK version 1.1,
  251. * replaced by <code>DateFormat.parse(String s)</code>.
  252. */
  253. public Date(String s) {
  254. this(parse(s));
  255. }
  256. /**
  257. * Return a copy of this object.
  258. */
  259. public Object clone() {
  260. Date d = null;
  261. try {
  262. d = (Date)super.clone();
  263. if (d.cal != null) d.cal = (Calendar)d.cal.clone();
  264. } catch (CloneNotSupportedException e) {} // Won't happen
  265. return d;
  266. }
  267. /**
  268. * Determines the date and time based on the arguments. The
  269. * arguments are interpreted as a year, month, day of the month,
  270. * hour of the day, minute within the hour, and second within the
  271. * minute, exactly as for the <tt>Date</tt> constructor with six
  272. * arguments, except that the arguments are interpreted relative
  273. * to UTC rather than to the local time zone. The time indecated is
  274. * returned represented as the distance, measured in milliseconds,
  275. * of that time from the epoch (00:00:00 GMT on January 1, 1970).
  276. *
  277. * @param year the year minus 1900.
  278. * @param month the month between 0-11.
  279. * @param date the day of the month between 1-31.
  280. * @param hrs the hours between 0-23.
  281. * @param min the minutes between 0-59.
  282. * @param sec the seconds between 0-59.
  283. * @return the number of milliseconds since January 1, 1970, 00:00:00 GMT for
  284. * the date and time specified by the arguments.
  285. * @see java.util.Calendar
  286. * @deprecated As of JDK version 1.1,
  287. * replaced by <code>Calendar.set(year + 1900, month, date,
  288. * hrs, min, sec)</code> or <code>GregorianCalendar(year + 1900,
  289. * month, date, hrs, min, sec)</code>, using a UTC
  290. * <code>TimeZone</code>, followed by <code>Calendar.getTime().getTime()</code>.
  291. */
  292. public static long UTC(int year, int month, int date,
  293. int hrs, int min, int sec) {
  294. if (utcCal == null)
  295. makeStaticCalendars();
  296. synchronized (utcCal) {
  297. utcCal.clear();
  298. utcCal.set(year + 1900, month, date, hrs, min, sec);
  299. return utcCal.getTimeInMillis();
  300. }
  301. }
  302. /**
  303. * Attempts to interpret the string <tt>s</tt> as a representation
  304. * of a date and time. If the attempt is successful, the time
  305. * indicated is returned represented as the distance, measured in
  306. * milliseconds, of that time from the epoch (00:00:00 GMT on
  307. * January 1, 1970). If the attempt fails, an
  308. * <tt>IllegalArgumentException</tt> is thrown.
  309. * <p>
  310. * It accepts many syntaxes; in particular, it recognizes the IETF
  311. * standard date syntax: "Sat, 12 Aug 1995 13:30:00 GMT". It also
  312. * understands the continental U.S. time-zone abbreviations, but for
  313. * general use, a time-zone offset should be used: "Sat, 12 Aug 1995
  314. * 13:30:00 GMT+0430" (4 hours, 30 minutes west of the Greenwich
  315. * meridian). If no time zone is specified, the local time zone is
  316. * assumed. GMT and UTC are considered equivalent.
  317. * <p>
  318. * The string <tt>s</tt> is processed from left to right, looking for
  319. * data of interest. Any material in <tt>s</tt> that is within the
  320. * ASCII parenthesis characters <tt>(</tt> and <tt>)</tt> is ignored.
  321. * Parentheses may be nested. Otherwise, the only characters permitted
  322. * within <tt>s</tt> are these ASCII characters:
  323. * <blockquote><pre>
  324. * abcdefghijklmnopqrstuvwxyz
  325. * ABCDEFGHIJKLMNOPQRSTUVWXYZ
  326. * 0123456789,+-:/</pre></blockquote>
  327. * and whitespace characters.<p>
  328. * A consecutive sequence of decimal digits is treated as a decimal
  329. * number:<ul>
  330. * <li>If a number is preceded by <tt>+</tt> or <tt>-</tt> and a year
  331. * has already been recognized, then the number is a time-zone
  332. * offset. If the number is less than 24, it is an offset measured
  333. * in hours. Otherwise, it is regarded as an offset in minutes,
  334. * expressed in 24-hour time format without punctuation. A
  335. * preceding <tt>-</tt> means a westward offset. Time zone offsets
  336. * are always relative to UTC (Greenwich). Thus, for example,
  337. * <tt>-5</tt> occurring in the string would mean "five hours west
  338. * of Greenwich" and <tt>+0430</tt> would mean "four hours and
  339. * thirty minutes east of Greenwich." It is permitted for the
  340. * string to specify <tt>GMT</tt>, <tt>UT</tt>, or <tt>UTC</tt>
  341. * redundantly-for example, <tt>GMT-5</tt> or <tt>utc+0430</tt>.
  342. * <li>The number is regarded as a year number if one of the
  343. * following conditions is true:
  344. * <ul>
  345. * <li>The number is equal to or greater than 70 and followed by a
  346. * space, comma, slash, or end of string
  347. * <li>The number is less than 70, and both a month and a day of
  348. * the month have already been recognized</li>
  349. * </ul>
  350. * If the recognized year number is less than 100, it is
  351. * interpreted as an abbreviated year relative to a century of
  352. * which dates are within 80 years before and 19 years after
  353. * the time when the Date class is initialized.
  354. * After adjusting the year number, 1900 is subtracted from
  355. * it. For example, if the current year is 1999 then years in
  356. * the range 19 to 99 are assumed to mean 1919 to 1999, while
  357. * years from 0 to 18 are assumed to mean 2000 to 2018. Note
  358. * that this is slightly different from the interpretation of
  359. * years less than 100 that is used in {@link java.text.SimpleDateFormat}.
  360. * <li>If the number is followed by a colon, it is regarded as an hour,
  361. * unless an hour has already been recognized, in which case it is
  362. * regarded as a minute.
  363. * <li>If the number is followed by a slash, it is regarded as a month
  364. * (it is decreased by 1 to produce a number in the range <tt>0</tt>
  365. * to <tt>11</tt>), unless a month has already been recognized, in
  366. * which case it is regarded as a day of the month.
  367. * <li>If the number is followed by whitespace, a comma, a hyphen, or
  368. * end of string, then if an hour has been recognized but not a
  369. * minute, it is regarded as a minute; otherwise, if a minute has
  370. * been recognized but not a second, it is regarded as a second;
  371. * otherwise, it is regarded as a day of the month. </ul><p>
  372. * A consecutive sequence of letters is regarded as a word and treated
  373. * as follows:<ul>
  374. * <li>A word that matches <tt>AM</tt>, ignoring case, is ignored (but
  375. * the parse fails if an hour has not been recognized or is less
  376. * than <tt>1</tt> or greater than <tt>12</tt>).
  377. * <li>A word that matches <tt>PM</tt>, ignoring case, adds <tt>12</tt>
  378. * to the hour (but the parse fails if an hour has not been
  379. * recognized or is less than <tt>1</tt> or greater than <tt>12</tt>).
  380. * <li>Any word that matches any prefix of <tt>SUNDAY, MONDAY, TUESDAY,
  381. * WEDNESDAY, THURSDAY, FRIDAY</tt>, or <tt>SATURDAY</tt>, ignoring
  382. * case, is ignored. For example, <tt>sat, Friday, TUE</tt>, and
  383. * <tt>Thurs</tt> are ignored.
  384. * <li>Otherwise, any word that matches any prefix of <tt>JANUARY,
  385. * FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER,
  386. * OCTOBER, NOVEMBER</tt>, or <tt>DECEMBER</tt>, ignoring case, and
  387. * considering them in the order given here, is recognized as
  388. * specifying a month and is converted to a number (<tt>0</tt> to
  389. * <tt>11</tt>). For example, <tt>aug, Sept, april</tt>, and
  390. * <tt>NOV</tt> are recognized as months. So is <tt>Ma</tt>, which
  391. * is recognized as <tt>MARCH</tt>, not <tt>MAY</tt>.
  392. * <li>Any word that matches <tt>GMT, UT</tt>, or <tt>UTC</tt>, ignoring
  393. * case, is treated as referring to UTC.
  394. * <li>Any word that matches <tt>EST, CST, MST</tt>, or <tt>PST</tt>,
  395. * ignoring case, is recognized as referring to the time zone in
  396. * North America that is five, six, seven, or eight hours west of
  397. * Greenwich, respectively. Any word that matches <tt>EDT, CDT,
  398. * MDT</tt>, or <tt>PDT</tt>, ignoring case, is recognized as
  399. * referring to the same time zone, respectively, during daylight
  400. * saving time.</ul><p>
  401. * Once the entire string s has been scanned, it is converted to a time
  402. * result in one of two ways. If a time zone or time-zone offset has been
  403. * recognized, then the year, month, day of month, hour, minute, and
  404. * second are interpreted in UTC and then the time-zone offset is
  405. * applied. Otherwise, the year, month, day of month, hour, minute, and
  406. * second are interpreted in the local time zone.
  407. *
  408. * @param s a string to be parsed as a date.
  409. * @return the number of milliseconds since January 1, 1970, 00:00:00 GMT
  410. * represented by the string argument.
  411. * @see java.text.DateFormat
  412. * @deprecated As of JDK version 1.1,
  413. * replaced by <code>DateFormat.parse(String s)</code>.
  414. */
  415. public static long parse(String s) {
  416. if (staticCal == null)
  417. makeStaticCalendars(); // Called only for side-effect of setting defaultCenturyStart
  418. int year = Integer.MIN_VALUE;
  419. int mon = -1;
  420. int mday = -1;
  421. int hour = -1;
  422. int min = -1;
  423. int sec = -1;
  424. int millis = -1;
  425. int c = -1;
  426. int i = 0;
  427. int n = -1;
  428. int wst = -1;
  429. int tzoffset = -1;
  430. int prevc = 0;
  431. syntax:
  432. {
  433. if (s == null)
  434. break syntax;
  435. int limit = s.length();
  436. while (i < limit) {
  437. c = s.charAt(i);
  438. i++;
  439. if (c <= ' ' || c == ',')
  440. continue;
  441. if (c == '(') { // skip comments
  442. int depth = 1;
  443. while (i < limit) {
  444. c = s.charAt(i);
  445. i++;
  446. if (c == '(') depth++;
  447. else if (c == ')')
  448. if (--depth <= 0)
  449. break;
  450. }
  451. continue;
  452. }
  453. if ('0' <= c && c <= '9') {
  454. n = c - '0';
  455. while (i < limit && '0' <= (c = s.charAt(i)) && c <= '9') {
  456. n = n * 10 + c - '0';
  457. i++;
  458. }
  459. if (prevc == '+' || prevc == '-' && year != Integer.MIN_VALUE) {
  460. // timezone offset
  461. if (n < 24)
  462. n = n * 60; // EG. "GMT-3"
  463. else
  464. n = n % 100 + n / 100 * 60; // eg "GMT-0430"
  465. if (prevc == '+') // plus means east of GMT
  466. n = -n;
  467. if (tzoffset != 0 && tzoffset != -1)
  468. break syntax;
  469. tzoffset = n;
  470. } else if (n >= 70)
  471. if (year != Integer.MIN_VALUE)
  472. break syntax;
  473. else if (c <= ' ' || c == ',' || c == '/' || i >= limit)
  474. // year = n < 1900 ? n : n - 1900;
  475. year = n;
  476. else
  477. break syntax;
  478. else if (c == ':')
  479. if (hour < 0)
  480. hour = (byte) n;
  481. else if (min < 0)
  482. min = (byte) n;
  483. else
  484. break syntax;
  485. else if (c == '/')
  486. if (mon < 0)
  487. mon = (byte) (n - 1);
  488. else if (mday < 0)
  489. mday = (byte) n;
  490. else
  491. break syntax;
  492. else if (i < limit && c != ',' && c > ' ' && c != '-')
  493. break syntax;
  494. else if (hour >= 0 && min < 0)
  495. min = (byte) n;
  496. else if (min >= 0 && sec < 0)
  497. sec = (byte) n;
  498. else if (mday < 0)
  499. mday = (byte) n;
  500. // Handle two-digit years < 70 (70-99 handled above).
  501. else if (year == Integer.MIN_VALUE && mon >= 0 && mday >= 0)
  502. year = n;
  503. else
  504. break syntax;
  505. prevc = 0;
  506. } else if (c == '/' || c == ':' || c == '+' || c == '-')
  507. prevc = c;
  508. else {
  509. int st = i - 1;
  510. while (i < limit) {
  511. c = s.charAt(i);
  512. if (!('A' <= c && c <= 'Z' || 'a' <= c && c <= 'z'))
  513. break;
  514. i++;
  515. }
  516. if (i <= st + 1)
  517. break syntax;
  518. int k;
  519. for (k = wtb.length; --k >= 0;)
  520. if (wtb[k].regionMatches(true, 0, s, st, i - st)) {
  521. int action = ttb[k];
  522. if (action != 0) {
  523. if (action == 1) { // pm
  524. if (hour > 12 || hour < 1)
  525. break syntax;
  526. else if (hour < 12)
  527. hour += 12;
  528. } else if (action == 14) { // am
  529. if (hour > 12 || hour < 1)
  530. break syntax;
  531. else if (hour == 12)
  532. hour = 0;
  533. } else if (action <= 13) { // month!
  534. if (mon < 0)
  535. mon = (byte) (action - 2);
  536. else
  537. break syntax;
  538. } else {
  539. tzoffset = action - 10000;
  540. }
  541. }
  542. break;
  543. }
  544. if (k < 0)
  545. break syntax;
  546. prevc = 0;
  547. }
  548. }
  549. if (year == Integer.MIN_VALUE || mon < 0 || mday < 0)
  550. break syntax;
  551. // Parse 2-digit years within the correct default century.
  552. if (year < 100) {
  553. year += (defaultCenturyStart / 100) * 100;
  554. if (year < defaultCenturyStart) year += 100;
  555. }
  556. year -= 1900;
  557. if (sec < 0)
  558. sec = 0;
  559. if (min < 0)
  560. min = 0;
  561. if (hour < 0)
  562. hour = 0;
  563. if (tzoffset == -1) // no time zone specified, have to use local
  564. return new Date (year, mon, mday, hour, min, sec).getTime();
  565. return UTC(year, mon, mday, hour, min, sec) + tzoffset * (60 * 1000);
  566. }
  567. // syntax error
  568. throw new IllegalArgumentException();
  569. }
  570. private final static String wtb[] = {
  571. "am", "pm",
  572. "monday", "tuesday", "wednesday", "thursday", "friday",
  573. "saturday", "sunday",
  574. "january", "february", "march", "april", "may", "june",
  575. "july", "august", "september", "october", "november", "december",
  576. "gmt", "ut", "utc", "est", "edt", "cst", "cdt",
  577. "mst", "mdt", "pst", "pdt"
  578. // this time zone table needs to be expanded
  579. };
  580. private final static int ttb[] = {
  581. 14, 1, 0, 0, 0, 0, 0, 0, 0,
  582. 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
  583. 10000 + 0, 10000 + 0, 10000 + 0, // GMT/UT/UTC
  584. 10000 + 5 * 60, 10000 + 4 * 60, // EST/EDT
  585. 10000 + 6 * 60, 10000 + 5 * 60,
  586. 10000 + 7 * 60, 10000 + 6 * 60,
  587. 10000 + 8 * 60, 10000 + 7 * 60
  588. };
  589. /**
  590. * Returns a value that is the result of subtracting 1900 from the
  591. * year that contains or begins with the instant in time represented
  592. * by this <code>Date</code> object, as interpreted in the local
  593. * time zone.
  594. *
  595. * @return the year represented by this date, minus 1900.
  596. * @see java.util.Calendar
  597. * @deprecated As of JDK version 1.1,
  598. * replaced by <code>Calendar.get(Calendar.YEAR) - 1900</code>.
  599. */
  600. public int getYear() {
  601. return getField(Calendar.YEAR) - 1900;
  602. }
  603. /**
  604. * Sets the year of this <tt>Date</tt> object to be the specified
  605. * value plus 1900. This <code>Date</code> object is modified so
  606. * that it represents a point in time within the specified year,
  607. * with the month, date, hour, minute, and second the same as
  608. * before, as interpreted in the local time zone. (Of course, if
  609. * the date was February 29, for example, and the year is set to a
  610. * non-leap year, then the new date will be treated as if it were
  611. * on March 1.)
  612. *
  613. * @param year the year value.
  614. * @see java.util.Calendar
  615. * @deprecated As of JDK version 1.1,
  616. * replaced by <code>Calendar.set(Calendar.YEAR, year + 1900)</code>.
  617. */
  618. public void setYear(int year) {
  619. setField(Calendar.YEAR, year + 1900);
  620. }
  621. /**
  622. * Returns a number representing the month that contains or begins
  623. * with the instant in time represented by this <tt>Date</tt> object.
  624. * The value returned is between <code>0</code> and <code>11</code>,
  625. * with the value <code>0</code> representing January.
  626. *
  627. * @return the month represented by this date.
  628. * @see java.util.Calendar
  629. * @deprecated As of JDK version 1.1,
  630. * replaced by <code>Calendar.get(Calendar.MONTH)</code>.
  631. */
  632. public int getMonth() {
  633. return getField(Calendar.MONTH);
  634. }
  635. /**
  636. * Sets the month of this date to the specified value. This
  637. * <tt>Date</tt> object is modified so that it represents a point
  638. * in time within the specified month, with the year, date, hour,
  639. * minute, and second the same as before, as interpreted in the
  640. * local time zone. If the date was October 31, for example, and
  641. * the month is set to June, then the new date will be treated as
  642. * if it were on July 1, because June has only 30 days.
  643. *
  644. * @param month the month value between 0-11.
  645. * @see java.util.Calendar
  646. * @deprecated As of JDK version 1.1,
  647. * replaced by <code>Calendar.set(Calendar.MONTH, int month)</code>.
  648. */
  649. public void setMonth(int month) {
  650. setField(Calendar.MONTH, month);
  651. }
  652. /**
  653. * Returns the day of the month represented by this <tt>Date</tt> object.
  654. * The value returned is between <code>1</code> and <code>31</code>
  655. * representing the day of the month that contains or begins with the
  656. * instant in time represented by this <tt>Date</tt> object, as
  657. * interpreted in the local time zone.
  658. *
  659. * @return the day of the month represented by this date.
  660. * @see java.util.Calendar
  661. * @deprecated As of JDK version 1.1,
  662. * replaced by <code>Calendar.get(Calendar.DAY_OF_MONTH)</code>.
  663. * @deprecated
  664. */
  665. public int getDate() {
  666. return getField(Calendar.DATE);
  667. }
  668. /**
  669. * Sets the day of the month of this <tt>Date</tt> object to the
  670. * specified value. This <tt>Date</tt> object is modified so that
  671. * it represents a point in time within the specified day of the
  672. * month, with the year, month, hour, minute, and second the same
  673. * as before, as interpreted in the local time zone. If the date
  674. * was April 30, for example, and the date is set to 31, then it
  675. * will be treated as if it were on May 1, because April has only
  676. * 30 days.
  677. *
  678. * @param date the day of the month value between 1-31.
  679. * @see java.util.Calendar
  680. * @deprecated As of JDK version 1.1,
  681. * replaced by <code>Calendar.set(Calendar.DAY_OF_MONTH, int date)</code>.
  682. */
  683. public void setDate(int date) {
  684. setField(Calendar.DATE, date);
  685. }
  686. /**
  687. * Returns the day of the week represented by this date. The
  688. * returned value (<tt>0</tt> = Sunday, <tt>1</tt> = Monday,
  689. * <tt>2</tt> = Tuesday, <tt>3</tt> = Wednesday, <tt>4</tt> =
  690. * Thursday, <tt>5</tt> = Friday, <tt>6</tt> = Saturday)
  691. * represents the day of the week that contains or begins with
  692. * the instant in time represented by this <tt>Date</tt> object,
  693. * as interpreted in the local time zone.
  694. *
  695. * @return the day of the week represented by this date.
  696. * @see java.util.Calendar
  697. * @deprecated As of JDK version 1.1,
  698. * replaced by <code>Calendar.get(Calendar.DAY_OF_WEEK)</code>.
  699. */
  700. public int getDay() {
  701. return getField(Calendar.DAY_OF_WEEK) - Calendar.SUNDAY;
  702. }
  703. /**
  704. * Returns the hour represented by this <tt>Date</tt> object. The
  705. * returned value is a number (<tt>0</tt> through <tt>23</tt>)
  706. * representing the hour within the day that contains or begins
  707. * with the instant in time represented by this <tt>Date</tt>
  708. * object, as interpreted in the local time zone.
  709. *
  710. * @return the hour represented by this date.
  711. * @see java.util.Calendar
  712. * @deprecated As of JDK version 1.1,
  713. * replaced by <code>Calendar.get(Calendar.HOUR_OF_DAY)</code>.
  714. */
  715. public int getHours() {
  716. return getField(Calendar.HOUR_OF_DAY);
  717. }
  718. /**
  719. * Sets the hour of this <tt>Date</tt> object to the specified value.
  720. * This <tt>Date</tt> object is modified so that it represents a point
  721. * in time within the specified hour of the day, with the year, month,
  722. * date, minute, and second the same as before, as interpreted in the
  723. * local time zone.
  724. *
  725. * @param hours the hour value.
  726. * @see java.util.Calendar
  727. * @deprecated As of JDK version 1.1,
  728. * replaced by <code>Calendar.set(Calendar.HOUR_OF_DAY, int hours)</code>.
  729. */
  730. public void setHours(int hours) {
  731. setField(Calendar.HOUR_OF_DAY, hours);
  732. }
  733. /**
  734. * Returns the number of minutes past the hour represented by this date,
  735. * as interpreted in the local time zone.
  736. * The value returned is between <code>0</code> and <code>59</code>.
  737. *
  738. * @return the number of minutes past the hour represented by this date.
  739. * @see java.util.Calendar
  740. * @deprecated As of JDK version 1.1,
  741. * replaced by <code>Calendar.get(Calendar.MINUTE)</code>.
  742. */
  743. public int getMinutes() {
  744. return getField(Calendar.MINUTE);
  745. }
  746. /**
  747. * Sets the minutes of this <tt>Date</tt> object to the specified value.
  748. * This <tt>Date</tt> object is modified so that it represents a point
  749. * in time within the specified minute of the hour, with the year, month,
  750. * date, hour, and second the same as before, as interpreted in the
  751. * local time zone.
  752. *
  753. * @param minutes the value of the minutes.
  754. * @see java.util.Calendar
  755. * @deprecated As of JDK version 1.1,
  756. * replaced by <code>Calendar.set(Calendar.MINUTE, int minutes)</code>.
  757. */
  758. public void setMinutes(int minutes) {
  759. setField(Calendar.MINUTE, minutes);
  760. }
  761. /**
  762. * Returns the number of seconds past the minute represented by this date.
  763. * The value returned is between <code>0</code> and <code>61</code>. The
  764. * values <code>60</code> and <code>61</code> can only occur on those
  765. * Java Virtual Machines that take leap seconds into account.
  766. *
  767. * @return the number of seconds past the minute represented by this date.
  768. * @see java.util.Calendar
  769. * @deprecated As of JDK version 1.1,
  770. * replaced by <code>Calendar.get(Calendar.SECOND)</code>.
  771. */
  772. public int getSeconds() {
  773. return getField(Calendar.SECOND);
  774. }
  775. /**
  776. * Sets the seconds of this <tt>Date</tt> to the specified value.
  777. * This <tt>Date</tt> object is modified so that it represents a
  778. * point in time within the specified second of the minute, with
  779. * the year, month, date, hour, and minute the same as before, as
  780. * interpreted in the local time zone.
  781. *
  782. * @param seconds the seconds value.
  783. * @see java.util.Calendar
  784. * @deprecated As of JDK version 1.1,
  785. * replaced by <code>Calendar.set(Calendar.SECOND, int seconds)</code>.
  786. */
  787. public void setSeconds(int seconds) {
  788. setField(Calendar.SECOND, seconds);
  789. }
  790. /**
  791. * Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT
  792. * represented by this <tt>Date</tt> object.
  793. *
  794. * @return the number of milliseconds since January 1, 1970, 00:00:00 GMT
  795. * represented by this date.
  796. */
  797. public long getTime() {
  798. return (cal == null) ? fastTime : cal.getTimeInMillis();
  799. }
  800. /**
  801. * Sets this <tt>Date</tt> object to represent a point in time that is
  802. * <tt>time</tt> milliseconds after January 1, 1970 00:00:00 GMT.
  803. *
  804. * @param time the number of milliseconds.
  805. */
  806. public void setTime(long time) {
  807. if (cal == null) {
  808. fastTime = time;
  809. }
  810. else {
  811. cal.setTimeInMillis(time);
  812. }
  813. }
  814. /**
  815. * Tests if this date is before the specified date.
  816. *
  817. * @param when a date.
  818. * @return <code>true</code> if and only if the instant of time
  819. * represented by this <tt>Date</tt> object is strictly
  820. * earlier than the instant represented by <tt>when</tt>
  821. * <code>false</code> otherwise.
  822. */
  823. public boolean before(Date when) {
  824. return getTime() < when.getTime();
  825. }
  826. /**
  827. * Tests if this date is after the specified date.
  828. *
  829. * @param when a date.
  830. * @return <code>true</code> if and only if the instant represented
  831. * by this <tt>Date</tt> object is strictly later than the
  832. * instant represented by <tt>when</tt>
  833. * <code>false</code> otherwise.
  834. */
  835. public boolean after(Date when) {
  836. return getTime() > when.getTime();
  837. }
  838. /**
  839. * Compares two dates for equality.
  840. * The result is <code>true</code> if and only if the argument is
  841. * not <code>null</code> and is a <code>Date</code> object that
  842. * represents the same point in time, to the millisecond, as this object.
  843. * <p>
  844. * Thus, two <code>Date</code> objects are equal if and only if the
  845. * <code>getTime</code> method returns the same <code>long</code>
  846. * value for both.
  847. *
  848. * @param obj the object to compare with.
  849. * @return <code>true</code> if the objects are the same;
  850. * <code>false</code> otherwise.
  851. * @see java.util.Date#getTime()
  852. */
  853. public boolean equals(Object obj) {
  854. return obj instanceof Date && getTime() == ((Date) obj).getTime();
  855. }
  856. /**
  857. * Compares two Dates for ordering.
  858. *
  859. * @param anotherDate the <code>Date</code> to be compared.
  860. * @return the value <code>0</code> if the argument Date is equal to
  861. * this Date; a value less than <code>0</code> if this Date
  862. * is before the Date argument; and a value greater than
  863. * <code>0</code> if this Date is after the Date argument.
  864. * @since 1.2
  865. */
  866. public int compareTo(Date anotherDate) {
  867. long thisTime = this.getTime();
  868. long anotherTime = anotherDate.getTime();
  869. return (thisTime<anotherTime ? -1 : (thisTime==anotherTime ? 0 : 1));
  870. }
  871. /**
  872. * Compares this Date to another Object. If the Object is a Date,
  873. * this function behaves like <code>compareTo(Date)</code>. Otherwise,
  874. * it throws a <code>ClassCastException</code> (as Dates are comparable
  875. * only to other Dates).
  876. *
  877. * @param o the <code>Object</code> to be compared.
  878. * @return the value <code>0</code> if the argument is a Date
  879. * equal to this Date; a value less than <code>0</code> if the
  880. * argument is a Date after this Date; and a value greater than
  881. * <code>0</code> if the argument is a Date before this Date.
  882. * @exception ClassCastException if the argument is not a
  883. * <code>Date</code>.
  884. * @see java.lang.Comparable
  885. * @since 1.2
  886. */
  887. public int compareTo(Object o) {
  888. return compareTo((Date)o);
  889. }
  890. /**
  891. * Returns a hash code value for this object. The result is the
  892. * exclusive OR of the two halves of the primitive <tt>long</tt>
  893. * value returned by the {@link Date#getTime}
  894. * method. That is, the hash code is the value of the expression:
  895. * <blockquote><pre>
  896. * (int)(this.getTime()^(this.getTime() >>> 32))</pre></blockquote>
  897. *
  898. * @return a hash code value for this object.
  899. */
  900. public int hashCode() {
  901. long ht = getTime();
  902. return (int) ht ^ (int) (ht >> 32);
  903. }
  904. /**
  905. * Converts this <code>Date</code> object to a <code>String</code>
  906. * of the form:
  907. * <blockquote><pre>
  908. * dow mon dd hh:mm:ss zzz yyyy</pre></blockquote>
  909. * where:<ul>
  910. * <li><tt>dow</tt> is the day of the week (<tt>Sun, Mon, Tue, Wed,
  911. * Thu, Fri, Sat</tt>).
  912. * <li><tt>mon</tt> is the month (<tt>Jan, Feb, Mar, Apr, May, Jun,
  913. * Jul, Aug, Sep, Oct, Nov, Dec</tt>).
  914. * <li><tt>dd</tt> is the day of the month (<tt>01</tt> through
  915. * <tt>31</tt>), as two decimal digits.
  916. * <li><tt>hh</tt> is the hour of the day (<tt>00</tt> through
  917. * <tt>23</tt>), as two decimal digits.
  918. * <li><tt>mm</tt> is the minute within the hour (<tt>00</tt> through
  919. * <tt>59</tt>), as two decimal digits.
  920. * <li><tt>ss</tt> is the second within the minute (<tt>00</tt> through
  921. * <tt>61</tt>, as two decimal digits.
  922. * <li><tt>zzz</tt> is the time zone (and may reflect daylight savings
  923. * time). Standard time zone abbreviations include those
  924. * recognized by the method <tt>parse</tt>. If time zone
  925. * informationi is not available, then <tt>zzz</tt> is empty -
  926. * that is, it consists of no characters at all.
  927. * <li><tt>yyyy</tt> is the year, as four decimal digits.
  928. * </ul>
  929. *
  930. * @return a string representation of this date.
  931. * @see java.util.Date#toLocaleString()
  932. * @see java.util.Date#toGMTString()
  933. */
  934. public String toString() {
  935. DateFormat formatter = null;
  936. if (simpleFormatter != null) {
  937. formatter = (DateFormat)simpleFormatter.get();
  938. }
  939. if (formatter == null) {
  940. /* No cache yet, or cached formatter GC'd */
  941. formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy",
  942. Locale.US);
  943. simpleFormatter = new SoftReference(formatter);
  944. }
  945. synchronized (formatter) {
  946. formatter.setTimeZone(TimeZone.getDefault());
  947. return formatter.format(this);
  948. }
  949. }
  950. /**
  951. * Creates a string representation of this <tt>Date</tt> object in an
  952. * implementation-dependent form. The intent is that the form should
  953. * be familiar to the user of the Java application, wherever it may
  954. * happen to be running. The intent is comparable to that of the
  955. * "<code>%c</code>" format supported by the <code>strftime()</code>
  956. * function of ISO C.
  957. *
  958. * @return a string representation of this date, using the locale
  959. * conventions.
  960. * @see java.text.DateFormat
  961. * @see java.util.Date#toString()
  962. * @see java.util.Date#toGMTString()
  963. * @deprecated As of JDK version 1.1,
  964. * replaced by <code>DateFormat.format(Date date)</code>.
  965. */
  966. public String toLocaleString() {
  967. DateFormat formatter = DateFormat.getDateTimeInstance();
  968. return formatter.format(this);
  969. }
  970. /**
  971. * Creates a string representation of this <tt>Date</tt> object of
  972. * the form:
  973. * <blockquote<pre>
  974. * d mon yyyy hh:mm:ss GMT</pre></blockquote>
  975. * where:<ul>
  976. * <li><i>d</i> is the day of the month (<tt>1</tt> through <tt>31</tt>),
  977. * as one or two decimal digits.
  978. * <li><i>mon</i> is the month (<tt>Jan, Feb, Mar, Apr, May, Jun, Jul,
  979. * Aug, Sep, Oct, Nov, Dec</tt>).
  980. * <li><i>yyyy</i> i sthe year, as four decimal digits.
  981. * <li><i>hh</i> is the hour of the day (<tt>00</tt> through <tt>23</tt>),
  982. * as two decimal digits.
  983. * <li><i>mm</i> is the minute within the hour (<tt>00</tt> through
  984. * <tt>59</tt>), as two decimal digits.
  985. * <li><i>ss</i> is the second within the minute (<tt>00</tt> through
  986. * <tt>61</tt>), as two decimal digits.
  987. * <li><i>GMT</i> is exactly the ASCII letters "<tt>GMT</tt>" to indicate
  988. * Greenwich Mean Time.
  989. * </ul><p>
  990. * The result does not depend on the local time zone.
  991. *
  992. * @return a string representation of this date, using the Internet GMT
  993. * conventions.
  994. * @see java.text.DateFormat
  995. * @see java.util.Date#toString()
  996. * @see java.util.Date#toLocaleString()
  997. * @deprecated As of JDK version 1.1,
  998. * replaced by <code>DateFormat.format(Date date)</code>, using a
  999. * GMT <code>TimeZone</code>.
  1000. */
  1001. public String toGMTString() {
  1002. DateFormat formatter = null;
  1003. if (gmtFormatter != null) {
  1004. formatter = (DateFormat)gmtFormatter.get();
  1005. }
  1006. if (formatter == null) {
  1007. /* No cache yet, or cached formatter GC'd */
  1008. formatter = new SimpleDateFormat("d MMM yyyy HH:mm:ss 'GMT'",
  1009. Locale.US);
  1010. formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
  1011. gmtFormatter = new SoftReference(formatter);
  1012. }
  1013. return formatter.format(this);
  1014. }
  1015. /**
  1016. * Returns the offset, measured in minutes, for the local time zone
  1017. * relative to UTC that is appropriate for the time represented by
  1018. * this <tt>Date</tt> object.
  1019. * <p>
  1020. * For example, in Massachusetts, five time zones west of Greenwich:
  1021. * <blockquote><pre>
  1022. * new Date(96, 1, 14).getTimezoneOffset() returns 300</pre></blockquote>
  1023. * because on February 14, 1996, standard time (Eastern Standard Time)
  1024. * is in use, which is offset five hours from UTC; but:
  1025. * <blockquote><pre>
  1026. * new Date(96, 5, 1).getTimezoneOffset() returns 240</pre></blockquote>
  1027. * because on May 1, 1996, daylight savings time (Eastern Daylight Time)
  1028. * is in use, which is offset only four hours from UTC.<p>
  1029. * This method produces teh same result as if it computed:
  1030. * <blockquote><pre>
  1031. * (this.getTime() - UTC(this.getYear(),
  1032. * this.getMonth(),
  1033. * this.getDate(),
  1034. * this.getHours(),
  1035. * this.getMinutes(),
  1036. * this.getSeconds())) / (60 * 1000)
  1037. * </pre></blockquote>
  1038. *
  1039. * @return the time-zone offset, in minutes, for the current locale.
  1040. * @see java.util.Calendar
  1041. * @see java.util.TimeZone
  1042. * @deprecated As of JDK version 1.1,
  1043. * replaced by <code>Calendar.get(Calendar.ZONE_OFFSET) +
  1044. * Calendar.get(Calendar.DST_OFFSET)</code>.
  1045. */
  1046. public int getTimezoneOffset() {
  1047. int offset;
  1048. if (cal == null) {
  1049. if (staticCal == null)
  1050. makeStaticCalendars();
  1051. synchronized (staticCal) {
  1052. staticCal.setTimeZone(TimeZone.getDefault());
  1053. staticCal.setTimeInMillis(getTime());
  1054. offset = staticCal.get(Calendar.ZONE_OFFSET) +
  1055. staticCal.get(Calendar.DST_OFFSET);
  1056. }
  1057. }
  1058. else {
  1059. TimeZone defaultZone = TimeZone.getDefault();
  1060. if (!defaultZone.equals(cal.getTimeZone())) {
  1061. long ms = cal.getTimeInMillis();
  1062. cal.setTimeZone(TimeZone.getDefault());
  1063. cal.setTimeInMillis(ms);
  1064. }
  1065. offset = cal.get(Calendar.ZONE_OFFSET) +
  1066. cal.get(Calendar.DST_OFFSET);
  1067. }
  1068. return -(offset / 1000 / 60); // convert to minutes
  1069. }
  1070. /**
  1071. * Save the state of this object to a stream (i.e., serialize it).
  1072. *
  1073. * @serialData The value returned by <code>getTime()</code>
  1074. * is emitted (long). This represents the offset from
  1075. * January 1, 1970, 00:00:00 GMT in milliseconds.
  1076. */
  1077. private void writeObject(ObjectOutputStream s)
  1078. throws IOException
  1079. {
  1080. s.writeLong(getTime());
  1081. }
  1082. /**
  1083. * Reconstitute this object from a stream (i.e., deserialize it).
  1084. */
  1085. private void readObject(ObjectInputStream s)
  1086. throws IOException, ClassNotFoundException
  1087. {
  1088. fastTime = s.readLong();
  1089. // we expect to have cal == null here
  1090. }
  1091. /**
  1092. * Return a field for this date by looking it up in a Calendar object.
  1093. *
  1094. * @return the field value
  1095. * @see java.util.Calendar
  1096. * @param field the field to return
  1097. */
  1098. private final int getField(int field) {
  1099. if (cal == null) {
  1100. if (staticCal == null)
  1101. makeStaticCalendars();
  1102. synchronized (staticCal) {
  1103. staticCal.setTimeZone(TimeZone.getDefault());
  1104. staticCal.setTimeInMillis(fastTime);
  1105. return staticCal.get(field);
  1106. }
  1107. }
  1108. else {
  1109. TimeZone defaultZone = TimeZone.getDefault();
  1110. if (!defaultZone.equals(cal.getTimeZone())) {
  1111. long ms = cal.getTimeInMillis();
  1112. cal.setTimeZone(TimeZone.getDefault());
  1113. cal.setTimeInMillis(ms);
  1114. }
  1115. return cal.get(field);
  1116. }
  1117. }
  1118. /**
  1119. * Set a field for this day.
  1120. *
  1121. * @param field the field to set
  1122. * @param value the value to set it to
  1123. * @see java.util.Calendar
  1124. */
  1125. private final void setField(int field, int value) {
  1126. if (cal == null) {
  1127. cal = new GregorianCalendar();
  1128. cal.setTimeInMillis(fastTime);
  1129. }
  1130. cal.set(field, value);
  1131. }
  1132. private synchronized static void makeStaticCalendars() {
  1133. if (staticCal == null) {
  1134. GregorianCalendar calendar = new GregorianCalendar();
  1135. utcCal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
  1136. defaultCenturyStart = calendar.get(Calendar.YEAR) - 80;
  1137. staticCal = calendar;
  1138. }
  1139. }
  1140. }