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