GregorianCalendar.java 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366
  1. /* java.util.GregorianCalendar
  2. Copyright (C) 1998, 1999, 2001, 2002, 2003, 2004, 2007
  3. Free Software Foundation, Inc.
  4. This file is part of GNU Classpath.
  5. GNU Classpath is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU Classpath is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Classpath; see the file COPYING. If not, write to the
  15. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA.
  17. Linking this library statically or dynamically with other modules is
  18. making a combined work based on this library. Thus, the terms and
  19. conditions of the GNU General Public License cover the whole
  20. combination.
  21. As a special exception, the copyright holders of this library give you
  22. permission to link this library with independent modules to produce an
  23. executable, regardless of the license terms of these independent
  24. modules, and to copy and distribute the resulting executable under
  25. terms of your choice, provided that you also meet, for each linked
  26. independent module, the terms and conditions of the license of that
  27. module. An independent module is a module which is not derived from
  28. or based on this library. If you modify this library, you may extend
  29. this exception to your version of the library, but you are not
  30. obligated to do so. If you do not wish to do so, delete this
  31. exception statement from your version. */
  32. package java.util;
  33. /**
  34. * <p>
  35. * This class represents the Gregorian calendar, that is used in most
  36. * countries all over the world. It does also handle the Julian calendar
  37. * for dates smaller than the date of the change to the Gregorian calendar.
  38. * The Gregorian calendar differs from the Julian calendar by a different
  39. * leap year rule (no leap year every 100 years, except if year is divisible
  40. * by 400).
  41. * </p>
  42. * <p>
  43. * This change date is different from country to country, and can be changed with
  44. * <code>setGregorianChange</code>. The first countries to adopt the Gregorian
  45. * calendar did so on the 15th of October, 1582. This date followed October
  46. * the 4th, 1582 in the Julian calendar system. The non-existant days that were
  47. * omitted when the change took place are interpreted as Gregorian dates.
  48. * </p>
  49. * <p>
  50. * Prior to the changeover date, New Year's Day occurred on the 25th of March.
  51. * However, this class always takes New Year's Day as being the 1st of January.
  52. * Client code should manually adapt the year value, if required, for dates
  53. * between January the 1st and March the 24th in years prior to the changeover.
  54. * </p>
  55. * <p>
  56. * Any date infinitely forwards or backwards in time can be represented by
  57. * this class. A <em>proleptic</em> calendar system is used, which allows
  58. * future dates to be created via the existing rules. This allows meaningful
  59. * and consistent dates to be produced for all years. However, dates are only
  60. * historically accurate following March the 1st, 4AD when the Julian calendar
  61. * system was adopted. Prior to this, leap year rules were applied erraticly.
  62. * </p>
  63. * <p>
  64. * There are two eras available for the Gregorian calendar, namely BC and AD.
  65. * </p>
  66. * <p>
  67. * Weeks are defined as a period of seven days, beginning on the first day
  68. * of the week, as returned by <code>getFirstDayOfWeek()</code>, and ending
  69. * on the day prior to this.
  70. * </p>
  71. * <p>
  72. * The weeks of the year are numbered from 1 to a possible 53. The first week
  73. * of the year is defined as the first week that contains at least the minimum
  74. * number of days of the first week in the new year (retrieved via
  75. * <code>getMinimalDaysInFirstWeek()</code>). All weeks after this are numbered
  76. * from 2 onwards.
  77. * </p>
  78. * <p>
  79. * For example, take the year 2004. It began on a Thursday. The first week
  80. * of 2004 depends both on where a week begins and how long it must minimally
  81. * last. Let's say that the week begins on a Monday and must have a minimum
  82. * of 5 days. In this case, the first week begins on Monday, the 5th of January.
  83. * The first 4 days (Thursday to Sunday) are not eligible, as they are too few
  84. * to make up the minimum number of days of the first week which must be in
  85. * the new year. If the minimum was lowered to 4 days, then the first week
  86. * would instead begin on Monday, the 29th of December, 2003. This first week
  87. * has 4 of its days in the new year, and is now eligible.
  88. * </p>
  89. * <p>
  90. * The weeks of the month are numbered from 0 to a possible 6. The first week
  91. * of the month (numbered 1) is a set of days, prior to the first day of the week,
  92. * which number at least the minimum number of days in a week. Unlike the first
  93. * week of the year, the first week of the month only uses days from that particular
  94. * month. As a consequence, it may have a variable number of days (from the minimum
  95. * number required up to a full week of 7) and it need not start on the first day of
  96. * the week. It must, however, be following by the first day of the week, as this
  97. * marks the beginning of week 2. Any days of the month which occur prior to the
  98. * first week (because the first day of the week occurs before the minimum number
  99. * of days is met) are seen as week 0.
  100. * </p>
  101. * <p>
  102. * Again, we will take the example of the year 2004 to demonstrate this. September
  103. * 2004 begins on a Wednesday. Taking our first day of the week as Monday, and the
  104. * minimum length of the first week as 6, we find that week 1 runs from Monday,
  105. * the 6th of September to Sunday the 12th. Prior to the 6th, there are only
  106. * 5 days (Wednesday through to Sunday). This is too small a number to meet the
  107. * minimum, so these are classed as being days in week 0. Week 2 begins on the
  108. * 13th, and so on. This changes if we reduce the minimum to 5. In this case,
  109. * week 1 is a truncated week from Wednesday the 1st to Sunday the 5th, and week
  110. * 0 doesn't exist. The first seven day week is week 2, starting on the 6th.
  111. * </p>
  112. * <p>
  113. * On using the <code>clear()</code> method, the Gregorian calendar returns
  114. * to its default value of the 1st of January, 1970 AD 00:00:00 (the epoch).
  115. * The day of the week is set to the correct day for that particular time.
  116. * The day is also the first of the month, and the date is in week 0.
  117. * </p>
  118. *
  119. * @see Calendar
  120. * @see TimeZone
  121. * @see Calendar#getFirstDayOfWeek()
  122. * @see Calendar#getMinimalDaysInFirstWeek()
  123. */
  124. public class GregorianCalendar extends Calendar
  125. {
  126. /**
  127. * Constant representing the era BC (Before Christ).
  128. */
  129. public static final int BC = 0;
  130. /**
  131. * Constant representing the era AD (Anno Domini).
  132. */
  133. public static final int AD = 1;
  134. /**
  135. * The point at which the Gregorian calendar rules were used.
  136. * This may be changed by using setGregorianChange;
  137. * The default is midnight (UTC) on October 5, 1582 (Julian),
  138. * or October 15, 1582 (Gregorian).
  139. *
  140. * @serial the changeover point from the Julian calendar
  141. * system to the Gregorian.
  142. */
  143. private long gregorianCutover = (new Date((24 * 60 * 60 * 1000L) * (((1582 * (365 * 4
  144. + 1)) / 4
  145. + (java.util.Calendar.OCTOBER * (31
  146. + 30 + 31 + 30 + 31) - 9) / 5 + 5)
  147. - ((1970 * (365 * 4 + 1)) / 4 + 1
  148. - 13)))).getTime();
  149. /**
  150. * For compatability with Sun's JDK.
  151. */
  152. static final long serialVersionUID = -8125100834729963327L;
  153. /**
  154. * Days in the epoch. Relative Jan 1, year '0' which is not a leap year.
  155. * (although there is no year zero, this does not matter.)
  156. * This is consistent with the formula:
  157. * = (year-1)*365L + ((year-1) >> 2)
  158. *
  159. * Plus the gregorian correction:
  160. * Math.floor((year-1) / 400.) - Math.floor((year-1) / 100.);
  161. * For a correct julian date, the correction is -2 instead.
  162. *
  163. * The gregorian cutover in 1582 was 10 days, so by calculating the
  164. * correction from year zero, we have 15 non-leap days (even centuries)
  165. * minus 3 leap days (year 400,800,1200) = 12. Subtracting two corrects
  166. * this to the correct number 10.
  167. */
  168. private static final int EPOCH_DAYS = 719162;
  169. /**
  170. * Constructs a new GregorianCalender representing the current
  171. * time, using the default time zone and the default locale.
  172. */
  173. public GregorianCalendar()
  174. {
  175. this(TimeZone.getDefault(), Locale.getDefault());
  176. }
  177. /**
  178. * Constructs a new GregorianCalender representing the current
  179. * time, using the specified time zone and the default locale.
  180. *
  181. * @param zone a time zone.
  182. */
  183. public GregorianCalendar(TimeZone zone)
  184. {
  185. this(zone, Locale.getDefault());
  186. }
  187. /**
  188. * Constructs a new GregorianCalender representing the current
  189. * time, using the default time zone and the specified locale.
  190. *
  191. * @param locale a locale.
  192. */
  193. public GregorianCalendar(Locale locale)
  194. {
  195. this(TimeZone.getDefault(), locale);
  196. }
  197. /**
  198. * Constructs a new GregorianCalender representing the current
  199. * time with the given time zone and the given locale.
  200. *
  201. * @param zone a time zone.
  202. * @param locale a locale.
  203. */
  204. public GregorianCalendar(TimeZone zone, Locale locale)
  205. {
  206. this(zone, locale, false);
  207. setTimeInMillis(System.currentTimeMillis());
  208. }
  209. /**
  210. * Common constructor that all constructors should call.
  211. * @param zone a time zone.
  212. * @param locale a locale.
  213. * @param unused unused parameter to make the signature differ from
  214. * the public constructor (TimeZone, Locale).
  215. */
  216. private GregorianCalendar(TimeZone zone, Locale locale, boolean unused)
  217. {
  218. super(zone, locale);
  219. }
  220. /**
  221. * Constructs a new GregorianCalendar representing midnight on the
  222. * given date with the default time zone and locale.
  223. *
  224. * @param year corresponds to the YEAR time field.
  225. * @param month corresponds to the MONTH time field.
  226. * @param day corresponds to the DAY time field.
  227. */
  228. public GregorianCalendar(int year, int month, int day)
  229. {
  230. this(TimeZone.getDefault(), Locale.getDefault(), false);
  231. set(year, month, day);
  232. }
  233. /**
  234. * Constructs a new GregorianCalendar representing midnight on the
  235. * given date with the default time zone and locale.
  236. *
  237. * @param year corresponds to the YEAR time field.
  238. * @param month corresponds to the MONTH time field.
  239. * @param day corresponds to the DAY time field.
  240. * @param hour corresponds to the HOUR_OF_DAY time field.
  241. * @param minute corresponds to the MINUTE time field.
  242. */
  243. public GregorianCalendar(int year, int month, int day, int hour, int minute)
  244. {
  245. this(TimeZone.getDefault(), Locale.getDefault(), false);
  246. set(year, month, day, hour, minute);
  247. }
  248. /**
  249. * Constructs a new GregorianCalendar representing midnight on the
  250. * given date with the default time zone and locale.
  251. *
  252. * @param year corresponds to the YEAR time field.
  253. * @param month corresponds to the MONTH time field.
  254. * @param day corresponds to the DAY time field.
  255. * @param hour corresponds to the HOUR_OF_DAY time field.
  256. * @param minute corresponds to the MINUTE time field.
  257. * @param second corresponds to the SECOND time field.
  258. */
  259. public GregorianCalendar(int year, int month, int day, int hour, int minute,
  260. int second)
  261. {
  262. this(TimeZone.getDefault(), Locale.getDefault(), false);
  263. set(year, month, day, hour, minute, second);
  264. }
  265. /**
  266. * Sets the date of the switch from Julian dates to Gregorian dates.
  267. * You can use <code>new Date(Long.MAX_VALUE)</code> to use a pure
  268. * Julian calendar, or <code>Long.MIN_VALUE</code> for a pure Gregorian
  269. * calendar.
  270. *
  271. * @param date the date of the change.
  272. */
  273. public void setGregorianChange(Date date)
  274. {
  275. gregorianCutover = date.getTime();
  276. }
  277. /**
  278. * Gets the date of the switch from Julian dates to Gregorian dates.
  279. *
  280. * @return the date of the change.
  281. */
  282. public final Date getGregorianChange()
  283. {
  284. return new Date(gregorianCutover);
  285. }
  286. /**
  287. * <p>
  288. * Determines if the given year is a leap year. The result is
  289. * undefined if the Gregorian change took place in 1800, so that
  290. * the end of February is skipped, and that year is specified.
  291. * (well...).
  292. * </p>
  293. * <p>
  294. * To specify a year in the BC era, use a negative value calculated
  295. * as 1 - y, where y is the required year in BC. So, 1 BC is 0,
  296. * 2 BC is -1, 3 BC is -2, etc.
  297. * </p>
  298. *
  299. * @param year a year (use a negative value for BC).
  300. * @return true, if the given year is a leap year, false otherwise.
  301. */
  302. public boolean isLeapYear(int year)
  303. {
  304. // Only years divisible by 4 can be leap years
  305. if ((year & 3) != 0)
  306. return false;
  307. // Is the leap-day a Julian date? Then it's a leap year
  308. if (! isGregorian(year, 31 + 29 - 1))
  309. return true;
  310. // Apply gregorian rules otherwise
  311. return ((year % 100) != 0 || (year % 400) == 0);
  312. }
  313. /**
  314. * Retrieves the day of the week corresponding to the specified
  315. * day of the specified year.
  316. *
  317. * @param year the year in which the dayOfYear occurs.
  318. * @param dayOfYear the day of the year (an integer between 0 and
  319. * and 366)
  320. */
  321. private int getWeekDay(int year, int dayOfYear)
  322. {
  323. boolean greg = isGregorian(year, dayOfYear);
  324. int day = (int) getLinearDay(year, dayOfYear, greg);
  325. // The epoch was a thursday.
  326. int weekday = (day + THURSDAY) % 7;
  327. if (weekday <= 0)
  328. weekday += 7;
  329. return weekday;
  330. }
  331. /**
  332. * Returns the day of the week for the first day of a given month (0..11)
  333. */
  334. private int getFirstDayOfMonth(int year, int month)
  335. {
  336. int[] dayCount = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  337. if (month > 11)
  338. {
  339. year += (month / 12);
  340. month = month % 12;
  341. }
  342. if (month < 0)
  343. {
  344. year += (int) month / 12;
  345. month = month % 12;
  346. if (month < 0)
  347. {
  348. month += 12;
  349. year--;
  350. }
  351. }
  352. int dayOfYear = dayCount[month] + 1;
  353. if (month > 1)
  354. if (isLeapYear(year))
  355. dayOfYear++;
  356. boolean greg = isGregorian(year, dayOfYear);
  357. int day = (int) getLinearDay(year, dayOfYear, greg);
  358. // The epoch was a thursday.
  359. int weekday = (day + THURSDAY) % 7;
  360. if (weekday <= 0)
  361. weekday += 7;
  362. return weekday;
  363. }
  364. /**
  365. * Takes a year, and a (zero based) day of year and determines
  366. * if it is gregorian or not.
  367. */
  368. private boolean isGregorian(int year, int dayOfYear)
  369. {
  370. int relativeDay = (year - 1) * 365 + ((year - 1) >> 2) + dayOfYear
  371. - EPOCH_DAYS; // gregorian days from 1 to epoch.
  372. int gregFactor = (int) Math.floor((double) (year - 1) / 400.)
  373. - (int) Math.floor((double) (year - 1) / 100.);
  374. return ((relativeDay + gregFactor) * 60L * 60L * 24L * 1000L >= gregorianCutover);
  375. }
  376. /**
  377. * Check set fields for validity, without leniency.
  378. *
  379. * @throws IllegalArgumentException if a field is invalid
  380. */
  381. private void nonLeniencyCheck() throws IllegalArgumentException
  382. {
  383. int[] month_days = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  384. int year = fields[YEAR];
  385. int month = fields[MONTH];
  386. int leap = isLeapYear(year) ? 1 : 0;
  387. if (isSet[ERA] && fields[ERA] != AD && fields[ERA] != BC)
  388. throw new IllegalArgumentException("Illegal ERA.");
  389. if (isSet[YEAR] && fields[YEAR] < 1)
  390. throw new IllegalArgumentException("Illegal YEAR.");
  391. if (isSet[MONTH] && (month < 0 || month > 11))
  392. throw new IllegalArgumentException("Illegal MONTH.");
  393. if (isSet[WEEK_OF_YEAR])
  394. {
  395. int daysInYear = 365 + leap;
  396. daysInYear += (getFirstDayOfMonth(year, 0) - 1); // pad first week
  397. int last = getFirstDayOfMonth(year, 11) + 4;
  398. if (last > 7)
  399. last -= 7;
  400. daysInYear += 7 - last;
  401. int weeks = daysInYear / 7;
  402. if (fields[WEEK_OF_YEAR] < 1 || fields[WEEK_OF_YEAR] > weeks)
  403. throw new IllegalArgumentException("Illegal WEEK_OF_YEAR.");
  404. }
  405. if (isSet[WEEK_OF_MONTH])
  406. {
  407. int weeks = (month == 1 && leap == 0) ? 5 : 6;
  408. if (fields[WEEK_OF_MONTH] < 1 || fields[WEEK_OF_MONTH] > weeks)
  409. throw new IllegalArgumentException("Illegal WEEK_OF_MONTH.");
  410. }
  411. if (isSet[DAY_OF_MONTH])
  412. if (fields[DAY_OF_MONTH] < 1
  413. || fields[DAY_OF_MONTH] > month_days[month]
  414. + ((month == 1) ? leap : 0))
  415. throw new IllegalArgumentException("Illegal DAY_OF_MONTH.");
  416. if (isSet[DAY_OF_YEAR]
  417. && (fields[DAY_OF_YEAR] < 1 || fields[DAY_OF_YEAR] > 365 + leap))
  418. throw new IllegalArgumentException("Illegal DAY_OF_YEAR.");
  419. if (isSet[DAY_OF_WEEK]
  420. && (fields[DAY_OF_WEEK] < 1 || fields[DAY_OF_WEEK] > 7))
  421. throw new IllegalArgumentException("Illegal DAY_OF_WEEK.");
  422. if (isSet[DAY_OF_WEEK_IN_MONTH])
  423. {
  424. int weeks = (month == 1 && leap == 0) ? 4 : 5;
  425. if (fields[DAY_OF_WEEK_IN_MONTH] < -weeks
  426. || fields[DAY_OF_WEEK_IN_MONTH] > weeks)
  427. throw new IllegalArgumentException("Illegal DAY_OF_WEEK_IN_MONTH.");
  428. }
  429. if (isSet[AM_PM] && fields[AM_PM] != AM && fields[AM_PM] != PM)
  430. throw new IllegalArgumentException("Illegal AM_PM.");
  431. if (isSet[HOUR] && (fields[HOUR] < 0 || fields[HOUR] > 11))
  432. throw new IllegalArgumentException("Illegal HOUR.");
  433. if (isSet[HOUR_OF_DAY]
  434. && (fields[HOUR_OF_DAY] < 0 || fields[HOUR_OF_DAY] > 23))
  435. throw new IllegalArgumentException("Illegal HOUR_OF_DAY.");
  436. if (isSet[MINUTE] && (fields[MINUTE] < 0 || fields[MINUTE] > 59))
  437. throw new IllegalArgumentException("Illegal MINUTE.");
  438. if (isSet[SECOND] && (fields[SECOND] < 0 || fields[SECOND] > 59))
  439. throw new IllegalArgumentException("Illegal SECOND.");
  440. if (isSet[MILLISECOND]
  441. && (fields[MILLISECOND] < 0 || fields[MILLISECOND] > 999))
  442. throw new IllegalArgumentException("Illegal MILLISECOND.");
  443. if (isSet[ZONE_OFFSET]
  444. && (fields[ZONE_OFFSET] < -12 * 60 * 60 * 1000L
  445. || fields[ZONE_OFFSET] > 12 * 60 * 60 * 1000L))
  446. throw new IllegalArgumentException("Illegal ZONE_OFFSET.");
  447. if (isSet[DST_OFFSET]
  448. && (fields[DST_OFFSET] < -12 * 60 * 60 * 1000L
  449. || fields[DST_OFFSET] > 12 * 60 * 60 * 1000L))
  450. throw new IllegalArgumentException("Illegal DST_OFFSET.");
  451. }
  452. /**
  453. * Converts the time field values (<code>fields</code>) to
  454. * milliseconds since the epoch UTC (<code>time</code>).
  455. *
  456. * @throws IllegalArgumentException if any calendar fields
  457. * are invalid.
  458. */
  459. protected synchronized void computeTime()
  460. {
  461. int millisInDay = 0;
  462. int era = fields[ERA];
  463. int year = fields[YEAR];
  464. int month = fields[MONTH];
  465. int day = fields[DAY_OF_MONTH];
  466. int minute = fields[MINUTE];
  467. int second = fields[SECOND];
  468. int millis = fields[MILLISECOND];
  469. int[] month_days = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  470. int[] dayCount = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  471. int hour = 0;
  472. if (! isLenient())
  473. nonLeniencyCheck();
  474. if (! isSet[MONTH] && (! isSet[DAY_OF_WEEK] || isSet[WEEK_OF_YEAR]))
  475. {
  476. // 5: YEAR + DAY_OF_WEEK + WEEK_OF_YEAR
  477. if (isSet[WEEK_OF_YEAR])
  478. {
  479. int first = getFirstDayOfMonth(year, 0);
  480. int offs = 1;
  481. int daysInFirstWeek = getFirstDayOfWeek() - first;
  482. if (daysInFirstWeek <= 0)
  483. daysInFirstWeek += 7;
  484. if (daysInFirstWeek < getMinimalDaysInFirstWeek())
  485. offs += daysInFirstWeek;
  486. else
  487. offs -= 7 - daysInFirstWeek;
  488. month = 0;
  489. day = offs + 7 * (fields[WEEK_OF_YEAR] - 1);
  490. offs = fields[DAY_OF_WEEK] - getFirstDayOfWeek();
  491. if (offs < 0)
  492. offs += 7;
  493. day += offs;
  494. }
  495. else
  496. {
  497. // 4: YEAR + DAY_OF_YEAR
  498. month = 0;
  499. day = fields[DAY_OF_YEAR];
  500. }
  501. }
  502. else
  503. {
  504. if (isSet[DAY_OF_WEEK])
  505. {
  506. int first = getFirstDayOfMonth(year, month);
  507. // 3: YEAR + MONTH + DAY_OF_WEEK_IN_MONTH + DAY_OF_WEEK
  508. if (isSet[DAY_OF_WEEK_IN_MONTH])
  509. {
  510. if (fields[DAY_OF_WEEK_IN_MONTH] < 0)
  511. {
  512. month++;
  513. first = getFirstDayOfMonth(year, month);
  514. day = 1 + 7 * (fields[DAY_OF_WEEK_IN_MONTH]);
  515. }
  516. else
  517. day = 1 + 7 * (fields[DAY_OF_WEEK_IN_MONTH] - 1);
  518. int offs = fields[DAY_OF_WEEK] - first;
  519. if (offs < 0)
  520. offs += 7;
  521. day += offs;
  522. }
  523. else
  524. { // 2: YEAR + MONTH + WEEK_OF_MONTH + DAY_OF_WEEK
  525. int offs = 1;
  526. int daysInFirstWeek = getFirstDayOfWeek() - first;
  527. if (daysInFirstWeek <= 0)
  528. daysInFirstWeek += 7;
  529. if (daysInFirstWeek < getMinimalDaysInFirstWeek())
  530. offs += daysInFirstWeek;
  531. else
  532. offs -= 7 - daysInFirstWeek;
  533. day = offs + 7 * (fields[WEEK_OF_MONTH] - 1);
  534. offs = fields[DAY_OF_WEEK] - getFirstDayOfWeek();
  535. if (offs < 0)
  536. offs += 7;
  537. day += offs;
  538. }
  539. }
  540. // 1: YEAR + MONTH + DAY_OF_MONTH
  541. }
  542. if (era == BC && year > 0)
  543. year = 1 - year;
  544. // rest of code assumes day/month/year set
  545. // should negative BC years be AD?
  546. // get the hour (but no check for validity)
  547. if (isSet[HOUR])
  548. {
  549. hour = fields[HOUR];
  550. if (fields[AM_PM] == PM)
  551. hour += 12;
  552. }
  553. else
  554. hour = fields[HOUR_OF_DAY];
  555. // Read the era,year,month,day fields and convert as appropriate.
  556. // Calculate number of milliseconds into the day
  557. // This takes care of both h, m, s, ms over/underflows.
  558. long allMillis = (((hour * 60L) + minute) * 60L + second) * 1000L + millis;
  559. day += allMillis / (24 * 60 * 60 * 1000L);
  560. millisInDay = (int) (allMillis % (24 * 60 * 60 * 1000L));
  561. if (month < 0)
  562. {
  563. year += (int) month / 12;
  564. month = month % 12;
  565. if (month < 0)
  566. {
  567. month += 12;
  568. year--;
  569. }
  570. }
  571. if (month > 11)
  572. {
  573. year += (month / 12);
  574. month = month % 12;
  575. }
  576. month_days[1] = isLeapYear(year) ? 29 : 28;
  577. while (day <= 0)
  578. {
  579. if (month == 0)
  580. {
  581. year--;
  582. month_days[1] = isLeapYear(year) ? 29 : 28;
  583. }
  584. month = (month + 11) % 12;
  585. day += month_days[month];
  586. }
  587. while (day > month_days[month])
  588. {
  589. day -= (month_days[month]);
  590. month = (month + 1) % 12;
  591. if (month == 0)
  592. {
  593. year++;
  594. month_days[1] = isLeapYear(year) ? 29 : 28;
  595. }
  596. }
  597. // ok, by here we have valid day,month,year,era and millisinday
  598. int dayOfYear = dayCount[month] + day - 1; // (day starts on 1)
  599. if (isLeapYear(year) && month > 1)
  600. dayOfYear++;
  601. int relativeDay = (year - 1) * 365 + ((year - 1) >> 2) + dayOfYear
  602. - EPOCH_DAYS; // gregorian days from 1 to epoch.
  603. int gregFactor = (int) Math.floor((double) (year - 1) / 400.)
  604. - (int) Math.floor((double) (year - 1) / 100.);
  605. if ((relativeDay + gregFactor) * 60L * 60L * 24L * 1000L >= gregorianCutover)
  606. relativeDay += gregFactor;
  607. else
  608. relativeDay -= 2;
  609. time = relativeDay * (24 * 60 * 60 * 1000L) + millisInDay;
  610. // the epoch was a Thursday.
  611. int weekday = (int) (relativeDay + THURSDAY) % 7;
  612. if (weekday <= 0)
  613. weekday += 7;
  614. fields[DAY_OF_WEEK] = weekday;
  615. // Time zone corrections.
  616. TimeZone zone = getTimeZone();
  617. int rawOffset = isSet[ZONE_OFFSET] ? fields[ZONE_OFFSET]
  618. : zone.getRawOffset();
  619. int dstOffset = isSet[DST_OFFSET] ? fields[DST_OFFSET]
  620. : (zone.getOffset((year < 0) ? BC : AD,
  621. (year < 0) ? 1 - year
  622. : year,
  623. month, day, weekday,
  624. millisInDay)
  625. - zone.getRawOffset());
  626. time -= rawOffset + dstOffset;
  627. isTimeSet = true;
  628. }
  629. /**
  630. * Get the linear day in days since the epoch, using the
  631. * Julian or Gregorian calendar as specified. If you specify a
  632. * nonpositive year it is interpreted as BC as following: 0 is 1
  633. * BC, -1 is 2 BC and so on.
  634. *
  635. * @param year the year of the date.
  636. * @param dayOfYear the day of year of the date; 1 based.
  637. * @param gregorian <code>true</code>, if we should use the Gregorian rules.
  638. * @return the days since the epoch, may be negative.
  639. */
  640. private long getLinearDay(int year, int dayOfYear, boolean gregorian)
  641. {
  642. // The 13 is the number of days, that were omitted in the Gregorian
  643. // Calender until the epoch.
  644. // We shift right by 2 instead of dividing by 4, to get correct
  645. // results for negative years (and this is even more efficient).
  646. long julianDay = (year - 1) * 365L + ((year - 1) >> 2) + (dayOfYear - 1)
  647. - EPOCH_DAYS; // gregorian days from 1 to epoch.
  648. if (gregorian)
  649. {
  650. // subtract the days that are missing in gregorian calendar
  651. // with respect to julian calendar.
  652. //
  653. // Okay, here we rely on the fact that the gregorian
  654. // calendar was introduced in the AD era. This doesn't work
  655. // with negative years.
  656. //
  657. // The additional leap year factor accounts for the fact that
  658. // a leap day is not seen on Jan 1 of the leap year.
  659. int gregOffset = (int) Math.floor((double) (year - 1) / 400.)
  660. - (int) Math.floor((double) (year - 1) / 100.);
  661. return julianDay + gregOffset;
  662. }
  663. else
  664. julianDay -= 2;
  665. return julianDay;
  666. }
  667. /**
  668. * Converts the given linear day into era, year, month,
  669. * day_of_year, day_of_month, day_of_week, and writes the result
  670. * into the fields array.
  671. *
  672. * @param day the linear day.
  673. * @param gregorian true, if we should use Gregorian rules.
  674. */
  675. private void calculateDay(int[] fields, long day, boolean gregorian)
  676. {
  677. // the epoch was a Thursday.
  678. int weekday = (int) (day + THURSDAY) % 7;
  679. if (weekday <= 0)
  680. weekday += 7;
  681. fields[DAY_OF_WEEK] = weekday;
  682. // get a first approximation of the year. This may be one
  683. // year too big.
  684. int year = 1970
  685. + (int) (gregorian
  686. ? ((day - 100L) * 400L) / (365L * 400L + 100L - 4L
  687. + 1L) : ((day - 100L) * 4L) / (365L * 4L + 1L));
  688. if (day >= 0)
  689. year++;
  690. long firstDayOfYear = getLinearDay(year, 1, gregorian);
  691. // Now look in which year day really lies.
  692. if (day < firstDayOfYear)
  693. {
  694. year--;
  695. firstDayOfYear = getLinearDay(year, 1, gregorian);
  696. }
  697. day -= firstDayOfYear - 1; // day of year, one based.
  698. fields[DAY_OF_YEAR] = (int) day;
  699. if (year <= 0)
  700. {
  701. fields[ERA] = BC;
  702. fields[YEAR] = 1 - year;
  703. }
  704. else
  705. {
  706. fields[ERA] = AD;
  707. fields[YEAR] = year;
  708. }
  709. int leapday = isLeapYear(year) ? 1 : 0;
  710. if (day <= 31 + 28 + leapday)
  711. {
  712. fields[MONTH] = (int) day / 32; // 31->JANUARY, 32->FEBRUARY
  713. fields[DAY_OF_MONTH] = (int) day - 31 * fields[MONTH];
  714. }
  715. else
  716. {
  717. // A few more magic formulas
  718. int scaledDay = ((int) day - leapday) * 5 + 8;
  719. fields[MONTH] = scaledDay / (31 + 30 + 31 + 30 + 31);
  720. fields[DAY_OF_MONTH] = (scaledDay % (31 + 30 + 31 + 30 + 31)) / 5 + 1;
  721. }
  722. }
  723. /**
  724. * Converts the milliseconds since the epoch UTC
  725. * (<code>time</code>) to time fields
  726. * (<code>fields</code>).
  727. */
  728. protected synchronized void computeFields()
  729. {
  730. boolean gregorian = (time >= gregorianCutover);
  731. TimeZone zone = getTimeZone();
  732. fields[ZONE_OFFSET] = zone.getRawOffset();
  733. long localTime = time + fields[ZONE_OFFSET];
  734. long day = localTime / (24 * 60 * 60 * 1000L);
  735. int millisInDay = (int) (localTime % (24 * 60 * 60 * 1000L));
  736. if (millisInDay < 0)
  737. {
  738. millisInDay += (24 * 60 * 60 * 1000);
  739. day--;
  740. }
  741. calculateDay(fields, day, gregorian);
  742. fields[DST_OFFSET] = zone.getOffset(fields[ERA], fields[YEAR],
  743. fields[MONTH], fields[DAY_OF_MONTH],
  744. fields[DAY_OF_WEEK], millisInDay)
  745. - fields[ZONE_OFFSET];
  746. millisInDay += fields[DST_OFFSET];
  747. if (millisInDay >= 24 * 60 * 60 * 1000)
  748. {
  749. millisInDay -= 24 * 60 * 60 * 1000;
  750. calculateDay(fields, ++day, gregorian);
  751. }
  752. fields[DAY_OF_WEEK_IN_MONTH] = (fields[DAY_OF_MONTH] + 6) / 7;
  753. // which day of the week are we (0..6), relative to getFirstDayOfWeek
  754. int relativeWeekday = (7 + fields[DAY_OF_WEEK] - getFirstDayOfWeek()) % 7;
  755. // which day of the week is the first of this month?
  756. // nb 35 is the smallest multiple of 7 that ensures that
  757. // the left hand side of the modulo operator is positive.
  758. int relativeWeekdayOfFirst = (relativeWeekday - fields[DAY_OF_MONTH]
  759. + 1 + 35) % 7;
  760. // which week of the month is the first of this month in?
  761. int minDays = getMinimalDaysInFirstWeek();
  762. int weekOfFirst = ((7 - relativeWeekdayOfFirst) >= minDays) ? 1 : 0;
  763. // which week of the month is this day in?
  764. fields[WEEK_OF_MONTH] = (fields[DAY_OF_MONTH]
  765. + relativeWeekdayOfFirst - 1) / 7 + weekOfFirst;
  766. int weekOfYear = (fields[DAY_OF_YEAR] - relativeWeekday + 6) / 7;
  767. // Do the Correction: getMinimalDaysInFirstWeek() is always in the
  768. // first week.
  769. int firstWeekday = (7 + getWeekDay(fields[YEAR], minDays)
  770. - getFirstDayOfWeek()) % 7;
  771. if (minDays - firstWeekday < 1)
  772. weekOfYear++;
  773. fields[WEEK_OF_YEAR] = weekOfYear;
  774. int hourOfDay = millisInDay / (60 * 60 * 1000);
  775. fields[AM_PM] = (hourOfDay < 12) ? AM : PM;
  776. int hour = hourOfDay % 12;
  777. fields[HOUR] = hour;
  778. fields[HOUR_OF_DAY] = hourOfDay;
  779. millisInDay %= (60 * 60 * 1000);
  780. fields[MINUTE] = millisInDay / (60 * 1000);
  781. millisInDay %= (60 * 1000);
  782. fields[SECOND] = millisInDay / (1000);
  783. fields[MILLISECOND] = millisInDay % 1000;
  784. areFieldsSet = isSet[ERA] = isSet[YEAR] = isSet[MONTH] = isSet[WEEK_OF_YEAR] = isSet[WEEK_OF_MONTH] = isSet[DAY_OF_MONTH] = isSet[DAY_OF_YEAR] = isSet[DAY_OF_WEEK] = isSet[DAY_OF_WEEK_IN_MONTH] = isSet[AM_PM] = isSet[HOUR] = isSet[HOUR_OF_DAY] = isSet[MINUTE] = isSet[SECOND] = isSet[MILLISECOND] = isSet[ZONE_OFFSET] = isSet[DST_OFFSET] = true;
  785. }
  786. /**
  787. * Return a hash code for this object, following the general contract
  788. * specified by {@link Object#hashCode()}.
  789. * @return the hash code
  790. */
  791. public int hashCode()
  792. {
  793. int val = (int) ((gregorianCutover >>> 32) ^ (gregorianCutover & 0xffffffff));
  794. return super.hashCode() ^ val;
  795. }
  796. /**
  797. * Compares the given calendar with this. An object, o, is
  798. * equivalent to this if it is also a <code>GregorianCalendar</code>
  799. * with the same time since the epoch under the same conditions
  800. * (same change date and same time zone).
  801. *
  802. * @param o the object to that we should compare.
  803. * @return true, if the given object is a calendar, that represents
  804. * the same time (but doesn't necessarily have the same fields).
  805. * @throws IllegalArgumentException if one of the fields
  806. * <code>ZONE_OFFSET</code> or <code>DST_OFFSET</code> is
  807. * specified, if an unknown field is specified or if one
  808. * of the calendar fields receives an illegal value when
  809. * leniancy is not enabled.
  810. */
  811. public boolean equals(Object o)
  812. {
  813. if (! (o instanceof GregorianCalendar))
  814. return false;
  815. GregorianCalendar cal = (GregorianCalendar) o;
  816. return (cal.gregorianCutover == gregorianCutover
  817. && super.equals(o));
  818. }
  819. /**
  820. * Adds the specified amount of time to the given time field. The
  821. * amount may be negative to subtract the time. If the field overflows
  822. * it does what you expect: Jan, 25 + 10 Days is Feb, 4.
  823. * @param field one of the time field constants.
  824. * @param amount the amount of time to add.
  825. * @exception IllegalArgumentException if <code>field</code> is
  826. * <code>ZONE_OFFSET</code>, <code>DST_OFFSET</code>, or invalid; or
  827. * if <code>amount</code> contains an out-of-range value and the calendar
  828. * is not in lenient mode.
  829. */
  830. public void add(int field, int amount)
  831. {
  832. switch (field)
  833. {
  834. case YEAR:
  835. complete();
  836. fields[YEAR] += amount;
  837. isTimeSet = false;
  838. break;
  839. case MONTH:
  840. complete();
  841. int months = fields[MONTH] + amount;
  842. fields[YEAR] += months / 12;
  843. fields[MONTH] = months % 12;
  844. if (fields[MONTH] < 0)
  845. {
  846. fields[MONTH] += 12;
  847. fields[YEAR]--;
  848. }
  849. int maxDay = getActualMaximum(DAY_OF_MONTH);
  850. if (fields[DAY_OF_MONTH] > maxDay)
  851. fields[DAY_OF_MONTH] = maxDay;
  852. set(YEAR, fields[YEAR]);
  853. set(MONTH, fields[MONTH]);
  854. break;
  855. case DAY_OF_MONTH:
  856. case DAY_OF_YEAR:
  857. case DAY_OF_WEEK:
  858. if (! isTimeSet)
  859. computeTime();
  860. time += amount * (24 * 60 * 60 * 1000L);
  861. areFieldsSet = false;
  862. break;
  863. case WEEK_OF_YEAR:
  864. case WEEK_OF_MONTH:
  865. case DAY_OF_WEEK_IN_MONTH:
  866. if (! isTimeSet)
  867. computeTime();
  868. time += amount * (7 * 24 * 60 * 60 * 1000L);
  869. areFieldsSet = false;
  870. break;
  871. case AM_PM:
  872. if (! isTimeSet)
  873. computeTime();
  874. time += amount * (12 * 60 * 60 * 1000L);
  875. areFieldsSet = false;
  876. break;
  877. case HOUR:
  878. case HOUR_OF_DAY:
  879. if (! isTimeSet)
  880. computeTime();
  881. time += amount * (60 * 60 * 1000L);
  882. areFieldsSet = false;
  883. break;
  884. case MINUTE:
  885. if (! isTimeSet)
  886. computeTime();
  887. time += amount * (60 * 1000L);
  888. areFieldsSet = false;
  889. break;
  890. case SECOND:
  891. if (! isTimeSet)
  892. computeTime();
  893. time += amount * (1000L);
  894. areFieldsSet = false;
  895. break;
  896. case MILLISECOND:
  897. if (! isTimeSet)
  898. computeTime();
  899. time += amount;
  900. areFieldsSet = false;
  901. break;
  902. case ZONE_OFFSET:
  903. case DST_OFFSET:default:
  904. throw new IllegalArgumentException("Invalid or unknown field");
  905. }
  906. }
  907. /**
  908. * Rolls the specified time field up or down. This means add one
  909. * to the specified field, but don't change the other fields. If
  910. * the maximum for this field is reached, start over with the
  911. * minimum value.
  912. *
  913. * <strong>Note:</strong> There may be situation, where the other
  914. * fields must be changed, e.g rolling the month on May, 31.
  915. * The date June, 31 is automatically converted to July, 1.
  916. * This requires lenient settings.
  917. *
  918. * @param field the time field. One of the time field constants.
  919. * @param up the direction, true for up, false for down.
  920. * @throws IllegalArgumentException if one of the fields
  921. * <code>ZONE_OFFSET</code> or <code>DST_OFFSET</code> is
  922. * specified, if an unknown field is specified or if one
  923. * of the calendar fields receives an illegal value when
  924. * leniancy is not enabled.
  925. */
  926. public void roll(int field, boolean up)
  927. {
  928. roll(field, up ? 1 : -1);
  929. }
  930. /**
  931. * Checks that the fields are still within their legal bounds,
  932. * following use of the <code>roll()</code> method.
  933. *
  934. * @param field the field to check.
  935. * @param delta multipler for alterations to the <code>time</code>.
  936. * @see #roll(int, boolean)
  937. * @see #roll(int, int)
  938. */
  939. private void cleanUpAfterRoll(int field, int delta)
  940. {
  941. switch (field)
  942. {
  943. case ERA:
  944. case YEAR:
  945. case MONTH:
  946. // check that day of month is still in correct range
  947. if (fields[DAY_OF_MONTH] > getActualMaximum(DAY_OF_MONTH))
  948. fields[DAY_OF_MONTH] = getActualMaximum(DAY_OF_MONTH);
  949. isTimeSet = false;
  950. isSet[WEEK_OF_MONTH] = false;
  951. isSet[DAY_OF_WEEK] = false;
  952. isSet[DAY_OF_WEEK_IN_MONTH] = false;
  953. isSet[DAY_OF_YEAR] = false;
  954. isSet[WEEK_OF_YEAR] = false;
  955. break;
  956. case DAY_OF_MONTH:
  957. isSet[WEEK_OF_MONTH] = false;
  958. isSet[DAY_OF_WEEK] = false;
  959. isSet[DAY_OF_WEEK_IN_MONTH] = false;
  960. isSet[DAY_OF_YEAR] = false;
  961. isSet[WEEK_OF_YEAR] = false;
  962. time += delta * (24 * 60 * 60 * 1000L);
  963. break;
  964. case WEEK_OF_MONTH:
  965. isSet[DAY_OF_MONTH] = false;
  966. isSet[DAY_OF_WEEK_IN_MONTH] = false;
  967. isSet[DAY_OF_YEAR] = false;
  968. isSet[WEEK_OF_YEAR] = false;
  969. time += delta * (7 * 24 * 60 * 60 * 1000L);
  970. break;
  971. case DAY_OF_WEEK_IN_MONTH:
  972. isSet[DAY_OF_MONTH] = false;
  973. isSet[WEEK_OF_MONTH] = false;
  974. isSet[DAY_OF_YEAR] = false;
  975. isSet[WEEK_OF_YEAR] = false;
  976. time += delta * (7 * 24 * 60 * 60 * 1000L);
  977. break;
  978. case DAY_OF_YEAR:
  979. isSet[MONTH] = false;
  980. isSet[DAY_OF_MONTH] = false;
  981. isSet[WEEK_OF_MONTH] = false;
  982. isSet[DAY_OF_WEEK_IN_MONTH] = false;
  983. isSet[DAY_OF_WEEK] = false;
  984. isSet[WEEK_OF_YEAR] = false;
  985. time += delta * (24 * 60 * 60 * 1000L);
  986. break;
  987. case WEEK_OF_YEAR:
  988. isSet[MONTH] = false;
  989. isSet[DAY_OF_MONTH] = false;
  990. isSet[WEEK_OF_MONTH] = false;
  991. isSet[DAY_OF_WEEK_IN_MONTH] = false;
  992. isSet[DAY_OF_YEAR] = false;
  993. time += delta * (7 * 24 * 60 * 60 * 1000L);
  994. break;
  995. case AM_PM:
  996. isSet[HOUR_OF_DAY] = false;
  997. time += delta * (12 * 60 * 60 * 1000L);
  998. break;
  999. case HOUR:
  1000. isSet[HOUR_OF_DAY] = false;
  1001. time += delta * (60 * 60 * 1000L);
  1002. break;
  1003. case HOUR_OF_DAY:
  1004. isSet[HOUR] = false;
  1005. isSet[AM_PM] = false;
  1006. time += delta * (60 * 60 * 1000L);
  1007. break;
  1008. case MINUTE:
  1009. time += delta * (60 * 1000L);
  1010. break;
  1011. case SECOND:
  1012. time += delta * (1000L);
  1013. break;
  1014. case MILLISECOND:
  1015. time += delta;
  1016. break;
  1017. }
  1018. }
  1019. /**
  1020. * Rolls the specified time field by the given amount. This means
  1021. * add amount to the specified field, but don't change the other
  1022. * fields. If the maximum for this field is reached, start over
  1023. * with the minimum value and vice versa for negative amounts.
  1024. *
  1025. * <strong>Note:</strong> There may be situation, where the other
  1026. * fields must be changed, e.g rolling the month on May, 31.
  1027. * The date June, 31 is automatically corrected to June, 30.
  1028. *
  1029. * @param field the time field. One of the time field constants.
  1030. * @param amount the amount by which we should roll.
  1031. * @throws IllegalArgumentException if one of the fields
  1032. * <code>ZONE_OFFSET</code> or <code>DST_OFFSET</code> is
  1033. * specified, if an unknown field is specified or if one
  1034. * of the calendar fields receives an illegal value when
  1035. * leniancy is not enabled.
  1036. */
  1037. public void roll(int field, int amount)
  1038. {
  1039. switch (field)
  1040. {
  1041. case DAY_OF_WEEK:
  1042. // day of week is special: it rolls automatically
  1043. add(field, amount);
  1044. return;
  1045. case ZONE_OFFSET:
  1046. case DST_OFFSET:
  1047. throw new IllegalArgumentException("Can't roll time zone");
  1048. }
  1049. complete();
  1050. int min = getActualMinimum(field);
  1051. int range = getActualMaximum(field) - min + 1;
  1052. int oldval = fields[field];
  1053. int newval = (oldval - min + range + amount) % range + min;
  1054. if (newval < min)
  1055. newval += range;
  1056. fields[field] = newval;
  1057. cleanUpAfterRoll(field, newval - oldval);
  1058. }
  1059. /**
  1060. * The minimum values for the calendar fields.
  1061. */
  1062. private static final int[] minimums =
  1063. {
  1064. BC, 1, 0, 0, 1, 1, 1, SUNDAY, 1, AM,
  1065. 1, 0, 0, 0, 0, -(12 * 60 * 60 * 1000),
  1066. 0
  1067. };
  1068. /**
  1069. * The maximum values for the calendar fields.
  1070. */
  1071. private static final int[] maximums =
  1072. {
  1073. AD, 5000000, 11, 53, 6, 31, 366,
  1074. SATURDAY, 5, PM, 12, 23, 59, 59, 999,
  1075. +(12 * 60 * 60 * 1000),
  1076. (12 * 60 * 60 * 1000)
  1077. };
  1078. /**
  1079. * Gets the smallest value that is allowed for the specified field.
  1080. *
  1081. * @param field one of the time field constants.
  1082. * @return the smallest value for the specified field.
  1083. */
  1084. public int getMinimum(int field)
  1085. {
  1086. return minimums[field];
  1087. }
  1088. /**
  1089. * Gets the biggest value that is allowed for the specified field.
  1090. *
  1091. * @param field one of the time field constants.
  1092. * @return the biggest value.
  1093. */
  1094. public int getMaximum(int field)
  1095. {
  1096. return maximums[field];
  1097. }
  1098. /**
  1099. * Gets the greatest minimum value that is allowed for the specified field.
  1100. * This is the largest value returned by the <code>getActualMinimum(int)</code>
  1101. * method.
  1102. *
  1103. * @param field the time field. One of the time field constants.
  1104. * @return the greatest minimum value.
  1105. * @see #getActualMinimum(int)
  1106. */
  1107. public int getGreatestMinimum(int field)
  1108. {
  1109. if (field == WEEK_OF_YEAR)
  1110. return 1;
  1111. return minimums[field];
  1112. }
  1113. /**
  1114. * Gets the smallest maximum value that is allowed for the
  1115. * specified field. This is the smallest value returned
  1116. * by the <code>getActualMaximum(int)</code>. For example,
  1117. * this is 28 for DAY_OF_MONTH (as all months have at least
  1118. * 28 days).
  1119. *
  1120. * @param field the time field. One of the time field constants.
  1121. * @return the least maximum value.
  1122. * @see #getActualMaximum(int)
  1123. * @since 1.2
  1124. */
  1125. public int getLeastMaximum(int field)
  1126. {
  1127. switch (field)
  1128. {
  1129. case WEEK_OF_YEAR:
  1130. return 52;
  1131. case DAY_OF_MONTH:
  1132. return 28;
  1133. case DAY_OF_YEAR:
  1134. return 365;
  1135. case DAY_OF_WEEK_IN_MONTH:
  1136. case WEEK_OF_MONTH:
  1137. return 4;
  1138. default:
  1139. return maximums[field];
  1140. }
  1141. }
  1142. /**
  1143. * Gets the actual minimum value that is allowed for the specified field.
  1144. * This value is dependent on the values of the other fields. Note that
  1145. * this calls <code>complete()</code> if not enough fields are set. This
  1146. * can have ugly side effects. The value given depends on the current
  1147. * time used by this instance.
  1148. *
  1149. * @param field the time field. One of the time field constants.
  1150. * @return the actual minimum value.
  1151. * @since 1.2
  1152. */
  1153. public int getActualMinimum(int field)
  1154. {
  1155. if (field == WEEK_OF_YEAR)
  1156. {
  1157. int min = getMinimalDaysInFirstWeek();
  1158. if (min == 0)
  1159. return 1;
  1160. if (! areFieldsSet || ! isSet[ERA] || ! isSet[YEAR])
  1161. complete();
  1162. int year = fields[ERA] == AD ? fields[YEAR] : 1 - fields[YEAR];
  1163. int weekday = getWeekDay(year, min);
  1164. if ((7 + weekday - getFirstDayOfWeek()) % 7 >= min - 1)
  1165. return 1;
  1166. return 0;
  1167. }
  1168. return minimums[field];
  1169. }
  1170. /**
  1171. * Gets the actual maximum value that is allowed for the specified field.
  1172. * This value is dependent on the values of the other fields. Note that
  1173. * this calls <code>complete()</code> if not enough fields are set. This
  1174. * can have ugly side effects. The value given depends on the current time
  1175. * used by this instance; thus, leap years have a maximum day of month value of
  1176. * 29, rather than 28.
  1177. *
  1178. * @param field the time field. One of the time field constants.
  1179. * @return the actual maximum value.
  1180. */
  1181. public int getActualMaximum(int field)
  1182. {
  1183. switch (field)
  1184. {
  1185. case WEEK_OF_YEAR:
  1186. {
  1187. if (! areFieldsSet || ! isSet[ERA] || ! isSet[YEAR])
  1188. complete();
  1189. // This is wrong for the year that contains the gregorian change.
  1190. // I.e it gives the weeks in the julian year or in the gregorian
  1191. // year in that case.
  1192. int year = fields[ERA] == AD ? fields[YEAR] : 1 - fields[YEAR];
  1193. int lastDay = isLeapYear(year) ? 366 : 365;
  1194. int weekday = getWeekDay(year, lastDay);
  1195. int week = (lastDay + 6 - (7 + weekday - getFirstDayOfWeek()) % 7) / 7;
  1196. int minimalDays = getMinimalDaysInFirstWeek();
  1197. int firstWeekday = getWeekDay(year, minimalDays);
  1198. /*
  1199. * Is there a set of days at the beginning of the year, before the
  1200. * first day of the week, equal to or greater than the minimum number
  1201. * of days required in the first week?
  1202. */
  1203. if (minimalDays - (7 + firstWeekday - getFirstDayOfWeek()) % 7 < 1)
  1204. return week + 1; /* Add week 1: firstWeekday through to firstDayOfWeek */
  1205. }
  1206. case DAY_OF_MONTH:
  1207. {
  1208. if (! areFieldsSet || ! isSet[MONTH])
  1209. complete();
  1210. int month = fields[MONTH];
  1211. // If you change this, you should also change
  1212. // SimpleTimeZone.getDaysInMonth();
  1213. if (month == FEBRUARY)
  1214. {
  1215. if (! isSet[YEAR] || ! isSet[ERA])
  1216. complete();
  1217. int year = fields[ERA] == AD ? fields[YEAR] : 1 - fields[YEAR];
  1218. return isLeapYear(year) ? 29 : 28;
  1219. }
  1220. else if (month < AUGUST)
  1221. return 31 - (month & 1);
  1222. else
  1223. return 30 + (month & 1);
  1224. }
  1225. case DAY_OF_YEAR:
  1226. {
  1227. if (! areFieldsSet || ! isSet[ERA] || ! isSet[YEAR])
  1228. complete();
  1229. int year = fields[ERA] == AD ? fields[YEAR] : 1 - fields[YEAR];
  1230. return isLeapYear(year) ? 366 : 365;
  1231. }
  1232. case DAY_OF_WEEK_IN_MONTH:
  1233. {
  1234. // This is wrong for the month that contains the gregorian change.
  1235. int daysInMonth = getActualMaximum(DAY_OF_MONTH);
  1236. // That's black magic, I know
  1237. return (daysInMonth - (fields[DAY_OF_MONTH] - 1) % 7 + 6) / 7;
  1238. }
  1239. case WEEK_OF_MONTH:
  1240. {
  1241. int daysInMonth = getActualMaximum(DAY_OF_MONTH);
  1242. int weekday = (daysInMonth - fields[DAY_OF_MONTH]
  1243. + fields[DAY_OF_WEEK] - SUNDAY) % 7 + SUNDAY;
  1244. return (daysInMonth + 6 - (7 + weekday - getFirstDayOfWeek()) % 7) / 7;
  1245. }
  1246. default:
  1247. return maximums[field];
  1248. }
  1249. }
  1250. }