GregorianCalendar.java 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  1. /* java.util.GregorianCalendar
  2. Copyright (C) 1998, 1999, 2001, 2002 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 the Gregorian calendar, that is used in most
  34. * countries all over the world. It does also handle the Julian calendar
  35. * for dates smaller than the date of the change to the Gregorian calendar.
  36. * This change date is different from country to country, you can set it with
  37. * <code>setGregorianChange</code>
  38. *
  39. * The Gregorian calendar differs from the Julian calendar by a different
  40. * leap year rule (no leap year every 100 years, except if year is divisible
  41. * by 400). The non existing days that were omited when the change took
  42. * place are interpreted as gregorian date
  43. *
  44. * There are to eras available for the Gregorian calendar, namely BC and AD.
  45. *
  46. * @see Calendar
  47. * @see TimeZone
  48. */
  49. public class GregorianCalendar extends Calendar
  50. {
  51. /**
  52. * Constant representing the era BC (before Christ).
  53. */
  54. public static final int BC = 0;
  55. /**
  56. * Constant representing the era AD (Anno Domini).
  57. */
  58. public static final int AD = 1;
  59. /**
  60. * The point at which the Gregorian calendar rules were used.
  61. * This is locale dependent; the default for most catholic
  62. * countries is midnight (UTC) on October 5, 1582 (Julian),
  63. * or October 15, 1582 (Gregorian).
  64. */
  65. private long gregorianCutover;
  66. static final long serialVersionUID = -8125100834729963327L;
  67. /**
  68. * The name of the resource bundle.
  69. */
  70. private static final String bundleName = "gnu.java.locale.Calendar";
  71. /**
  72. * Constructs a new GregorianCalender representing the current
  73. * time, using the default time zone and the default locale.
  74. */
  75. public GregorianCalendar()
  76. {
  77. this(TimeZone.getDefault(), Locale.getDefault());
  78. }
  79. /**
  80. * Constructs a new GregorianCalender representing the current
  81. * time, using the specified time zone and the default locale.
  82. * @param zone a time zone.
  83. */
  84. public GregorianCalendar(TimeZone zone)
  85. {
  86. this(zone, Locale.getDefault());
  87. }
  88. /**
  89. * Constructs a new GregorianCalender representing the current
  90. * time, using the default time zone and the specified locale.
  91. * @param locale a locale.
  92. */
  93. public GregorianCalendar(Locale locale)
  94. {
  95. this(TimeZone.getDefault(), locale);
  96. }
  97. /**
  98. * Constructs a new GregorianCalender representing the current
  99. * time with the given time zone and the given locale.
  100. * @param zone a time zone.
  101. * @param locale a locale.
  102. */
  103. public GregorianCalendar(TimeZone zone, Locale locale)
  104. {
  105. super(zone, locale);
  106. ResourceBundle rb = ResourceBundle.getBundle(bundleName, locale);
  107. gregorianCutover = ((Date) rb.getObject("gregorianCutOver")).getTime();
  108. setTimeInMillis(System.currentTimeMillis());
  109. }
  110. /**
  111. * Constructs a new GregorianCalendar representing midnight on the
  112. * given date with the default time zone and locale.
  113. * @param year corresponds to the YEAR time field.
  114. * @param month corresponds to the MONTH time field.
  115. * @param day corresponds to the DAY time field.
  116. */
  117. public GregorianCalendar(int year, int month, int day)
  118. {
  119. super();
  120. set(year, month, day);
  121. }
  122. /**
  123. * Constructs a new GregorianCalendar representing midnight on the
  124. * given date with the default time zone and locale.
  125. * @param year corresponds to the YEAR time field.
  126. * @param month corresponds to the MONTH time field.
  127. * @param day corresponds to the DAY time field.
  128. * @param hour corresponds to the HOUR_OF_DAY time field.
  129. * @param minute corresponds to the MINUTE time field.
  130. */
  131. public GregorianCalendar(int year, int month, int day, int hour, int minute)
  132. {
  133. super();
  134. set(year, month, day, hour, minute);
  135. }
  136. /**
  137. * Constructs a new GregorianCalendar representing midnight on the
  138. * given date with the default time zone and locale.
  139. * @param year corresponds to the YEAR time field.
  140. * @param month corresponds to the MONTH time field.
  141. * @param day corresponds to the DAY time field.
  142. * @param hour corresponds to the HOUR_OF_DAY time field.
  143. * @param minute corresponds to the MINUTE time field.
  144. * @param second corresponds to the SECOND time field.
  145. */
  146. public GregorianCalendar(int year, int month, int day,
  147. int hour, int minute, int second)
  148. {
  149. super();
  150. set(year, month, day, hour, minute, second);
  151. }
  152. /**
  153. * Sets the date of the switch from Julian dates to Gregorian dates.
  154. * You can use <code>new Date(Long.MAX_VALUE)</code> to use a pure
  155. * Julian calendar, or <code>Long.MIN_VALUE</code> for a pure Gregorian
  156. * calendar.
  157. * @param date the date of the change.
  158. */
  159. public void setGregorianChange(Date date)
  160. {
  161. gregorianCutover = date.getTime();
  162. }
  163. /**
  164. * Gets the date of the switch from Julian dates to Gregorian dates.
  165. * @return the date of the change.
  166. */
  167. public final Date getGregorianChange()
  168. {
  169. return new Date(gregorianCutover);
  170. }
  171. /**
  172. * Determines if the given year is a leap year. The result is
  173. * undefined if the gregorian change took place in 1800, so that
  174. * the end of february is skiped and you give that year
  175. * (well...).<br>
  176. *
  177. * The year should be positive and you can't give an ERA. But
  178. * remember that before 4 BC there wasn't a consistent leap year
  179. * rule, so who cares.
  180. *
  181. * @param year a year use nonnegative value for BC.
  182. * @return true, if the given year is a leap year, false otherwise. */
  183. public boolean isLeapYear(int year)
  184. {
  185. if ((year & 3) != 0)
  186. // Only years divisible by 4 can be leap years
  187. return false;
  188. // compute the linear day of the 29. February of that year.
  189. // The 13 is the number of days, that were omitted in the Gregorian
  190. // Calender until the epoch.
  191. int julianDay = (((year-1) * (365*4+1)) >> 2) + (31+29 -
  192. (((1970-1) * (365*4+1)) / 4 + 1 - 13));
  193. // If that day is smaller than the gregorianChange the julian
  194. // rule applies: This is a leap year since it is divisible by 4.
  195. if (julianDay * (24 * 60 * 60 * 1000L) < gregorianCutover)
  196. return true;
  197. return ((year % 100) != 0 || (year % 400) == 0);
  198. }
  199. /**
  200. * Get the linear time in milliseconds since the epoch. If you
  201. * specify a nonpositive year it is interpreted as BC as
  202. * following: 0 is 1 BC, -1 is 2 BC and so on. The date is
  203. * interpreted as gregorian if the change occurred before that date.
  204. *
  205. * @param year the year of the date.
  206. * @param dayOfYear the day of year of the date; 1 based.
  207. * @param millis the millisecond in that day.
  208. * @return the days since the epoch, may be negative. */
  209. private long getLinearTime(int year, int dayOfYear, int millis)
  210. {
  211. // The 13 is the number of days, that were omitted in the Gregorian
  212. // Calender until the epoch.
  213. // We shift right by 2 instead of dividing by 4, to get correct
  214. // results for negative years (and this is even more efficient).
  215. int julianDay = ((year * (365 * 4 + 1)) >> 2) + dayOfYear -
  216. ((1970 * (365 * 4 + 1)) / 4 + 1 - 13);
  217. long time = julianDay * (24 * 60 * 60 * 1000L) + millis;
  218. if (time >= gregorianCutover)
  219. {
  220. // subtract the days that are missing in gregorian calendar
  221. // with respect to julian calendar.
  222. //
  223. // Okay, here we rely on the fact that the gregorian
  224. // calendar was introduced in the AD era. This doesn't work
  225. // with negative years.
  226. //
  227. // The additional leap year factor accounts for the fact that
  228. // a leap day is not seen on Jan 1 of the leap year.
  229. int gregOffset = (year / 400) - (year / 100) + 2;
  230. if (isLeapYear (year, true) && dayOfYear < 31 + 29)
  231. --gregOffset;
  232. time += gregOffset * (24 * 60 * 60 * 1000L);
  233. }
  234. return time;
  235. }
  236. private int getWeekDay(int year, int dayOfYear)
  237. {
  238. int day =
  239. (int) (getLinearTime(year, dayOfYear, 0) / (24 * 60 * 60 * 1000L));
  240. // The epoch was a thursday.
  241. int weekday = (day + THURSDAY) % 7;
  242. if (weekday <= 0)
  243. weekday += 7;
  244. return weekday;
  245. }
  246. /**
  247. * Calculate the dayOfYear from the fields array.
  248. * The relativeDays is used, to account for weeks that begin before
  249. * the gregorian change and end after it.<br>
  250. *
  251. * We return two values, the first is used to determine, if we
  252. * should use Gregorian calendar or Julian calendar, in case of
  253. * the change year, the second is a relative day after the given
  254. * day. This is necessary for week calculation in the year in
  255. * which gregorian change occurs. <br>
  256. *
  257. * @param year the year, negative for BC.
  258. * @return an array of two int values, the first containing a reference
  259. * day of current year, the second a relative count since this reference
  260. * day. */
  261. private int[] getDayOfYear(int year)
  262. {
  263. if (isSet[MONTH])
  264. {
  265. int dayOfYear;
  266. if (fields[MONTH] > FEBRUARY)
  267. {
  268. // The months after February are regular:
  269. // 9 is an offset found by try and error.
  270. dayOfYear = (fields[MONTH] * (31 + 30 + 31 + 30 + 31) - 9) / 5;
  271. if (isLeapYear(year))
  272. dayOfYear++;
  273. }
  274. else
  275. dayOfYear = 31 * fields[MONTH];
  276. if (isSet[DAY_OF_MONTH])
  277. {
  278. return new int[]
  279. {
  280. dayOfYear + fields[DAY_OF_MONTH], 0};
  281. }
  282. if (isSet[WEEK_OF_MONTH] && isSet[DAY_OF_WEEK])
  283. {
  284. // the weekday of the first day in that month is:
  285. int weekday = getWeekDay(year, ++dayOfYear);
  286. return new int[]
  287. {
  288. dayOfYear,
  289. // the day of week in the first week
  290. // (weeks starting on sunday) is:
  291. fields[DAY_OF_WEEK] - weekday +
  292. // Now jump to the right week and correct the possible
  293. // error made by assuming sunday is the first week day.
  294. 7 * (fields[WEEK_OF_MONTH]
  295. + (fields[DAY_OF_WEEK] < getFirstDayOfWeek()? 0 : -1)
  296. + (weekday < getFirstDayOfWeek()? -1 : 0))};
  297. }
  298. if (isSet[DAY_OF_WEEK] && isSet[DAY_OF_WEEK_IN_MONTH])
  299. {
  300. // the weekday of the first day in that month is:
  301. int weekday = getWeekDay(year, ++dayOfYear);
  302. return new int[] {
  303. dayOfYear,
  304. fields[DAY_OF_WEEK] - weekday +
  305. 7 * (fields[DAY_OF_WEEK_IN_MONTH]
  306. + (fields[DAY_OF_WEEK] < weekday ? 0 : -1))};
  307. }
  308. }
  309. // MONTH + something did not succeed.
  310. if (isSet[DAY_OF_YEAR])
  311. {
  312. return new int[] {0, fields[DAY_OF_YEAR]};
  313. }
  314. if (isSet[DAY_OF_WEEK] && isSet[WEEK_OF_YEAR])
  315. {
  316. int dayOfYear = getMinimalDaysInFirstWeek();
  317. // the weekday of the day, that begins the first week
  318. // in that year is:
  319. int weekday = getWeekDay(year, dayOfYear);
  320. return new int[] {
  321. dayOfYear,
  322. // the day of week in the first week
  323. // (weeks starting on sunday) is:
  324. fields[DAY_OF_WEEK] - weekday
  325. // Now jump to the right week and correct the possible
  326. // error made by assuming sunday is the first week day.
  327. + 7 * (fields[WEEK_OF_YEAR]
  328. + (fields[DAY_OF_WEEK] < getFirstDayOfWeek()? 0 : -1)
  329. + (weekday < getFirstDayOfWeek()? -1 : 0))};
  330. }
  331. // As last resort return Jan, 1st.
  332. return new int[] {1, 0};
  333. }
  334. /**
  335. * Converts the time field values (<code>fields</code>) to
  336. * milliseconds since the epoch UTC (<code>time</code>).
  337. */
  338. protected synchronized void computeTime()
  339. {
  340. int era = isSet[ERA] ? fields[ERA] : AD;
  341. int year = isSet[YEAR] ? fields[YEAR] : 1970;
  342. if (era == BC)
  343. year = 1 - year;
  344. int[] daysOfYear = getDayOfYear(year);
  345. int hour = 0;
  346. if (isSet[HOUR_OF_DAY])
  347. hour = fields[HOUR_OF_DAY];
  348. else if (isSet[HOUR])
  349. {
  350. hour = fields[HOUR];
  351. if (isSet[AM_PM] && fields[AM_PM] == PM)
  352. hour += 12;
  353. }
  354. int minute = isSet[MINUTE] ? fields[MINUTE] : 0;
  355. int second = isSet[SECOND] ? fields[SECOND] : 0;
  356. int millis = isSet[MILLISECOND] ? fields[MILLISECOND] : 0;
  357. int millisInDay;
  358. if (isLenient())
  359. {
  360. // prevent overflow
  361. long allMillis = (((hour * 60L) + minute) * 60L + second) * 1000L
  362. + millis;
  363. daysOfYear[1] += allMillis / (24 * 60 * 60 * 1000L);
  364. millisInDay = (int) (allMillis % (24 * 60 * 60 * 1000L));
  365. }
  366. else
  367. {
  368. if (hour < 0 || hour >= 24 || minute < 0 || minute > 59
  369. || second < 0 || second > 59 || millis < 0 || millis >= 1000)
  370. throw new IllegalArgumentException();
  371. millisInDay = (((hour * 60) + minute) * 60 + second) * 1000 + millis;
  372. }
  373. time = getLinearTime(year, daysOfYear[0], millisInDay);
  374. // Add the relative days after calculating the linear time, to
  375. // get right behaviour when jumping over the gregorianCutover.
  376. time += daysOfYear[1] * (24 * 60 * 60 * 1000L);
  377. TimeZone zone = getTimeZone();
  378. int rawOffset = isSet[ZONE_OFFSET]
  379. ? fields[ZONE_OFFSET] : zone.getRawOffset();
  380. int dayOfYear = daysOfYear[0] + daysOfYear[1];
  381. int month = (dayOfYear * 5 + 3) / (31 + 30 + 31 + 30 + 31);
  382. int day = (6 + (dayOfYear * 5 + 3) % (31 + 30 + 31 + 30 + 31)) / 5;
  383. int weekday = ((int) (time / (24 * 60 * 60 * 1000L)) + THURSDAY) % 7;
  384. if (weekday <= 0)
  385. weekday += 7;
  386. int dstOffset = isSet[DST_OFFSET]
  387. ? fields[DST_OFFSET] : (zone.getOffset((year < 0) ? BC : AD,
  388. (year < 0) ? 1 - year : year,
  389. month, day, weekday, millisInDay)
  390. - zone.getRawOffset());
  391. time -= rawOffset + dstOffset;
  392. isTimeSet = true;
  393. }
  394. /**
  395. * Determines if the given year is a leap year.
  396. *
  397. * The year should be positive and you can't give an ERA. But
  398. * remember that before 4 BC there wasn't a consistent leap year
  399. * rule, so who cares.
  400. *
  401. * @param year a year use nonnegative value for BC.
  402. * @param gregorian if true, use gregorian leap year rule.
  403. * @return true, if the given year is a leap year, false otherwise. */
  404. private boolean isLeapYear(int year, boolean gregorian)
  405. {
  406. if ((year & 3) != 0)
  407. // Only years divisible by 4 can be leap years
  408. return false;
  409. if (!gregorian)
  410. return true;
  411. // We rely on AD area here.
  412. return ((year % 100) != 0 || (year % 400) == 0);
  413. }
  414. /**
  415. * Get the linear day in days since the epoch, using the
  416. * Julian or Gregorian calendar as specified. If you specify a
  417. * nonpositive year it is interpreted as BC as following: 0 is 1
  418. * BC, -1 is 2 BC and so on.
  419. *
  420. * @param year the year of the date.
  421. * @param dayOfYear the day of year of the date; 1 based.
  422. * @param gregorian True, if we should use Gregorian rules.
  423. * @return the days since the epoch, may be negative. */
  424. private int getLinearDay(int year, int dayOfYear, boolean gregorian)
  425. {
  426. // The 13 is the number of days, that were omitted in the Gregorian
  427. // Calender until the epoch.
  428. // We shift right by 2 instead of dividing by 4, to get correct
  429. // results for negative years (and this is even more efficient).
  430. int julianDay = ((year * (365 * 4 + 1)) >> 2) + dayOfYear -
  431. ((1970 * (365 * 4 + 1)) / 4 + 1 - 13);
  432. if (gregorian)
  433. {
  434. // subtract the days that are missing in gregorian calendar
  435. // with respect to julian calendar.
  436. //
  437. // Okay, here we rely on the fact that the gregorian
  438. // calendar was introduced in the AD era. This doesn't work
  439. // with negative years.
  440. //
  441. // The additional leap year factor accounts for the fact that
  442. // a leap day is not seen on Jan 1 of the leap year.
  443. int gregOffset = (year / 400) - (year / 100) + 2;
  444. if (isLeapYear (year, true) && dayOfYear < 31 + 29)
  445. --gregOffset;
  446. julianDay += gregOffset;
  447. }
  448. return julianDay;
  449. }
  450. /**
  451. * Converts the given linear day into era, year, month,
  452. * day_of_year, day_of_month, day_of_week, and writes the result
  453. * into the fields array.
  454. * @param day the linear day.
  455. */
  456. private void calculateDay(int day, boolean gregorian)
  457. {
  458. // the epoch is a Thursday.
  459. int weekday = (day + THURSDAY) % 7;
  460. if (weekday <= 0)
  461. weekday += 7;
  462. fields[DAY_OF_WEEK] = weekday;
  463. // get a first approximation of the year. This may be one
  464. // year to big.
  465. int year = 1970 + (gregorian
  466. ? ((day - 100) * 400) / (365 * 400 + 100 - 4 + 1)
  467. : ((day - 100) * 4) / (365 * 4 + 1));
  468. if (day >= 0)
  469. year++;
  470. int firstDayOfYear = getLinearDay(year, 1, gregorian);
  471. // Now look in which year day really lies.
  472. if (day < firstDayOfYear)
  473. {
  474. year--;
  475. firstDayOfYear = getLinearDay(year, 1, gregorian);
  476. }
  477. day -= firstDayOfYear - 1; // day of year, one based.
  478. fields[DAY_OF_YEAR] = day;
  479. if (year <= 0)
  480. {
  481. fields[ERA] = BC;
  482. fields[YEAR] = 1 - year;
  483. }
  484. else
  485. {
  486. fields[ERA] = AD;
  487. fields[YEAR] = year;
  488. }
  489. int leapday = isLeapYear(year, gregorian) ? 1 : 0;
  490. if (day <= 31 + 28 + leapday)
  491. {
  492. fields[MONTH] = day / 32; // 31->JANUARY, 32->FEBRUARY
  493. fields[DAY_OF_MONTH] = day - 31 * fields[MONTH];
  494. }
  495. else
  496. {
  497. // A few more magic formulas
  498. int scaledDay = (day - leapday) * 5 + 8;
  499. fields[MONTH] = scaledDay / (31 + 30 + 31 + 30 + 31);
  500. fields[DAY_OF_MONTH] = (scaledDay % (31 + 30 + 31 + 30 + 31)) / 5 + 1;
  501. }
  502. }
  503. /**
  504. * Converts the milliseconds since the epoch UTC
  505. * (<code>time</code>) to time fields
  506. * (<code>fields</code>).
  507. */
  508. protected synchronized void computeFields()
  509. {
  510. boolean gregorian = (time >= gregorianCutover);
  511. TimeZone zone = getTimeZone();
  512. fields[ZONE_OFFSET] = zone.getRawOffset();
  513. long localTime = time + fields[ZONE_OFFSET];
  514. int day = (int) (localTime / (24 * 60 * 60 * 1000L));
  515. int millisInDay = (int) (localTime % (24 * 60 * 60 * 1000L));
  516. if (millisInDay < 0)
  517. {
  518. millisInDay += (24 * 60 * 60 * 1000);
  519. day--;
  520. }
  521. calculateDay(day, gregorian);
  522. fields[DST_OFFSET] =
  523. zone.getOffset(fields[ERA], fields[YEAR], fields[MONTH],
  524. fields[DAY_OF_MONTH], fields[DAY_OF_WEEK],
  525. millisInDay) - fields[ZONE_OFFSET];
  526. millisInDay += fields[DST_OFFSET];
  527. if (millisInDay >= 24 * 60 * 60 * 1000)
  528. {
  529. millisInDay -= 24 * 60 * 60 * 1000;
  530. calculateDay(++day, gregorian);
  531. }
  532. fields[DAY_OF_WEEK_IN_MONTH] = (fields[DAY_OF_MONTH] + 6) / 7;
  533. // which day of the week are we (0..6), relative to getFirstDayOfWeek
  534. int relativeWeekday = (7 + fields[DAY_OF_WEEK] - getFirstDayOfWeek()) % 7;
  535. fields[WEEK_OF_MONTH] = (fields[DAY_OF_MONTH] - relativeWeekday + 6) / 7;
  536. int weekOfYear = (fields[DAY_OF_YEAR] - relativeWeekday + 6) / 7;
  537. // Do the Correction: getMinimalDaysInFirstWeek() is always in the
  538. // first week.
  539. int minDays = getMinimalDaysInFirstWeek();
  540. int firstWeekday =
  541. (7 + getWeekDay(fields[YEAR], minDays) - getFirstDayOfWeek()) % 7;
  542. if (minDays - firstWeekday < 1)
  543. weekOfYear++;
  544. fields[WEEK_OF_YEAR] = weekOfYear;
  545. int hourOfDay = millisInDay / (60 * 60 * 1000);
  546. fields[AM_PM] = (hourOfDay < 12) ? AM : PM;
  547. int hour = hourOfDay % 12;
  548. fields[HOUR] = (hour == 0) ? 12 : hour;
  549. fields[HOUR_OF_DAY] = hourOfDay;
  550. millisInDay %= (60 * 60 * 1000);
  551. fields[MINUTE] = millisInDay / (60 * 1000);
  552. millisInDay %= (60 * 1000);
  553. fields[SECOND] = millisInDay / (1000);
  554. fields[MILLISECOND] = millisInDay % 1000;
  555. areFieldsSet = isSet[ERA] = isSet[YEAR] = isSet[MONTH] =
  556. isSet[WEEK_OF_YEAR] = isSet[WEEK_OF_MONTH] =
  557. isSet[DAY_OF_MONTH] = isSet[DAY_OF_YEAR] = isSet[DAY_OF_WEEK] =
  558. isSet[DAY_OF_WEEK_IN_MONTH] = isSet[AM_PM] = isSet[HOUR] =
  559. isSet[HOUR_OF_DAY] = isSet[MINUTE] = isSet[SECOND] =
  560. isSet[MILLISECOND] = isSet[ZONE_OFFSET] = isSet[DST_OFFSET] = true;
  561. }
  562. /**
  563. * Compares the given calender with this.
  564. * @param o the object to that we should compare.
  565. * @return true, if the given object is a calendar, that represents
  566. * the same time (but doesn't necessary have the same fields).
  567. * @XXX Should we check if time zones, locale, cutover etc. are equal?
  568. */
  569. public boolean equals(Object o)
  570. {
  571. if (!(o instanceof GregorianCalendar))
  572. return false;
  573. GregorianCalendar cal = (GregorianCalendar) o;
  574. return (cal.getTimeInMillis() == getTimeInMillis());
  575. }
  576. // /**
  577. // * Compares the given calender with this.
  578. // * @param o the object to that we should compare.
  579. // * @return true, if the given object is a calendar, and this calendar
  580. // * represents a smaller time than the calender o.
  581. // */
  582. // public boolean before(Object o) {
  583. // if (!(o instanceof GregorianCalendar))
  584. // return false;
  585. // GregorianCalendar cal = (GregorianCalendar) o;
  586. // return (cal.getTimeInMillis() < getTimeInMillis());
  587. // }
  588. // /**
  589. // * Compares the given calender with this.
  590. // * @param o the object to that we should compare.
  591. // * @return true, if the given object is a calendar, and this calendar
  592. // * represents a bigger time than the calender o.
  593. // */
  594. // public boolean after(Object o) {
  595. // if (!(o instanceof GregorianCalendar))
  596. // return false;
  597. // GregorianCalendar cal = (GregorianCalendar) o;
  598. // return (cal.getTimeInMillis() > getTimeInMillis());
  599. // }
  600. /**
  601. * Adds the specified amount of time to the given time field. The
  602. * amount may be negative to subtract the time. If the field overflows
  603. * it does what you expect: Jan, 25 + 10 Days is Feb, 4.
  604. * @param field the time field. One of the time field constants.
  605. * @param amount the amount of time.
  606. */
  607. public void add(int field, int amount)
  608. {
  609. switch (field)
  610. {
  611. case YEAR:
  612. complete();
  613. fields[YEAR] += amount;
  614. isTimeSet = false;
  615. break;
  616. case MONTH:
  617. complete();
  618. int months = fields[MONTH] + amount;
  619. fields[YEAR] += months / 12;
  620. fields[MONTH] = months % 12;
  621. if (fields[MONTH] < 0)
  622. {
  623. fields[MONTH] += 12;
  624. fields[YEAR]--;
  625. }
  626. isTimeSet = false;
  627. int maxDay = getActualMaximum(DAY_OF_MONTH);
  628. if (fields[DAY_OF_MONTH] > maxDay)
  629. {
  630. fields[DAY_OF_MONTH] = maxDay;
  631. isTimeSet = false;
  632. }
  633. break;
  634. case DAY_OF_MONTH:
  635. case DAY_OF_YEAR:
  636. case DAY_OF_WEEK:
  637. if (!isTimeSet)
  638. computeTime();
  639. time += amount * (24 * 60 * 60 * 1000L);
  640. areFieldsSet = false;
  641. break;
  642. case WEEK_OF_YEAR:
  643. case WEEK_OF_MONTH:
  644. case DAY_OF_WEEK_IN_MONTH:
  645. if (!isTimeSet)
  646. computeTime();
  647. time += amount * (7 * 24 * 60 * 60 * 1000L);
  648. areFieldsSet = false;
  649. break;
  650. case AM_PM:
  651. if (!isTimeSet)
  652. computeTime();
  653. time += amount * (12 * 60 * 60 * 1000L);
  654. areFieldsSet = false;
  655. break;
  656. case HOUR:
  657. case HOUR_OF_DAY:
  658. if (!isTimeSet)
  659. computeTime();
  660. time += amount * (60 * 60 * 1000L);
  661. areFieldsSet = false;
  662. break;
  663. case MINUTE:
  664. if (!isTimeSet)
  665. computeTime();
  666. time += amount * (60 * 1000L);
  667. areFieldsSet = false;
  668. break;
  669. case SECOND:
  670. if (!isTimeSet)
  671. computeTime();
  672. time += amount * (1000L);
  673. areFieldsSet = false;
  674. break;
  675. case MILLISECOND:
  676. if (!isTimeSet)
  677. computeTime();
  678. time += amount;
  679. areFieldsSet = false;
  680. break;
  681. case ZONE_OFFSET:
  682. complete();
  683. fields[ZONE_OFFSET] += amount;
  684. time -= amount;
  685. break;
  686. case DST_OFFSET:
  687. complete();
  688. fields[DST_OFFSET] += amount;
  689. isTimeSet = false;
  690. break;
  691. default:
  692. throw new IllegalArgumentException
  693. ("Unknown Calendar field: " + field);
  694. }
  695. }
  696. /**
  697. * Rolls the specified time field up or down. This means add one
  698. * to the specified field, but don't change the other fields. If
  699. * the maximum for this field is reached, start over with the
  700. * minimum value.
  701. *
  702. * <strong>Note:</strong> There may be situation, where the other
  703. * fields must be changed, e.g rolling the month on May, 31.
  704. * The date June, 31 is automatically converted to July, 1.
  705. * This requires lenient settings.
  706. *
  707. * @param field the time field. One of the time field constants.
  708. * @param up the direction, true for up, false for down.
  709. */
  710. public void roll(int field, boolean up)
  711. {
  712. roll(field, up ? 1 : -1);
  713. }
  714. private void cleanUpAfterRoll(int field, int delta)
  715. {
  716. switch (field)
  717. {
  718. case ERA:
  719. case YEAR:
  720. case MONTH:
  721. // check that day of month is still in correct range
  722. if (fields[DAY_OF_MONTH] > getActualMaximum(DAY_OF_MONTH))
  723. fields[DAY_OF_MONTH] = getActualMaximum(DAY_OF_MONTH);
  724. isTimeSet = false;
  725. isSet[WEEK_OF_MONTH] = false;
  726. isSet[DAY_OF_WEEK] = false;
  727. isSet[DAY_OF_WEEK_IN_MONTH] = false;
  728. isSet[DAY_OF_YEAR] = false;
  729. isSet[WEEK_OF_YEAR] = false;
  730. break;
  731. case DAY_OF_MONTH:
  732. isSet[WEEK_OF_MONTH] = false;
  733. isSet[DAY_OF_WEEK] = false;
  734. isSet[DAY_OF_WEEK_IN_MONTH] = false;
  735. isSet[DAY_OF_YEAR] = false;
  736. isSet[WEEK_OF_YEAR] = false;
  737. time += delta * (24 * 60 * 60 * 1000L);
  738. break;
  739. case WEEK_OF_MONTH:
  740. isSet[DAY_OF_MONTH] = false;
  741. isSet[DAY_OF_WEEK_IN_MONTH] = false;
  742. isSet[DAY_OF_YEAR] = false;
  743. isSet[WEEK_OF_YEAR] = false;
  744. time += delta * (7 * 24 * 60 * 60 * 1000L);
  745. break;
  746. case DAY_OF_WEEK_IN_MONTH:
  747. isSet[DAY_OF_MONTH] = false;
  748. isSet[WEEK_OF_MONTH] = false;
  749. isSet[DAY_OF_YEAR] = false;
  750. isSet[WEEK_OF_YEAR] = false;
  751. time += delta * (7 * 24 * 60 * 60 * 1000L);
  752. break;
  753. case DAY_OF_YEAR:
  754. isSet[MONTH] = false;
  755. isSet[DAY_OF_MONTH] = false;
  756. isSet[WEEK_OF_MONTH] = false;
  757. isSet[DAY_OF_WEEK_IN_MONTH] = false;
  758. isSet[DAY_OF_WEEK] = false;
  759. isSet[WEEK_OF_YEAR] = false;
  760. time += delta * (24 * 60 * 60 * 1000L);
  761. break;
  762. case WEEK_OF_YEAR:
  763. isSet[MONTH] = false;
  764. isSet[DAY_OF_MONTH] = false;
  765. isSet[WEEK_OF_MONTH] = false;
  766. isSet[DAY_OF_WEEK_IN_MONTH] = false;
  767. isSet[DAY_OF_YEAR] = false;
  768. time += delta * (7 * 24 * 60 * 60 * 1000L);
  769. break;
  770. case AM_PM:
  771. isSet[HOUR_OF_DAY] = false;
  772. time += delta * (12 * 60 * 60 * 1000L);
  773. break;
  774. case HOUR:
  775. isSet[HOUR_OF_DAY] = false;
  776. time += delta * (60 * 60 * 1000L);
  777. break;
  778. case HOUR_OF_DAY:
  779. isSet[HOUR] = false;
  780. isSet[AM_PM] = false;
  781. time += delta * (60 * 60 * 1000L);
  782. break;
  783. case MINUTE:
  784. time += delta * (60 * 1000L);
  785. break;
  786. case SECOND:
  787. time += delta * (1000L);
  788. break;
  789. case MILLISECOND:
  790. time += delta;
  791. break;
  792. }
  793. }
  794. /**
  795. * Rolls the specified time field by the given amount. This means
  796. * add amount to the specified field, but don't change the other
  797. * fields. If the maximum for this field is reached, start over
  798. * with the minimum value and vice versa for negative amounts.
  799. *
  800. * <strong>Note:</strong> There may be situation, where the other
  801. * fields must be changed, e.g rolling the month on May, 31.
  802. * The date June, 31 is automatically corrected to June, 30.
  803. *
  804. * @param field the time field. One of the time field constants.
  805. * @param amount the amount by which we should roll.
  806. */
  807. public void roll(int field, int amount)
  808. {
  809. switch (field)
  810. {
  811. case DAY_OF_WEEK:
  812. // day of week is special: it rolls automatically
  813. add(field, amount);
  814. return;
  815. case ZONE_OFFSET:
  816. case DST_OFFSET:
  817. throw new IllegalArgumentException("Can't roll time zone");
  818. }
  819. complete();
  820. int min = getActualMinimum(field);
  821. int range = getActualMaximum(field) - min + 1;
  822. int oldval = fields[field];
  823. int newval = (oldval - min + range + amount) % range + min;
  824. if (newval < min)
  825. newval += range;
  826. fields[field] = newval;
  827. cleanUpAfterRoll(field, newval - oldval);
  828. }
  829. private static final int[] minimums =
  830. { BC, 1, 0, 0, 1, 1, 1, SUNDAY, 1,
  831. AM, 1, 0, 1, 1, 1, -(12*60*60*1000), 0 };
  832. private static final int[] maximums =
  833. { AD, 5000000, 11, 53, 5, 31, 366, SATURDAY, 5,
  834. PM, 12, 23, 59, 59, 999, +(12*60*60*1000), (12*60*60*1000) };
  835. /**
  836. * Gets the smallest value that is allowed for the specified field.
  837. * @param field the time field. One of the time field constants.
  838. * @return the smallest value.
  839. */
  840. public int getMinimum(int field)
  841. {
  842. return minimums[field];
  843. }
  844. /**
  845. * Gets the biggest value that is allowed for the specified field.
  846. * @param field the time field. One of the time field constants.
  847. * @return the biggest value.
  848. */
  849. public int getMaximum(int field)
  850. {
  851. return maximums[field];
  852. }
  853. /**
  854. * Gets the greatest minimum value that is allowed for the specified field.
  855. * @param field the time field. One of the time field constants.
  856. * @return the greatest minimum value.
  857. */
  858. public int getGreatestMinimum(int field)
  859. {
  860. if (field == WEEK_OF_YEAR)
  861. return 1;
  862. return minimums[field];
  863. }
  864. /**
  865. * Gets the smallest maximum value that is allowed for the
  866. * specified field. For example this is 28 for DAY_OF_MONTH.
  867. * @param field the time field. One of the time field constants.
  868. * @return the least maximum value.
  869. * @since jdk1.2
  870. */
  871. public int getLeastMaximum(int field)
  872. {
  873. switch (field)
  874. {
  875. case WEEK_OF_YEAR:
  876. return 52;
  877. case DAY_OF_MONTH:
  878. return 28;
  879. case DAY_OF_YEAR:
  880. return 365;
  881. case DAY_OF_WEEK_IN_MONTH:
  882. case WEEK_OF_MONTH:
  883. return 4;
  884. default:
  885. return maximums[field];
  886. }
  887. }
  888. /**
  889. * Gets the actual minimum value that is allowed for the specified field.
  890. * This value is dependent on the values of the other fields. Note that
  891. * this calls <code>complete()</code> if not enough fields are set. This
  892. * can have ugly side effects.
  893. * @param field the time field. One of the time field constants.
  894. * @return the actual minimum value.
  895. * @since jdk1.2
  896. */
  897. public int getActualMinimum(int field)
  898. {
  899. if (field == WEEK_OF_YEAR)
  900. {
  901. int min = getMinimalDaysInFirstWeek();
  902. if (min == 0)
  903. return 1;
  904. if (!areFieldsSet || !isSet[ERA] || !isSet[YEAR])
  905. complete();
  906. int year = fields[ERA] == AD ? fields[YEAR] : 1 - fields[YEAR];
  907. int weekday = getWeekDay(year, min);
  908. if ((7 + weekday - getFirstDayOfWeek()) % 7 >= min - 1)
  909. return 1;
  910. return 0;
  911. }
  912. return minimums[field];
  913. }
  914. /**
  915. * Gets the actual maximum value that is allowed for the specified field.
  916. * This value is dependent on the values of the other fields. Note that
  917. * this calls <code>complete()</code> if not enough fields are set. This
  918. * can have ugly side effects.
  919. * @param field the time field. One of the time field constants.
  920. * @return the actual maximum value.
  921. */
  922. public int getActualMaximum(int field)
  923. {
  924. switch (field)
  925. {
  926. case WEEK_OF_YEAR:
  927. {
  928. if (!areFieldsSet || !isSet[ERA] || !isSet[YEAR])
  929. complete();
  930. // This is wrong for the year that contains the gregorian change.
  931. // I.e it gives the weeks in the julian year or in the gregorian
  932. // year in that case.
  933. int year = fields[ERA] == AD ? fields[YEAR] : 1 - fields[YEAR];
  934. int lastDay = isLeapYear(year) ? 366 : 365;
  935. int weekday = getWeekDay(year, lastDay);
  936. int week = (lastDay + 6
  937. - (7 + weekday - getFirstDayOfWeek()) % 7) / 7;
  938. int minimalDays = getMinimalDaysInFirstWeek();
  939. int firstWeekday = getWeekDay(year, minimalDays);
  940. if (minimalDays - (7 + firstWeekday - getFirstDayOfWeek()) % 7 < 1)
  941. return week + 1;
  942. }
  943. case DAY_OF_MONTH:
  944. {
  945. if (!areFieldsSet || !isSet[MONTH])
  946. complete();
  947. int month = fields[MONTH];
  948. // If you change this, you should also change
  949. // SimpleTimeZone.getDaysInMonth();
  950. if (month == FEBRUARY)
  951. {
  952. if (!isSet[YEAR] || !isSet[ERA])
  953. complete();
  954. int year = fields[ERA] == AD ? fields[YEAR] : 1 - fields[YEAR];
  955. return isLeapYear(year) ? 29 : 28;
  956. }
  957. else if (month < AUGUST)
  958. return 31 - (month & 1);
  959. else
  960. return 30 + (month & 1);
  961. }
  962. case DAY_OF_YEAR:
  963. {
  964. if (!areFieldsSet || !isSet[ERA] || !isSet[YEAR])
  965. complete();
  966. int year = fields[ERA] == AD ? fields[YEAR] : 1 - fields[YEAR];
  967. return isLeapYear(year) ? 366 : 365;
  968. }
  969. case DAY_OF_WEEK_IN_MONTH:
  970. {
  971. // This is wrong for the month that contains the gregorian change.
  972. int daysInMonth = getActualMaximum(DAY_OF_MONTH);
  973. // That's black magic, I know
  974. return (daysInMonth - (fields[DAY_OF_MONTH] - 1) % 7 + 6) / 7;
  975. }
  976. case WEEK_OF_MONTH:
  977. {
  978. int daysInMonth = getActualMaximum(DAY_OF_MONTH);
  979. int weekday = (daysInMonth - fields[DAY_OF_MONTH]
  980. + fields[DAY_OF_WEEK] - SUNDAY) % 7 + SUNDAY;
  981. return (daysInMonth + 6
  982. - (7 + weekday - getFirstDayOfWeek()) % 7) / 7;
  983. }
  984. default:
  985. return maximums[field];
  986. }
  987. }
  988. }