1. /*
  2. * @(#)Date.java 1.80 04/05/18
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.util;
  8. import java.text.DateFormat;
  9. import java.io.IOException;
  10. import java.io.ObjectOutputStream;
  11. import java.io.ObjectInputStream;
  12. import java.lang.ref.SoftReference;
  13. import sun.util.calendar.BaseCalendar;
  14. import sun.util.calendar.CalendarDate;
  15. import sun.util.calendar.CalendarSystem;
  16. import sun.util.calendar.CalendarUtils;
  17. import sun.util.calendar.Era;
  18. import sun.util.calendar.Gregorian;
  19. import sun.util.calendar.ZoneInfo;
  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 from 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.80, 05/18/04
  104. * @see java.text.DateFormat
  105. * @see java.util.Calendar
  106. * @see java.util.TimeZone
  107. * @since JDK1.0
  108. */
  109. public class Date
  110. implements java.io.Serializable, Cloneable, Comparable<Date>
  111. {
  112. private static final BaseCalendar gcal =
  113. CalendarSystem.getGregorianCalendar();
  114. private static BaseCalendar jcal;
  115. private transient long fastTime;
  116. /*
  117. * If cdate is null, then fastTime indicates the time in millis.
  118. * Otherwise, fastTime is ignored, and cdate indicates the time.
  119. * The cdate object is only created if a setXxx call is made to
  120. * set a field.
  121. */
  122. private transient BaseCalendar.Date cdate;
  123. // Initialized just before the value is used. See parse().
  124. private static int defaultCenturyStart;
  125. /* use serialVersionUID from modified java.util.Date for
  126. * interoperability with JDK1.1. The Date was modified to write
  127. * and read only the UTC time.
  128. */
  129. private static final long serialVersionUID = 7523967970034938905L;
  130. /**
  131. * Allocates a <code>Date</code> object and initializes it so that
  132. * it represents the time at which it was allocated, measured to the
  133. * nearest millisecond.
  134. *
  135. * @see java.lang.System#currentTimeMillis()
  136. */
  137. public Date() {
  138. this(System.currentTimeMillis());
  139. }
  140. /**
  141. * Allocates a <code>Date</code> object and initializes it to
  142. * represent the specified number of milliseconds since the
  143. * standard base time known as "the epoch", namely January 1,
  144. * 1970, 00:00:00 GMT.
  145. *
  146. * @param date the milliseconds since January 1, 1970, 00:00:00 GMT.
  147. * @see java.lang.System#currentTimeMillis()
  148. */
  149. public Date(long date) {
  150. fastTime = date;
  151. }
  152. /**
  153. * Allocates a <code>Date</code> object and initializes it so that
  154. * it represents midnight, local time, at the beginning of the day
  155. * specified by the <code>year</code>, <code>month</code>, and
  156. * <code>date</code> arguments.
  157. *
  158. * @param year the year minus 1900.
  159. * @param month the month between 0-11.
  160. * @param date the day of the month between 1-31.
  161. * @see java.util.Calendar
  162. * @deprecated As of JDK version 1.1,
  163. * replaced by <code>Calendar.set(year + 1900, month, date)</code>
  164. * or <code>GregorianCalendar(year + 1900, month, date)</code>.
  165. */
  166. @Deprecated
  167. public Date(int year, int month, int date) {
  168. this(year, month, date, 0, 0, 0);
  169. }
  170. /**
  171. * Allocates a <code>Date</code> object and initializes it so that
  172. * it represents the instant at the start of the minute specified by
  173. * the <code>year</code>, <code>month</code>, <code>date</code>,
  174. * <code>hrs</code>, and <code>min</code> arguments, in the local
  175. * time zone.
  176. *
  177. * @param year the year minus 1900.
  178. * @param month the month between 0-11.
  179. * @param date the day of the month between 1-31.
  180. * @param hrs the hours between 0-23.
  181. * @param min the minutes between 0-59.
  182. * @see java.util.Calendar
  183. * @deprecated As of JDK version 1.1,
  184. * replaced by <code>Calendar.set(year + 1900, month, date,
  185. * hrs, min)</code> or <code>GregorianCalendar(year + 1900,
  186. * month, date, hrs, min)</code>.
  187. */
  188. @Deprecated
  189. public Date(int year, int month, int date, int hrs, int min) {
  190. this(year, month, date, hrs, min, 0);
  191. }
  192. /**
  193. * Allocates a <code>Date</code> object and initializes it so that
  194. * it represents the instant at the start of the second specified
  195. * by the <code>year</code>, <code>month</code>, <code>date</code>,
  196. * <code>hrs</code>, <code>min</code>, and <code>sec</code> arguments,
  197. * in the local time zone.
  198. *
  199. * @param year the year minus 1900.
  200. * @param month the month between 0-11.
  201. * @param date the day of the month between 1-31.
  202. * @param hrs the hours between 0-23.
  203. * @param min the minutes between 0-59.
  204. * @param sec the seconds between 0-59.
  205. * @see java.util.Calendar
  206. * @deprecated As of JDK version 1.1,
  207. * replaced by <code>Calendar.set(year + 1900, month, date,
  208. * hrs, min, sec)</code> or <code>GregorianCalendar(year + 1900,
  209. * month, date, hrs, min, sec)</code>.
  210. */
  211. @Deprecated
  212. public Date(int year, int month, int date, int hrs, int min, int sec) {
  213. int y = year + 1900;
  214. // month is 0-based. So we have to normalize month to support Long.MAX_VALUE.
  215. if (month >= 12) {
  216. y += month / 12;
  217. month %= 12;
  218. } else if (month < 0) {
  219. y += CalendarUtils.floorDivide(month, 12);
  220. month = CalendarUtils.mod(month, 12);
  221. }
  222. BaseCalendar cal = getCalendarSystem(y);
  223. cdate = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.getDefaultRef());
  224. cdate.setNormalizedDate(y, month + 1, date).setTimeOfDay(hrs, min, sec, 0);
  225. getTimeImpl();
  226. cdate = null;
  227. }
  228. /**
  229. * Allocates a <code>Date</code> object and initializes it so that
  230. * it represents the date and time indicated by the string
  231. * <code>s</code>, which is interpreted as if by the
  232. * {@link Date#parse} method.
  233. *
  234. * @param s a string representation of the date.
  235. * @see java.text.DateFormat
  236. * @see java.util.Date#parse(java.lang.String)
  237. * @deprecated As of JDK version 1.1,
  238. * replaced by <code>DateFormat.parse(String s)</code>.
  239. */
  240. @Deprecated
  241. public Date(String s) {
  242. this(parse(s));
  243. }
  244. /**
  245. * Return a copy of this object.
  246. */
  247. public Object clone() {
  248. Date d = null;
  249. try {
  250. d = (Date)super.clone();
  251. if (cdate != null) {
  252. d.cdate = (BaseCalendar.Date) cdate.clone();
  253. }
  254. } catch (CloneNotSupportedException e) {} // Won't happen
  255. return d;
  256. }
  257. /**
  258. * Determines the date and time based on the arguments. The
  259. * arguments are interpreted as a year, month, day of the month,
  260. * hour of the day, minute within the hour, and second within the
  261. * minute, exactly as for the <tt>Date</tt> constructor with six
  262. * arguments, except that the arguments are interpreted relative
  263. * to UTC rather than to the local time zone. The time indicated is
  264. * returned represented as the distance, measured in milliseconds,
  265. * of that time from the epoch (00:00:00 GMT on January 1, 1970).
  266. *
  267. * @param year the year minus 1900.
  268. * @param month the month between 0-11.
  269. * @param date the day of the month between 1-31.
  270. * @param hrs the hours between 0-23.
  271. * @param min the minutes between 0-59.
  272. * @param sec the seconds between 0-59.
  273. * @return the number of milliseconds since January 1, 1970, 00:00:00 GMT for
  274. * the date and time specified by the arguments.
  275. * @see java.util.Calendar
  276. * @deprecated As of JDK version 1.1,
  277. * replaced by <code>Calendar.set(year + 1900, month, date,
  278. * hrs, min, sec)</code> or <code>GregorianCalendar(year + 1900,
  279. * month, date, hrs, min, sec)</code>, using a UTC
  280. * <code>TimeZone</code>, followed by <code>Calendar.getTime().getTime()</code>.
  281. */
  282. @Deprecated
  283. public static long UTC(int year, int month, int date,
  284. int hrs, int min, int sec) {
  285. int y = year + 1900;
  286. // month is 0-based. So we have to normalize month to support Long.MAX_VALUE.
  287. if (month >= 12) {
  288. y += month / 12;
  289. month %= 12;
  290. } else if (month < 0) {
  291. y += CalendarUtils.floorDivide(month, 12);
  292. month = CalendarUtils.mod(month, 12);
  293. }
  294. int m = month + 1;
  295. BaseCalendar cal = getCalendarSystem(y);
  296. BaseCalendar.Date udate = (BaseCalendar.Date) cal.newCalendarDate(null);
  297. udate.setNormalizedDate(y, m, date).setTimeOfDay(hrs, min, sec, 0);
  298. udate = normalize(udate);
  299. cal = getCalendarSystem(udate);
  300. return cal.getTime(udate);
  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. @Deprecated
  416. public static long parse(String s) {
  417. int year = Integer.MIN_VALUE;
  418. int mon = -1;
  419. int mday = -1;
  420. int hour = -1;
  421. int min = -1;
  422. int sec = -1;
  423. int millis = -1;
  424. int c = -1;
  425. int i = 0;
  426. int n = -1;
  427. int wst = -1;
  428. int tzoffset = -1;
  429. int prevc = 0;
  430. syntax:
  431. {
  432. if (s == null)
  433. break syntax;
  434. int limit = s.length();
  435. while (i < limit) {
  436. c = s.charAt(i);
  437. i++;
  438. if (c <= ' ' || c == ',')
  439. continue;
  440. if (c == '(') { // skip comments
  441. int depth = 1;
  442. while (i < limit) {
  443. c = s.charAt(i);
  444. i++;
  445. if (c == '(') depth++;
  446. else if (c == ')')
  447. if (--depth <= 0)
  448. break;
  449. }
  450. continue;
  451. }
  452. if ('0' <= c && c <= '9') {
  453. n = c - '0';
  454. while (i < limit && '0' <= (c = s.charAt(i)) && c <= '9') {
  455. n = n * 10 + c - '0';
  456. i++;
  457. }
  458. if (prevc == '+' || prevc == '-' && year != Integer.MIN_VALUE) {
  459. // timezone offset
  460. if (n < 24)
  461. n = n * 60; // EG. "GMT-3"
  462. else
  463. n = n % 100 + n / 100 * 60; // eg "GMT-0430"
  464. if (prevc == '+') // plus means east of GMT
  465. n = -n;
  466. if (tzoffset != 0 && tzoffset != -1)
  467. break syntax;
  468. tzoffset = n;
  469. } else if (n >= 70)
  470. if (year != Integer.MIN_VALUE)
  471. break syntax;
  472. else if (c <= ' ' || c == ',' || c == '/' || i >= limit)
  473. // year = n < 1900 ? n : n - 1900;
  474. year = n;
  475. else
  476. break syntax;
  477. else if (c == ':')
  478. if (hour < 0)
  479. hour = (byte) n;
  480. else if (min < 0)
  481. min = (byte) n;
  482. else
  483. break syntax;
  484. else if (c == '/')
  485. if (mon < 0)
  486. mon = (byte) (n - 1);
  487. else if (mday < 0)
  488. mday = (byte) n;
  489. else
  490. break syntax;
  491. else if (i < limit && c != ',' && c > ' ' && c != '-')
  492. break syntax;
  493. else if (hour >= 0 && min < 0)
  494. min = (byte) n;
  495. else if (min >= 0 && sec < 0)
  496. sec = (byte) n;
  497. else if (mday < 0)
  498. mday = (byte) n;
  499. // Handle two-digit years < 70 (70-99 handled above).
  500. else if (year == Integer.MIN_VALUE && mon >= 0 && mday >= 0)
  501. year = n;
  502. else
  503. break syntax;
  504. prevc = 0;
  505. } else if (c == '/' || c == ':' || c == '+' || c == '-')
  506. prevc = c;
  507. else {
  508. int st = i - 1;
  509. while (i < limit) {
  510. c = s.charAt(i);
  511. if (!('A' <= c && c <= 'Z' || 'a' <= c && c <= 'z'))
  512. break;
  513. i++;
  514. }
  515. if (i <= st + 1)
  516. break syntax;
  517. int k;
  518. for (k = wtb.length; --k >= 0;)
  519. if (wtb[k].regionMatches(true, 0, s, st, i - st)) {
  520. int action = ttb[k];
  521. if (action != 0) {
  522. if (action == 1) { // pm
  523. if (hour > 12 || hour < 1)
  524. break syntax;
  525. else if (hour < 12)
  526. hour += 12;
  527. } else if (action == 14) { // am
  528. if (hour > 12 || hour < 1)
  529. break syntax;
  530. else if (hour == 12)
  531. hour = 0;
  532. } else if (action <= 13) { // month!
  533. if (mon < 0)
  534. mon = (byte) (action - 2);
  535. else
  536. break syntax;
  537. } else {
  538. tzoffset = action - 10000;
  539. }
  540. }
  541. break;
  542. }
  543. if (k < 0)
  544. break syntax;
  545. prevc = 0;
  546. }
  547. }
  548. if (year == Integer.MIN_VALUE || mon < 0 || mday < 0)
  549. break syntax;
  550. // Parse 2-digit years within the correct default century.
  551. if (year < 100) {
  552. synchronized (Date.class) {
  553. if (defaultCenturyStart == 0) {
  554. defaultCenturyStart = gcal.getCalendarDate().getYear() - 80;
  555. }
  556. }
  557. year += (defaultCenturyStart / 100) * 100;
  558. if (year < defaultCenturyStart) year += 100;
  559. }
  560. if (sec < 0)
  561. sec = 0;
  562. if (min < 0)
  563. min = 0;
  564. if (hour < 0)
  565. hour = 0;
  566. BaseCalendar cal = getCalendarSystem(year);
  567. if (tzoffset == -1) { // no time zone specified, have to use local
  568. BaseCalendar.Date ldate = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.getDefaultRef());
  569. ldate.setDate(year, mon + 1, mday);
  570. ldate.setTimeOfDay(hour, min, sec, 0);
  571. return cal.getTime(ldate);
  572. }
  573. BaseCalendar.Date udate = (BaseCalendar.Date) cal.newCalendarDate(null); // no time zone
  574. udate.setDate(year, mon + 1, mday);
  575. udate.setTimeOfDay(hour, min, sec, 0);
  576. return cal.getTime(udate) + tzoffset * (60 * 1000);
  577. }
  578. // syntax error
  579. throw new IllegalArgumentException();
  580. }
  581. private final static String wtb[] = {
  582. "am", "pm",
  583. "monday", "tuesday", "wednesday", "thursday", "friday",
  584. "saturday", "sunday",
  585. "january", "february", "march", "april", "may", "june",
  586. "july", "august", "september", "october", "november", "december",
  587. "gmt", "ut", "utc", "est", "edt", "cst", "cdt",
  588. "mst", "mdt", "pst", "pdt"
  589. };
  590. private final static int ttb[] = {
  591. 14, 1, 0, 0, 0, 0, 0, 0, 0,
  592. 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
  593. 10000 + 0, 10000 + 0, 10000 + 0, // GMT/UT/UTC
  594. 10000 + 5 * 60, 10000 + 4 * 60, // EST/EDT
  595. 10000 + 6 * 60, 10000 + 5 * 60, // CST/CDT
  596. 10000 + 7 * 60, 10000 + 6 * 60, // MST/MDT
  597. 10000 + 8 * 60, 10000 + 7 * 60 // PST/PDT
  598. };
  599. /**
  600. * Returns a value that is the result of subtracting 1900 from the
  601. * year that contains or begins with the instant in time represented
  602. * by this <code>Date</code> object, as interpreted in the local
  603. * time zone.
  604. *
  605. * @return the year represented by this date, minus 1900.
  606. * @see java.util.Calendar
  607. * @deprecated As of JDK version 1.1,
  608. * replaced by <code>Calendar.get(Calendar.YEAR) - 1900</code>.
  609. */
  610. @Deprecated
  611. public int getYear() {
  612. return normalize().getYear() - 1900;
  613. }
  614. /**
  615. * Sets the year of this <tt>Date</tt> object to be the specified
  616. * value plus 1900. This <code>Date</code> object is modified so
  617. * that it represents a point in time within the specified year,
  618. * with the month, date, hour, minute, and second the same as
  619. * before, as interpreted in the local time zone. (Of course, if
  620. * the date was February 29, for example, and the year is set to a
  621. * non-leap year, then the new date will be treated as if it were
  622. * on March 1.)
  623. *
  624. * @param year the year value.
  625. * @see java.util.Calendar
  626. * @deprecated As of JDK version 1.1,
  627. * replaced by <code>Calendar.set(Calendar.YEAR, year + 1900)</code>.
  628. */
  629. @Deprecated
  630. public void setYear(int year) {
  631. getCalendarDate().setNormalizedYear(year + 1900);
  632. }
  633. /**
  634. * Returns a number representing the month that contains or begins
  635. * with the instant in time represented by this <tt>Date</tt> object.
  636. * The value returned is between <code>0</code> and <code>11</code>,
  637. * with the value <code>0</code> representing January.
  638. *
  639. * @return the month represented by this date.
  640. * @see java.util.Calendar
  641. * @deprecated As of JDK version 1.1,
  642. * replaced by <code>Calendar.get(Calendar.MONTH)</code>.
  643. */
  644. @Deprecated
  645. public int getMonth() {
  646. return normalize().getMonth() - 1; // adjust 1-based to 0-based
  647. }
  648. /**
  649. * Sets the month of this date to the specified value. This
  650. * <tt>Date</tt> object is modified so that it represents a point
  651. * in time within the specified month, with the year, date, hour,
  652. * minute, and second the same as before, as interpreted in the
  653. * local time zone. If the date was October 31, for example, and
  654. * the month is set to June, then the new date will be treated as
  655. * if it were on July 1, because June has only 30 days.
  656. *
  657. * @param month the month value between 0-11.
  658. * @see java.util.Calendar
  659. * @deprecated As of JDK version 1.1,
  660. * replaced by <code>Calendar.set(Calendar.MONTH, int month)</code>.
  661. */
  662. @Deprecated
  663. public void setMonth(int month) {
  664. int y = 0;
  665. if (month >= 12) {
  666. y = month / 12;
  667. month %= 12;
  668. } else if (month < 0) {
  669. y = CalendarUtils.floorDivide(month, 12);
  670. month = CalendarUtils.mod(month, 12);
  671. }
  672. BaseCalendar.Date d = getCalendarDate();
  673. if (y != 0) {
  674. d.setNormalizedYear(d.getNormalizedYear() + y);
  675. }
  676. d.setMonth(month + 1); // adjust 0-based to 1-based month numbering
  677. }
  678. /**
  679. * Returns the day of the month represented by this <tt>Date</tt> object.
  680. * The value returned is between <code>1</code> and <code>31</code>
  681. * representing the day of the month that contains or begins with the
  682. * instant in time represented by this <tt>Date</tt> object, as
  683. * interpreted in the local time zone.
  684. *
  685. * @return the day of the month represented by this date.
  686. * @see java.util.Calendar
  687. * @deprecated As of JDK version 1.1,
  688. * replaced by <code>Calendar.get(Calendar.DAY_OF_MONTH)</code>.
  689. * @deprecated
  690. */
  691. @Deprecated
  692. public int getDate() {
  693. return normalize().getDayOfMonth();
  694. }
  695. /**
  696. * Sets the day of the month of this <tt>Date</tt> object to the
  697. * specified value. This <tt>Date</tt> object is modified so that
  698. * it represents a point in time within the specified day of the
  699. * month, with the year, month, hour, minute, and second the same
  700. * as before, as interpreted in the local time zone. If the date
  701. * was April 30, for example, and the date is set to 31, then it
  702. * will be treated as if it were on May 1, because April has only
  703. * 30 days.
  704. *
  705. * @param date the day of the month value between 1-31.
  706. * @see java.util.Calendar
  707. * @deprecated As of JDK version 1.1,
  708. * replaced by <code>Calendar.set(Calendar.DAY_OF_MONTH, int date)</code>.
  709. */
  710. @Deprecated
  711. public void setDate(int date) {
  712. getCalendarDate().setDayOfMonth(date);
  713. }
  714. /**
  715. * Returns the day of the week represented by this date. The
  716. * returned value (<tt>0</tt> = Sunday, <tt>1</tt> = Monday,
  717. * <tt>2</tt> = Tuesday, <tt>3</tt> = Wednesday, <tt>4</tt> =
  718. * Thursday, <tt>5</tt> = Friday, <tt>6</tt> = Saturday)
  719. * represents the day of the week that contains or begins with
  720. * the instant in time represented by this <tt>Date</tt> object,
  721. * as interpreted in the local time zone.
  722. *
  723. * @return the day of the week represented by this date.
  724. * @see java.util.Calendar
  725. * @deprecated As of JDK version 1.1,
  726. * replaced by <code>Calendar.get(Calendar.DAY_OF_WEEK)</code>.
  727. */
  728. @Deprecated
  729. public int getDay() {
  730. return normalize().getDayOfWeek() - gcal.SUNDAY;
  731. }
  732. /**
  733. * Returns the hour represented by this <tt>Date</tt> object. The
  734. * returned value is a number (<tt>0</tt> through <tt>23</tt>)
  735. * representing the hour within the day that contains or begins
  736. * with the instant in time represented by this <tt>Date</tt>
  737. * object, as interpreted in the local time zone.
  738. *
  739. * @return the hour represented by this date.
  740. * @see java.util.Calendar
  741. * @deprecated As of JDK version 1.1,
  742. * replaced by <code>Calendar.get(Calendar.HOUR_OF_DAY)</code>.
  743. */
  744. @Deprecated
  745. public int getHours() {
  746. return normalize().getHours();
  747. }
  748. /**
  749. * Sets the hour of this <tt>Date</tt> object to the specified value.
  750. * This <tt>Date</tt> object is modified so that it represents a point
  751. * in time within the specified hour of the day, with the year, month,
  752. * date, minute, and second the same as before, as interpreted in the
  753. * local time zone.
  754. *
  755. * @param hours the hour value.
  756. * @see java.util.Calendar
  757. * @deprecated As of JDK version 1.1,
  758. * replaced by <code>Calendar.set(Calendar.HOUR_OF_DAY, int hours)</code>.
  759. */
  760. @Deprecated
  761. public void setHours(int hours) {
  762. getCalendarDate().setHours(hours);
  763. }
  764. /**
  765. * Returns the number of minutes past the hour represented by this date,
  766. * as interpreted in the local time zone.
  767. * The value returned is between <code>0</code> and <code>59</code>.
  768. *
  769. * @return the number of minutes past the hour represented by this date.
  770. * @see java.util.Calendar
  771. * @deprecated As of JDK version 1.1,
  772. * replaced by <code>Calendar.get(Calendar.MINUTE)</code>.
  773. */
  774. @Deprecated
  775. public int getMinutes() {
  776. return normalize().getMinutes();
  777. }
  778. /**
  779. * Sets the minutes of this <tt>Date</tt> object to the specified value.
  780. * This <tt>Date</tt> object is modified so that it represents a point
  781. * in time within the specified minute of the hour, with the year, month,
  782. * date, hour, and second the same as before, as interpreted in the
  783. * local time zone.
  784. *
  785. * @param minutes the value of the minutes.
  786. * @see java.util.Calendar
  787. * @deprecated As of JDK version 1.1,
  788. * replaced by <code>Calendar.set(Calendar.MINUTE, int minutes)</code>.
  789. */
  790. @Deprecated
  791. public void setMinutes(int minutes) {
  792. getCalendarDate().setMinutes(minutes);
  793. }
  794. /**
  795. * Returns the number of seconds past the minute represented by this date.
  796. * The value returned is between <code>0</code> and <code>61</code>. The
  797. * values <code>60</code> and <code>61</code> can only occur on those
  798. * Java Virtual Machines that take leap seconds into account.
  799. *
  800. * @return the number of seconds past the minute represented by this date.
  801. * @see java.util.Calendar
  802. * @deprecated As of JDK version 1.1,
  803. * replaced by <code>Calendar.get(Calendar.SECOND)</code>.
  804. */
  805. @Deprecated
  806. public int getSeconds() {
  807. return normalize().getSeconds();
  808. }
  809. /**
  810. * Sets the seconds of this <tt>Date</tt> to the specified value.
  811. * This <tt>Date</tt> object is modified so that it represents a
  812. * point in time within the specified second of the minute, with
  813. * the year, month, date, hour, and minute the same as before, as
  814. * interpreted in the local time zone.
  815. *
  816. * @param seconds the seconds value.
  817. * @see java.util.Calendar
  818. * @deprecated As of JDK version 1.1,
  819. * replaced by <code>Calendar.set(Calendar.SECOND, int seconds)</code>.
  820. */
  821. @Deprecated
  822. public void setSeconds(int seconds) {
  823. getCalendarDate().setSeconds(seconds);
  824. }
  825. /**
  826. * Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT
  827. * represented by this <tt>Date</tt> object.
  828. *
  829. * @return the number of milliseconds since January 1, 1970, 00:00:00 GMT
  830. * represented by this date.
  831. */
  832. public long getTime() {
  833. return getTimeImpl();
  834. }
  835. private final long getTimeImpl() {
  836. if (cdate != null) {
  837. normalize();
  838. BaseCalendar cal = getCalendarSystem(cdate);
  839. fastTime = cal.getTime(cdate);
  840. }
  841. return fastTime;
  842. }
  843. /**
  844. * Sets this <code>Date</code> object to represent a point in time that is
  845. * <code>time</code> milliseconds after January 1, 1970 00:00:00 GMT.
  846. *
  847. * @param time the number of milliseconds.
  848. */
  849. public void setTime(long time) {
  850. fastTime = time;
  851. cdate = null;
  852. }
  853. /**
  854. * Tests if this date is before the specified date.
  855. *
  856. * @param when a date.
  857. * @return <code>true</code> if and only if the instant of time
  858. * represented by this <tt>Date</tt> object is strictly
  859. * earlier than the instant represented by <tt>when</tt>
  860. * <code>false</code> otherwise.
  861. * @exception NullPointerException if <code>when</code> is null.
  862. */
  863. public boolean before(Date when) {
  864. return getMillisOf(this) < getMillisOf(when);
  865. }
  866. /**
  867. * Tests if this date is after the specified date.
  868. *
  869. * @param when a date.
  870. * @return <code>true</code> if and only if the instant represented
  871. * by this <tt>Date</tt> object is strictly later than the
  872. * instant represented by <tt>when</tt>
  873. * <code>false</code> otherwise.
  874. * @exception NullPointerException if <code>when</code> is null.
  875. */
  876. public boolean after(Date when) {
  877. return getMillisOf(this) > getMillisOf(when);
  878. }
  879. /**
  880. * Compares two dates for equality.
  881. * The result is <code>true</code> if and only if the argument is
  882. * not <code>null</code> and is a <code>Date</code> object that
  883. * represents the same point in time, to the millisecond, as this object.
  884. * <p>
  885. * Thus, two <code>Date</code> objects are equal if and only if the
  886. * <code>getTime</code> method returns the same <code>long</code>
  887. * value for both.
  888. *
  889. * @param obj the object to compare with.
  890. * @return <code>true</code> if the objects are the same;
  891. * <code>false</code> otherwise.
  892. * @see java.util.Date#getTime()
  893. */
  894. public boolean equals(Object obj) {
  895. return obj instanceof Date && getTime() == ((Date) obj).getTime();
  896. }
  897. /**
  898. * Returns the millisecond value of this <code>Date</code> object
  899. * without affecting its internal state.
  900. */
  901. static final long getMillisOf(Date date) {
  902. if (date.cdate == null) {
  903. return date.fastTime;
  904. }
  905. BaseCalendar.Date d = (BaseCalendar.Date) date.cdate.clone();
  906. return gcal.getTime(d);
  907. }
  908. /**
  909. * Compares two Dates for ordering.
  910. *
  911. * @param anotherDate the <code>Date</code> to be compared.
  912. * @return the value <code>0</code> if the argument Date is equal to
  913. * this Date; a value less than <code>0</code> if this Date
  914. * is before the Date argument; and a value greater than
  915. * <code>0</code> if this Date is after the Date argument.
  916. * @since 1.2
  917. * @exception NullPointerException if <code>anotherDate</code> is null.
  918. */
  919. public int compareTo(Date anotherDate) {
  920. long thisTime = getMillisOf(this);
  921. long anotherTime = getMillisOf(anotherDate);
  922. return (thisTime<anotherTime ? -1 : (thisTime==anotherTime ? 0 : 1));
  923. }
  924. /**
  925. * Returns a hash code value for this object. The result is the
  926. * exclusive OR of the two halves of the primitive <tt>long</tt>
  927. * value returned by the {@link Date#getTime}
  928. * method. That is, the hash code is the value of the expression:
  929. * <blockquote><pre>
  930. * (int)(this.getTime()^(this.getTime() >>> 32))</pre></blockquote>
  931. *
  932. * @return a hash code value for this object.
  933. */
  934. public int hashCode() {
  935. long ht = this.getTime();
  936. return (int) ht ^ (int) (ht >> 32);
  937. }
  938. /**
  939. * Converts this <code>Date</code> object to a <code>String</code>
  940. * of the form:
  941. * <blockquote><pre>
  942. * dow mon dd hh:mm:ss zzz yyyy</pre></blockquote>
  943. * where:<ul>
  944. * <li><tt>dow</tt> is the day of the week (<tt>Sun, Mon, Tue, Wed,
  945. * Thu, Fri, Sat</tt>).
  946. * <li><tt>mon</tt> is the month (<tt>Jan, Feb, Mar, Apr, May, Jun,
  947. * Jul, Aug, Sep, Oct, Nov, Dec</tt>).
  948. * <li><tt>dd</tt> is the day of the month (<tt>01</tt> through
  949. * <tt>31</tt>), as two decimal digits.
  950. * <li><tt>hh</tt> is the hour of the day (<tt>00</tt> through
  951. * <tt>23</tt>), as two decimal digits.
  952. * <li><tt>mm</tt> is the minute within the hour (<tt>00</tt> through
  953. * <tt>59</tt>), as two decimal digits.
  954. * <li><tt>ss</tt> is the second within the minute (<tt>00</tt> through
  955. * <tt>61</tt>, as two decimal digits.
  956. * <li><tt>zzz</tt> is the time zone (and may reflect daylight saving
  957. * time). Standard time zone abbreviations include those
  958. * recognized by the method <tt>parse</tt>. If time zone
  959. * information is not available, then <tt>zzz</tt> is empty -
  960. * that is, it consists of no characters at all.
  961. * <li><tt>yyyy</tt> is the year, as four decimal digits.
  962. * </ul>
  963. *
  964. * @return a string representation of this date.
  965. * @see java.util.Date#toLocaleString()
  966. * @see java.util.Date#toGMTString()
  967. */
  968. public String toString() {
  969. // "EEE MMM dd HH:mm:ss zzz yyyy";
  970. BaseCalendar.Date date = normalize();
  971. StringBuilder sb = new StringBuilder(28);
  972. int index = date.getDayOfWeek();
  973. if (index == gcal.SUNDAY) {
  974. index = 8;
  975. }
  976. convertToAbbr(sb, wtb[index]).append(' '); // EEE
  977. convertToAbbr(sb, wtb[date.getMonth() - 1 + 2 + 7]).append(' '); // MMM
  978. CalendarUtils.sprintf0d(sb, date.getDayOfMonth(), 2).append(' '); // dd
  979. CalendarUtils.sprintf0d(sb, date.getHours(), 2).append(':'); // HH
  980. CalendarUtils.sprintf0d(sb, date.getMinutes(), 2).append(':'); // mm
  981. CalendarUtils.sprintf0d(sb, date.getSeconds(), 2).append(' '); // ss
  982. TimeZone zi = date.getZone();
  983. if (zi != null) {
  984. sb.append(zi.getDisplayName(date.isDaylightTime(), zi.SHORT, Locale.US)); // zzz
  985. } else {
  986. sb.append("GMT");
  987. }
  988. sb.append(' ').append(date.getYear()); // yyyy
  989. return sb.toString();
  990. }
  991. /**
  992. * Converts the given name to its 3-letter abbreviation (e.g.,
  993. * "monday" -> "Mon") and stored the abbreviation in the given
  994. * <code>StringBuilder</code>.
  995. */
  996. private static final StringBuilder convertToAbbr(StringBuilder sb, String name) {
  997. sb.append(Character.toUpperCase(name.charAt(0)));
  998. sb.append(name.charAt(1)).append(name.charAt(2));
  999. return sb;
  1000. }
  1001. /**
  1002. * Creates a string representation of this <tt>Date</tt> object in an
  1003. * implementation-dependent form. The intent is that the form should
  1004. * be familiar to the user of the Java application, wherever it may
  1005. * happen to be running. The intent is comparable to that of the
  1006. * "<code>%c</code>" format supported by the <code>strftime()</code>
  1007. * function of ISO C.
  1008. *
  1009. * @return a string representation of this date, using the locale
  1010. * conventions.
  1011. * @see java.text.DateFormat
  1012. * @see java.util.Date#toString()
  1013. * @see java.util.Date#toGMTString()
  1014. * @deprecated As of JDK version 1.1,
  1015. * replaced by <code>DateFormat.format(Date date)</code>.
  1016. */
  1017. @Deprecated
  1018. public String toLocaleString() {
  1019. DateFormat formatter = DateFormat.getDateTimeInstance();
  1020. return formatter.format(this);
  1021. }
  1022. /**
  1023. * Creates a string representation of this <tt>Date</tt> object of
  1024. * the form:
  1025. * <blockquote<pre>
  1026. * d mon yyyy hh:mm:ss GMT</pre></blockquote>
  1027. * where:<ul>
  1028. * <li><i>d</i> is the day of the month (<tt>1</tt> through <tt>31</tt>),
  1029. * as one or two decimal digits.
  1030. * <li><i>mon</i> is the month (<tt>Jan, Feb, Mar, Apr, May, Jun, Jul,
  1031. * Aug, Sep, Oct, Nov, Dec</tt>).
  1032. * <li><i>yyyy</i> is the year, as four decimal digits.
  1033. * <li><i>hh</i> is the hour of the day (<tt>00</tt> through <tt>23</tt>),
  1034. * as two decimal digits.
  1035. * <li><i>mm</i> is the minute within the hour (<tt>00</tt> through
  1036. * <tt>59</tt>), as two decimal digits.
  1037. * <li><i>ss</i> is the second within the minute (<tt>00</tt> through
  1038. * <tt>61</tt>), as two decimal digits.
  1039. * <li><i>GMT</i> is exactly the ASCII letters "<tt>GMT</tt>" to indicate
  1040. * Greenwich Mean Time.
  1041. * </ul><p>
  1042. * The result does not depend on the local time zone.
  1043. *
  1044. * @return a string representation of this date, using the Internet GMT
  1045. * conventions.
  1046. * @see java.text.DateFormat
  1047. * @see java.util.Date#toString()
  1048. * @see java.util.Date#toLocaleString()
  1049. * @deprecated As of JDK version 1.1,
  1050. * replaced by <code>DateFormat.format(Date date)</code>, using a
  1051. * GMT <code>TimeZone</code>.
  1052. */
  1053. @Deprecated
  1054. public String toGMTString() {
  1055. // d MMM yyyy HH:mm:ss 'GMT'
  1056. long t = getTime();
  1057. BaseCalendar cal = getCalendarSystem(t);
  1058. BaseCalendar.Date date =
  1059. (BaseCalendar.Date) cal.getCalendarDate(getTime(), (TimeZone)null);
  1060. StringBuilder sb = new StringBuilder(32);
  1061. CalendarUtils.sprintf0d(sb, date.getDayOfMonth(), 1).append(' '); // d
  1062. convertToAbbr(sb, wtb[date.getMonth() - 1 + 2 + 7]).append(' '); // MMM
  1063. sb.append(date.getYear()).append(' '); // yyyy
  1064. CalendarUtils.sprintf0d(sb, date.getHours(), 2).append(':'); // HH
  1065. CalendarUtils.sprintf0d(sb, date.getMinutes(), 2).append(':'); // mm
  1066. CalendarUtils.sprintf0d(sb, date.getSeconds(), 2); // ss
  1067. sb.append(" GMT"); // ' GMT'
  1068. return sb.toString();
  1069. }
  1070. /**
  1071. * Returns the offset, measured in minutes, for the local time zone
  1072. * relative to UTC that is appropriate for the time represented by
  1073. * this <code>Date</code> object.
  1074. * <p>
  1075. * For example, in Massachusetts, five time zones west of Greenwich:
  1076. * <blockquote><pre>
  1077. * new Date(96, 1, 14).getTimezoneOffset() returns 300</pre></blockquote>
  1078. * because on February 14, 1996, standard time (Eastern Standard Time)
  1079. * is in use, which is offset five hours from UTC; but:
  1080. * <blockquote><pre>
  1081. * new Date(96, 5, 1).getTimezoneOffset() returns 240</pre></blockquote>
  1082. * because on June 1, 1996, daylight saving time (Eastern Daylight Time)
  1083. * is in use, which is offset only four hours from UTC.<p>
  1084. * This method produces the same result as if it computed:
  1085. * <blockquote><pre>
  1086. * (this.getTime() - UTC(this.getYear(),
  1087. * this.getMonth(),
  1088. * this.getDate(),
  1089. * this.getHours(),
  1090. * this.getMinutes(),
  1091. * this.getSeconds())) / (60 * 1000)
  1092. * </pre></blockquote>
  1093. *
  1094. * @return the time-zone offset, in minutes, for the current time zone.
  1095. * @see java.util.Calendar#ZONE_OFFSET
  1096. * @see java.util.Calendar#DST_OFFSET
  1097. * @see java.util.TimeZone#getDefault
  1098. * @deprecated As of JDK version 1.1,
  1099. * replaced by <code>-(Calendar.get(Calendar.ZONE_OFFSET) +
  1100. * Calendar.get(Calendar.DST_OFFSET)) / (60 * 1000)</code>.
  1101. */
  1102. @Deprecated
  1103. public int getTimezoneOffset() {
  1104. int zoneOffset;
  1105. if (cdate == null) {
  1106. TimeZone tz = TimeZone.getDefaultRef();
  1107. if (tz instanceof ZoneInfo) {
  1108. zoneOffset = ((ZoneInfo)tz).getOffsets(fastTime, null);
  1109. } else {
  1110. zoneOffset = tz.getOffset(fastTime);
  1111. }
  1112. } else {
  1113. normalize();
  1114. zoneOffset = cdate.getZoneOffset();
  1115. }
  1116. return -zoneOffset60000; // convert to minutes
  1117. }
  1118. private final BaseCalendar.Date getCalendarDate() {
  1119. if (cdate == null) {
  1120. BaseCalendar cal = getCalendarSystem(fastTime);
  1121. cdate = (BaseCalendar.Date) cal.getCalendarDate(fastTime,
  1122. TimeZone.getDefaultRef());
  1123. }
  1124. return cdate;
  1125. }
  1126. private final BaseCalendar.Date normalize() {
  1127. if (cdate == null) {
  1128. BaseCalendar cal = getCalendarSystem(fastTime);
  1129. cdate = (BaseCalendar.Date) cal.getCalendarDate(fastTime,
  1130. TimeZone.getDefaultRef());
  1131. } else {
  1132. TimeZone tz = TimeZone.getDefaultRef();
  1133. if (tz != cdate.getZone()) {
  1134. BaseCalendar cal = getCalendarSystem(cdate);
  1135. long t = cal.getTime(cdate);
  1136. cdate.setZone(tz);
  1137. cal.getCalendarDate(t, cdate);
  1138. }
  1139. cdate = normalize(cdate);
  1140. }
  1141. return cdate;
  1142. }
  1143. private static final BaseCalendar.Date normalize(BaseCalendar.Date cdate) {
  1144. int y = cdate.getNormalizedYear();
  1145. int m = cdate.getMonth();
  1146. int d = cdate.getDayOfMonth();
  1147. int hh = cdate.getHours();
  1148. int mm = cdate.getMinutes();
  1149. int ss = cdate.getSeconds();
  1150. int ms = cdate.getMillis();
  1151. TimeZone tz = cdate.getZone();
  1152. // If the specified year can't be handled using a long value
  1153. // in milliseconds, GregorianCalendar is used for full
  1154. // compatibility with underflow and overflow. This is required
  1155. // by some JCK tests. The limits are based max year values -
  1156. // years that can be represented by max values of d, hh, mm,
  1157. // ss and ms. Also, let GregorianCalendar handle the default
  1158. // cutover year so that we don't need to worry about the
  1159. // transition here.
  1160. if (y == 1582 || y > 280000000 || y < -280000000) {
  1161. if (tz == null) {
  1162. tz = TimeZone.getTimeZone("GMT");
  1163. }
  1164. GregorianCalendar gc = new GregorianCalendar(tz);
  1165. gc.clear();
  1166. gc.set(gc.MILLISECOND, ms);
  1167. gc.set(y, m-1, d, hh, mm, ss);
  1168. long t = gc.getTimeInMillis();
  1169. BaseCalendar cal = getCalendarSystem(t);
  1170. cdate = (BaseCalendar.Date) cal.getCalendarDate(t, tz);
  1171. return cdate;
  1172. }
  1173. BaseCalendar cal = getCalendarSystem(y);
  1174. if (cal != getCalendarSystem(cdate)) {
  1175. cdate = (BaseCalendar.Date) cal.newCalendarDate(tz);
  1176. cdate.setNormalizedDate(y, m, d).setTimeOfDay(hh, mm, ss, ms);
  1177. }
  1178. // Perform the GregorianCalendar-style normalization.
  1179. long t = cal.getTime(cdate);
  1180. // In case the normalized date requires the other calendar
  1181. // system, we need to recalculate it using the other one.
  1182. BaseCalendar ncal = getCalendarSystem(t);
  1183. if (ncal != cal) {
  1184. cdate = (BaseCalendar.Date) ncal.newCalendarDate(cdate.getZone());
  1185. cdate.setNormalizedDate(y, m, d).setTimeOfDay(hh, mm, ss, ms);
  1186. ncal.getTime(cdate);
  1187. }
  1188. return cdate;
  1189. }
  1190. /**
  1191. * Returns the Gregorian or Julian calendar system to use with the
  1192. * given date. Use Gregorian from October 15, 1582.
  1193. *
  1194. * @param year normalized calendar year (not -1900)
  1195. * @return the CalendarSystem to use for the specified date
  1196. */
  1197. private static final BaseCalendar getCalendarSystem(int year) {
  1198. if (year >= 1582) {
  1199. return gcal;
  1200. }
  1201. return getJulianCalendar();
  1202. }
  1203. private static final BaseCalendar getCalendarSystem(long t) {
  1204. if (t >= GregorianCalendar.DEFAULT_GREGORIAN_CUTOVER) {
  1205. return gcal;
  1206. }
  1207. return getJulianCalendar();
  1208. }
  1209. private static final BaseCalendar getCalendarSystem(BaseCalendar.Date cdate) {
  1210. if (jcal == null) {
  1211. return gcal;
  1212. }
  1213. if (cdate.getEra() != null) {
  1214. return jcal;
  1215. }
  1216. return gcal;
  1217. }
  1218. synchronized private static final BaseCalendar getJulianCalendar() {
  1219. if (jcal == null) {
  1220. jcal = (BaseCalendar) CalendarSystem.forName("julian");
  1221. }
  1222. return jcal;
  1223. }
  1224. /**
  1225. * Save the state of this object to a stream (i.e., serialize it).
  1226. *
  1227. * @serialData The value returned by <code>getTime()</code>
  1228. * is emitted (long). This represents the offset from
  1229. * January 1, 1970, 00:00:00 GMT in milliseconds.
  1230. */
  1231. private void writeObject(ObjectOutputStream s)
  1232. throws IOException
  1233. {
  1234. s.writeLong(getTimeImpl());
  1235. }
  1236. /**
  1237. * Reconstitute this object from a stream (i.e., deserialize it).
  1238. */
  1239. private void readObject(ObjectInputStream s)
  1240. throws IOException, ClassNotFoundException
  1241. {
  1242. fastTime = s.readLong();
  1243. }
  1244. }