Date.java 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. /* java.util.Date
  2. Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.util;
  32. /**
  33. * This class represents a specific time in milliseconds since the epoch.
  34. * The epoch is 1970, January 1 00:00:00.0000 UTC.
  35. *
  36. * Date is intended to reflect universal time coordinate (UTC), but doesn't
  37. * handle the leap seconds.
  38. *
  39. * Prior to jdk 1.1 this class was the sole Time class and had also
  40. * calendar functionality. But this can't be localized, so a new Calendar
  41. * class was created, that you should use instead. The functions which
  42. * get or return a year, month, day etc. are all deprecated and shouldn't be
  43. * used. Use Calendar instead.
  44. *
  45. * @see Calendar
  46. * @see GregorianCalendar
  47. * @see java.text.DateFormat
  48. * @author Jochen Hoenicke
  49. * @author Per Bothner <bothner@cygnus.com>
  50. */
  51. public class Date implements Cloneable, Comparable, java.io.Serializable
  52. {
  53. /**
  54. * This is the serialization UID for this class
  55. */
  56. private static final long serialVersionUID = 7523967970034938905L;
  57. /**
  58. * The time in milliseconds since the epoch.
  59. */
  60. private transient long time;
  61. /**
  62. * Creates a new Date Object representing the current time.
  63. */
  64. public Date()
  65. {
  66. time = System.currentTimeMillis();
  67. }
  68. /**
  69. * Creates a new Date Object representing the given time.
  70. * @param time the time in milliseconds since the epoch.
  71. */
  72. public Date(long time)
  73. {
  74. this.time = time;
  75. }
  76. /**
  77. * Creates a new Date Object representing the given time.
  78. * @deprecated use <code>new GregorianCalendar(year+1900, month,
  79. * day)</code> instead.
  80. */
  81. public Date(int year, int month, int day)
  82. {
  83. time = new GregorianCalendar(year + 1900, month, day).getTimeInMillis();
  84. }
  85. /**
  86. * Creates a new Date Object representing the given time.
  87. * @deprecated use <code>new GregorianCalendar(year+1900, month,
  88. * day, hour, min)</code> instead.
  89. */
  90. public Date(int year, int month, int day, int hour, int min)
  91. {
  92. time =
  93. new GregorianCalendar(year + 1900, month, day, hour,
  94. min).getTimeInMillis();
  95. }
  96. /*
  97. * Creates a new Date Object representing the given time.
  98. * @deprecated use <code>new GregorianCalendar(year+1900, month,
  99. * day)</code> instead.
  100. */
  101. public Date(int year, int month, int day, int hour, int min, int sec)
  102. {
  103. time =
  104. new GregorianCalendar(year + 1900, month, day, hour, min,
  105. sec).getTimeInMillis();
  106. }
  107. /**
  108. * Creates a new Date from the given string representation. This
  109. * does the same as <code>new Date(Date.parse(s))</code>
  110. * @see #parse
  111. * @deprecated use <code>java.text.DateFormat.parse(s)</code> instead.
  112. */
  113. public Date(String s)
  114. {
  115. time = parse(s);
  116. }
  117. public Object clone()
  118. {
  119. try
  120. {
  121. return super.clone();
  122. }
  123. catch (CloneNotSupportedException ex)
  124. {
  125. return null;
  126. }
  127. }
  128. /**
  129. * @deprecated Use Calendar with a UTC TimeZone instead.
  130. * @return the time in millis since the epoch.
  131. */
  132. public static long UTC(int year, int month, int date,
  133. int hrs, int min, int sec)
  134. {
  135. GregorianCalendar cal =
  136. new GregorianCalendar(year + 1900, month, date, hrs, min, sec);
  137. cal.set(Calendar.ZONE_OFFSET, 0);
  138. cal.set(Calendar.DST_OFFSET, 0);
  139. return cal.getTimeInMillis();
  140. }
  141. /**
  142. * Gets the time represented by this Object
  143. * @return the time in milliseconds since the epoch.
  144. */
  145. public long getTime()
  146. {
  147. return time;
  148. }
  149. /**
  150. * @deprecated use
  151. * Calendar.get(Calendar.ZONE_OFFSET)+Calendar.get(Calendar.DST_OFFSET)
  152. * instead.
  153. * @return The time zone offset in minutes of the local time zone
  154. * relative to UTC. The time represented by this object is used to
  155. * determine if we should use daylight savings.
  156. */
  157. public int getTimezoneOffset()
  158. {
  159. Calendar cal = Calendar.getInstance();
  160. cal.setTimeInMillis(time);
  161. return (cal.get(Calendar.ZONE_OFFSET)
  162. + cal.get(Calendar.DST_OFFSET)) / (60 * 1000);
  163. }
  164. /**
  165. * Sets the time which this Object should represented.
  166. * @param time the time in milliseconds since the epoch. */
  167. public void setTime(long time)
  168. {
  169. this.time = time;
  170. }
  171. /**
  172. * Tests if this date is after the specified date.
  173. * @param when the other date
  174. * @return true, if the date represented by this Object is
  175. * strictly later than the time represented by when.
  176. */
  177. public boolean after(Date when)
  178. {
  179. return time > when.time;
  180. }
  181. /**
  182. * Tests if this date is before the specified date.
  183. * @param when the other date
  184. * @return true, if the date represented by when is strictly later
  185. * than the time represented by this object.
  186. */
  187. public boolean before(Date when)
  188. {
  189. return time < when.time;
  190. }
  191. /**
  192. * Compares two dates for equality.
  193. * @param obj the object to compare.
  194. * @return true, if obj is a Date object and the date represented
  195. * by obj is exactly the same as the time represented by this
  196. * object.
  197. */
  198. public boolean equals(Object obj)
  199. {
  200. return (obj instanceof Date && time == ((Date) obj).time);
  201. }
  202. /**
  203. * Compares two dates.
  204. * @param when the other date.
  205. * @return 0, if the date represented
  206. * by obj is exactly the same as the time represented by this
  207. * object, a negative if this Date is before the other Date, and
  208. * a positive value otherwise.
  209. */
  210. public int compareTo(Date when)
  211. {
  212. return (time < when.time) ? -1 : (time == when.time) ? 0 : 1;
  213. }
  214. /**
  215. * Compares this Date to another. This behaves like
  216. * <code>compareTo(Date)</code>, but it may throw a
  217. * <code>ClassCastException</code>
  218. * @param obj the other date.
  219. * @return 0, if the date represented
  220. * by obj is exactly the same as the time represented by this
  221. * object, a negative if this Date is before the other Date, and
  222. * a positive value otherwise.
  223. * @exception ClassCastException if obj is not of type Date.
  224. */
  225. public int compareTo(Object obj)
  226. {
  227. return compareTo((Date) obj);
  228. }
  229. public int hashCode()
  230. {
  231. return (int) time ^ (int) (time >>> 32);
  232. }
  233. private static final String[] weekNames = { "Sun", "Mon", "Tue", "Wed",
  234. "Thu", "Fri", "Sat" };
  235. private static final String[] monthNames = { "Jan", "Feb", "Mar", "Apr",
  236. "May", "Jun", "Jul", "Aug",
  237. "Sep", "Oct", "Nov", "Dec" };
  238. public String toString()
  239. {
  240. Calendar cal = Calendar.getInstance();
  241. cal.setTimeInMillis(time);
  242. String day = "0" + cal.get(Calendar.DATE);
  243. String hour = "0" + cal.get(Calendar.HOUR_OF_DAY);
  244. String min = "0" + cal.get(Calendar.MINUTE);
  245. String sec = "0" + cal.get(Calendar.SECOND);
  246. String year = "000" + cal.get(Calendar.YEAR);
  247. return weekNames[cal.get(Calendar.DAY_OF_WEEK) - 1] + " "
  248. + monthNames[cal.get(Calendar.MONTH)] + " "
  249. + day.substring(day.length() - 2) + " "
  250. + hour.substring(hour.length() - 2) + ":"
  251. + min.substring(min.length() - 2) + ":"
  252. + sec.substring(sec.length() - 2) + " "
  253. +
  254. cal.getTimeZone().getDisplayName(cal.getTimeZone().inDaylightTime(this),
  255. TimeZone.SHORT) + " " +
  256. year.substring(year.length() - 4);
  257. }
  258. /** Format this object in a locale-specific way.
  259. * @deprecated Use DateFormat.format(Date)
  260. */
  261. public String toLocaleString()
  262. {
  263. return java.text.DateFormat.getInstance().format(this);
  264. }
  265. /** Format this object in a standard format in the GMT timezone.
  266. * @deprecated Use DateFormat.format(Date) with a GMT TimeZone.
  267. */
  268. public String toGMTString()
  269. {
  270. java.text.DateFormat format = java.text.DateFormat.getInstance();
  271. format.setTimeZone(TimeZone.getTimeZone("GMT"));
  272. return format.format(this);
  273. }
  274. private static int skipParens(String string, int offset)
  275. {
  276. int len = string.length();
  277. int p = 0;
  278. int i;
  279. for (i = offset; i < len; ++i)
  280. {
  281. if (string.charAt(i) == '(')
  282. ++p;
  283. else if (string.charAt(i) == ')')
  284. {
  285. --p;
  286. if (p == 0)
  287. return i + 1;
  288. // If we've encounted unbalanced parens, just return the
  289. // leftover one as an ordinary character. It will be
  290. // caught later in parsing and cause an
  291. // IllegalArgumentException.
  292. if (p < 0)
  293. return i;
  294. }
  295. }
  296. // Not sure what to do if `p != 0' here.
  297. return i;
  298. }
  299. private static int parseTz(String tok, char sign)
  300. throws IllegalArgumentException
  301. {
  302. int num;
  303. try
  304. {
  305. // parseInt doesn't handle '+' so strip off sign.
  306. num = Integer.parseInt(tok.substring(1));
  307. }
  308. catch (NumberFormatException ex)
  309. {
  310. throw new IllegalArgumentException(tok);
  311. }
  312. // Convert hours to minutes.
  313. if (num < 24)
  314. num *= 60;
  315. else
  316. num = (num / 100) * 60 + num % 100;
  317. return sign == '-' ? -num : num;
  318. }
  319. private static int parseMonth(String tok)
  320. {
  321. // Initialize strings for month names.
  322. // We could possibly use the fields of DateFormatSymbols but that is
  323. // localized and thus might not match the English words specified.
  324. String months[] = { "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY",
  325. "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER",
  326. "NOVEMBER", "DECEMBER" };
  327. int i;
  328. for (i = 0; i < 12; i++)
  329. if (months[i].startsWith(tok))
  330. return i;
  331. // Return -1 if not found.
  332. return -1;
  333. }
  334. private static boolean parseDayOfWeek(String tok)
  335. {
  336. // Initialize strings for days of the week names.
  337. // We could possibly use the fields of DateFormatSymbols but that is
  338. // localized and thus might not match the English words specified.
  339. String daysOfWeek[] = { "SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY",
  340. "THURSDAY", "FRIDAY", "SATURDAY" };
  341. int i;
  342. for (i = 0; i < 7; i++)
  343. if (daysOfWeek[i].startsWith(tok))
  344. return true;
  345. return false;
  346. }
  347. /** Parse a String and return the time it represents.
  348. * @param s The String to parse.
  349. * @deprecated Use DateFormat.parse(String)
  350. */
  351. public static long parse(String string)
  352. {
  353. // Initialize date/time fields before parsing begins.
  354. int year = -1;
  355. int month = -1;
  356. int day = -1;
  357. int hour = -1;
  358. int minute = -1;
  359. int second = -1;
  360. int timezone = 0;
  361. boolean localTimezone = true;
  362. // Trim out any nested stuff in parentheses now to make parsing easier.
  363. StringBuffer buf = new StringBuffer();
  364. int off = 0;
  365. int openParenOffset, tmpMonth;
  366. while ((openParenOffset = string.indexOf('(', off)) >= 0)
  367. {
  368. // Copy part of string leading up to open paren.
  369. buf.append(string.substring(off, openParenOffset));
  370. off = skipParens(string, openParenOffset);
  371. }
  372. buf.append(string.substring(off));
  373. // Make all chars upper case to simplify comparisons later.
  374. // Also ignore commas; treat them as delimiters.
  375. StringTokenizer strtok =
  376. new StringTokenizer(buf.toString().toUpperCase(), " \t\n\r,");
  377. while (strtok.hasMoreTokens())
  378. {
  379. String tok = strtok.nextToken();
  380. char firstch = tok.charAt(0);
  381. if ((firstch == '+' || firstch == '-') && year >= 0)
  382. {
  383. timezone = parseTz(tok, firstch);
  384. localTimezone = false;
  385. }
  386. else if (firstch >= '0' && firstch <= '9')
  387. {
  388. while (tok != null && tok.length() > 0)
  389. {
  390. // A colon or slash may be valid in the number.
  391. // Find the first of these before calling parseInt.
  392. int colon = tok.indexOf(':');
  393. int slash = tok.indexOf('/');
  394. int hyphen = tok.indexOf('-');
  395. // We choose tok.length initially because it makes
  396. // processing simpler.
  397. int punctOffset = tok.length();
  398. if (colon >= 0)
  399. punctOffset = Math.min(punctOffset, colon);
  400. if (slash >= 0)
  401. punctOffset = Math.min(punctOffset, slash);
  402. if (hyphen >= 0)
  403. punctOffset = Math.min(punctOffset, hyphen);
  404. // Following code relies on -1 being the exceptional
  405. // case.
  406. if (punctOffset == tok.length())
  407. punctOffset = -1;
  408. int num;
  409. try
  410. {
  411. num = Integer.parseInt(punctOffset < 0 ? tok :
  412. tok.substring(0, punctOffset));
  413. }
  414. catch (NumberFormatException ex)
  415. {
  416. throw new IllegalArgumentException(tok);
  417. }
  418. // TBD: Spec says year can be followed by a slash. That might
  419. // make sense if using YY/MM/DD formats, but it would fail in
  420. // that format for years <= 70. Also, what about 1900? That
  421. // is interpreted as the year 3800; seems that the comparison
  422. // should be num >= 1900 rather than just > 1900.
  423. // What about a year of 62 - 70? (61 or less could be a (leap)
  424. // second). 70/MM/DD cause an exception but 71/MM/DD is ok
  425. // even though there's no ambiguity in either case.
  426. // For the parse method, the spec as written seems too loose.
  427. // Until shown otherwise, we'll follow the spec as written.
  428. if (num > 70 && (punctOffset < 0 || punctOffset == slash))
  429. year = num > 1900 ? num - 1900 : num;
  430. else if (punctOffset > 0 && punctOffset == colon)
  431. {
  432. if (hour < 0)
  433. hour = num;
  434. else
  435. minute = num;
  436. }
  437. else if (punctOffset > 0 && punctOffset == slash)
  438. {
  439. if (month < 0)
  440. month = num - 1;
  441. else
  442. day = num;
  443. }
  444. else if (hour >= 0 && minute < 0)
  445. minute = num;
  446. else if (minute >= 0 && second < 0)
  447. second = num;
  448. else if (day < 0)
  449. day = num;
  450. else
  451. throw new IllegalArgumentException(tok);
  452. // Advance string if there's more to process in this token.
  453. if (punctOffset < 0 || punctOffset + 1 >= tok.length())
  454. tok = null;
  455. else
  456. tok = tok.substring(punctOffset + 1);
  457. }
  458. }
  459. else if (firstch >= 'A' && firstch <= 'Z')
  460. {
  461. if (tok.equals("AM"))
  462. {
  463. if (hour < 1 || hour > 12)
  464. throw new IllegalArgumentException(tok);
  465. if (hour == 12)
  466. hour = 0;
  467. }
  468. else if (tok.equals("PM"))
  469. {
  470. if (hour < 1 || hour > 12)
  471. throw new IllegalArgumentException(tok);
  472. if (hour < 12)
  473. hour += 12;
  474. }
  475. else if (parseDayOfWeek(tok))
  476. ; // Ignore it; throw the token away.
  477. else if (tok.equals("UT") || tok.equals("UTC") || tok.equals("GMT"))
  478. localTimezone = false;
  479. else if (tok.startsWith("UT") || tok.startsWith("GMT"))
  480. {
  481. int signOffset = 3;
  482. if (tok.charAt(1) == 'T' && tok.charAt(2) != 'C')
  483. signOffset = 2;
  484. char sign = tok.charAt(signOffset);
  485. if (sign != '+' && sign != '-')
  486. throw new IllegalArgumentException(tok);
  487. timezone = parseTz(tok.substring(signOffset), sign);
  488. localTimezone = false;
  489. }
  490. else if ((tmpMonth = parseMonth(tok)) >= 0)
  491. month = tmpMonth;
  492. else if (tok.length() == 3 && tok.charAt(2) == 'T')
  493. {
  494. // Convert timezone offset from hours to minutes.
  495. char ch = tok.charAt(0);
  496. if (ch == 'E')
  497. timezone = -5 * 60;
  498. else if (ch == 'C')
  499. timezone = -6 * 60;
  500. else if (ch == 'M')
  501. timezone = -7 * 60;
  502. else if (ch == 'P')
  503. timezone = -8 * 60;
  504. else
  505. throw new IllegalArgumentException(tok);
  506. // Shift 60 minutes for Daylight Savings Time.
  507. if (tok.charAt(1) == 'D')
  508. timezone += 60;
  509. else if (tok.charAt(1) != 'S')
  510. throw new IllegalArgumentException(tok);
  511. localTimezone = false;
  512. }
  513. else
  514. throw new IllegalArgumentException(tok);
  515. }
  516. else
  517. throw new IllegalArgumentException(tok);
  518. }
  519. // Unspecified minutes and seconds should default to 0.
  520. if (minute < 0)
  521. minute = 0;
  522. if (second < 0)
  523. second = 0;
  524. // Throw exception if any other fields have not been recognized and set.
  525. if (year < 0 || month < 0 || day < 0 || hour < 0)
  526. throw new IllegalArgumentException("Missing field");
  527. // Return the time in either local time or relative to GMT as parsed.
  528. // If no time-zone was specified, get the local one (in minutes) and
  529. // convert to milliseconds before adding to the UTC.
  530. return UTC(year, month, day, hour, minute, second) + (localTimezone ?
  531. new Date(year, month, day).getTimezoneOffset() * 60 * 1000:
  532. -timezone * 60 * 1000);
  533. }
  534. /**
  535. * @return the year minus 1900 represented by this date object.
  536. * @deprecated Use Calendar instead of Date, and use get(Calendar.YEAR)
  537. * instead. Note about the 1900 difference in year.
  538. */
  539. public int getYear()
  540. {
  541. Calendar cal = Calendar.getInstance();
  542. cal.setTimeInMillis(time);
  543. return cal.get(Calendar.YEAR) - 1900;
  544. }
  545. /**
  546. * Sets the year to year minus 1900, not changing the other fields.
  547. * @param year the year minus 1900.
  548. * @deprecated Use Calendar instead of Date, and use
  549. * set(Calendar.YEAR, year) instead. Note about the 1900
  550. * difference in year.
  551. */
  552. public void setYear(int year)
  553. {
  554. Calendar cal = Calendar.getInstance();
  555. cal.setTimeInMillis(time);
  556. cal.set(Calendar.YEAR, 1900 + year);
  557. time = cal.getTimeInMillis();
  558. }
  559. /**
  560. * @return the month represented by this date object (zero based).
  561. * @deprecated Use Calendar instead of Date, and use get(Calendar.MONTH)
  562. * instead.
  563. */
  564. public int getMonth()
  565. {
  566. Calendar cal = Calendar.getInstance();
  567. cal.setTimeInMillis(time);
  568. return cal.get(Calendar.MONTH);
  569. }
  570. /**
  571. * Sets the month to the given value, not changing the other fields.
  572. * @param month the month, zero based.
  573. * @deprecated Use Calendar instead of Date, and use
  574. * set(Calendar.MONTH, month) instead.
  575. */
  576. public void setMonth(int month)
  577. {
  578. Calendar cal = Calendar.getInstance();
  579. cal.setTimeInMillis(time);
  580. cal.set(Calendar.MONTH, month);
  581. time = cal.getTimeInMillis();
  582. }
  583. /**
  584. * @return the day of month represented by this date object.
  585. * @deprecated Use Calendar instead of Date, and use get(Calendar.DATE)
  586. * instead.
  587. */
  588. public int getDate()
  589. {
  590. Calendar cal = Calendar.getInstance();
  591. cal.setTimeInMillis(time);
  592. return cal.get(Calendar.DATE);
  593. }
  594. /**
  595. * Sets the date to the given value, not changing the other fields.
  596. * @param date the date.
  597. * @deprecated Use Calendar instead of Date, and use
  598. * set(Calendar.DATE, date) instead.
  599. */
  600. public void setDate(int date)
  601. {
  602. Calendar cal = Calendar.getInstance();
  603. cal.setTimeInMillis(time);
  604. cal.set(Calendar.DATE, date);
  605. time = cal.getTimeInMillis();
  606. }
  607. /**
  608. * @return the day represented by this date object.
  609. * @deprecated Use Calendar instead of Date, and use get(Calendar.DAY_OF_WEEK)
  610. * instead.
  611. */
  612. public int getDay()
  613. {
  614. Calendar cal = Calendar.getInstance();
  615. cal.setTimeInMillis(time);
  616. // For Calendar, Sunday is 1. For Date, Sunday is 0.
  617. return cal.get(Calendar.DAY_OF_WEEK) - 1;
  618. }
  619. /**
  620. * @return the hours represented by this date object.
  621. * @deprecated Use Calendar instead of Date, and use get(Calendar.HOUR_OF_DAY)
  622. * instead.
  623. */
  624. public int getHours()
  625. {
  626. Calendar cal = Calendar.getInstance();
  627. cal.setTimeInMillis(time);
  628. return cal.get(Calendar.HOUR_OF_DAY);
  629. }
  630. /**
  631. * Sets the hours to the given value, not changing the other fields.
  632. * @param hours the hours.
  633. * @deprecated Use Calendar instead of Date, and use
  634. * set(Calendar.HOUR_OF_DAY, hours) instead.
  635. */
  636. public void setHours(int hours)
  637. {
  638. Calendar cal = Calendar.getInstance();
  639. cal.setTimeInMillis(time);
  640. cal.set(Calendar.HOUR_OF_DAY, hours);
  641. time = cal.getTimeInMillis();
  642. }
  643. /**
  644. * @return the minutes represented by this date object.
  645. * @deprecated Use Calendar instead of Date, and use get(Calendar.MINUTE)
  646. * instead.
  647. */
  648. public int getMinutes()
  649. {
  650. Calendar cal = Calendar.getInstance();
  651. cal.setTimeInMillis(time);
  652. return cal.get(Calendar.MINUTE);
  653. }
  654. /**
  655. * Sets the minutes to the given value, not changing the other fields.
  656. * @param minutes the minutes.
  657. * @deprecated Use Calendar instead of Date, and use
  658. * set(Calendar.MINUTE, minutes) instead.
  659. */
  660. public void setMinutes(int minutes)
  661. {
  662. Calendar cal = Calendar.getInstance();
  663. cal.setTimeInMillis(time);
  664. cal.set(Calendar.MINUTE, minutes);
  665. time = cal.getTimeInMillis();
  666. }
  667. /**
  668. * @return the seconds represented by this date object.
  669. * @deprecated Use Calendar instead of Date, and use get(Calendar.SECOND)
  670. * instead.
  671. */
  672. public int getSeconds()
  673. {
  674. Calendar cal = Calendar.getInstance();
  675. cal.setTimeInMillis(time);
  676. return cal.get(Calendar.SECOND);
  677. }
  678. /**
  679. * Sets the seconds to the given value, not changing the other fields.
  680. * @param seconds the seconds.
  681. * @deprecated Use Calendar instead of Date, and use
  682. * set(Calendar.SECOND, seconds) instead.
  683. */
  684. public void setSeconds(int seconds)
  685. {
  686. Calendar cal = Calendar.getInstance();
  687. cal.setTimeInMillis(time);
  688. cal.set(Calendar.SECOND, seconds);
  689. time = cal.getTimeInMillis();
  690. }
  691. /**
  692. * Reads an Object from the stream.
  693. */
  694. private void readObject(java.io.ObjectInputStream input)
  695. throws java.io.IOException, ClassNotFoundException
  696. {
  697. input.defaultReadObject();
  698. time = input.readLong();
  699. }
  700. /**
  701. * Writes an Object to the stream.
  702. * @serialdata A long value representing the offset from the epoch
  703. * in milliseconds. This is the same value that is returned by the
  704. * method getTime().
  705. */
  706. private void writeObject(java.io.ObjectOutputStream output)
  707. throws java.io.IOException
  708. {
  709. output.defaultWriteObject();
  710. output.writeLong(time);
  711. }
  712. }