1. // $Id: XMLGregorianCalendar.java,v 1.28.4.2.2.4.2.1.2.2.2.6 2004/06/09 19:06:01 ndw Exp $
  2. /*
  3. * @(#)XMLGregorianCalendar.java 1.16 04/07/26
  4. *
  5. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  6. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  7. */
  8. package javax.xml.datatype;
  9. import javax.xml.namespace.QName;
  10. import java.math.BigDecimal;
  11. import java.math.BigInteger;
  12. import java.util.TimeZone;
  13. import java.util.GregorianCalendar;
  14. /**
  15. * <p>Representation for W3C XML Schema 1.0 date/time datatypes.
  16. * Specifically, these date/time datatypes are
  17. * <a href="#DATETIME"><code>dateTime</code></a>,
  18. * <a href="#TIME"><code>time</code></a>,
  19. * <a href="#DATE"><code>date</code></a>,
  20. * <a href="#GYEARMONTH"><code>gYearMonth</code></a>,
  21. * <a href="#GMONTHDAY"><code>gMonthDay</code></a>,
  22. * <a href="#GYEAR"><code>gYear</code></a>
  23. * <a href="#GMONTH"><code>gMonth</code></a> and
  24. * <a href="#GDAY"><code>gDay</code></a> defined in the XML Namespace
  25. * <code>"http://www.w3.org/2001/XMLSchema"</code>.
  26. * These datatypes are normatively defined in
  27. * <a href="http://www.w3.org/TR/xmlschema-2/#dateTime">W3C XML Schema 1.0 Part 2, Section 3.2.7-14</a>.</p>
  28. *
  29. * <p>The table below defines the mapping between XML Schema 1.0
  30. * date/time datatype fields and this class' fields. It also summarizes
  31. * the value constraints for the date and time fields defined in
  32. * <a href="http://www.w3.org/TR/xmlschema-2/#isoformats">W3C XML Schema 1.0 Part 2, Appendix D,
  33. * <i>ISO 8601 Date and Time Formats</i></a>.</p>
  34. *
  35. * <a name="datetimefieldsmapping"/>
  36. * <table border="2" rules="all" cellpadding="2">
  37. * <thead>
  38. * <tr>
  39. * <th align="center" colspan="3">
  40. * Date/Time Datatype Field Mapping Between XML Schema 1.0 and Java Representation
  41. * </th>
  42. * </tr>
  43. * </thead>
  44. * <tbody>
  45. * <tr>
  46. * <th>XML Schema 1.0<br/>
  47. * datatype<br/>
  48. * field</th>
  49. * <th>Related<br/>XMLGregorianCalendar<br/>Accessor(s)</th>
  50. * <th>Value Range</th>
  51. * </tr>
  52. * <a name="datetimefield-year"/>
  53. * <tr>
  54. * <td> year </td>
  55. * <td> {@link #getYear()} + {@link #getEon()} or<br/>
  56. * {@link #getEonAndYear}
  57. * </td>
  58. * <td> <code>getYear()</code> is a value between -(10^9-1) to (10^9)-1
  59. * or {@link DatatypeConstants#FIELD_UNDEFINED}.<br/>
  60. * {@link #getEon()} is high order year value in billion of years.<br/>
  61. * <code>getEon()</code> has values greater than or equal to (10^9) or less than or equal to -(10^9).
  62. * A value of null indicates field is undefined.</br>
  63. * Given that <a href="http://www.w3.org/2001/05/xmlschema-errata#e2-63">XML Schema 1.0 errata</a> states that the year zero
  64. * will be a valid lexical value in a future version of XML Schema,
  65. * this class allows the year field to be set to zero. Otherwise,
  66. * the year field value is handled exactly as described
  67. * in the errata and [ISO-8601-1988]. Note that W3C XML Schema 1.0
  68. * validation does not allow for the year field to have a value of zero.
  69. * </td>
  70. * </tr>
  71. * <a name="datetimefield-month"/>
  72. * <tr>
  73. * <td> month </td>
  74. * <td> {@link #getMonth()} </td>
  75. * <td> 1 to 12 or {@link DatatypeConstants#FIELD_UNDEFINED} </td>
  76. * </tr>
  77. * <a name="datetimefield-day"/>
  78. * <tr>
  79. * <td> day </td>
  80. * <td> {@link #getDay()} </td>
  81. * <td> Independent of month, max range is 1 to 31 or {@link DatatypeConstants#FIELD_UNDEFINED}.<br/>
  82. * The normative value constraint stated relative to month
  83. * field's value is in <a href="http://www.w3.org/TR/xmlschema-2/#isoformats">W3C XML Schema 1.0 Part 2, Appendix D</a>.
  84. * </td>
  85. * </tr>
  86. * <tr id="datetimefield-hour">
  87. * <td>hour</td>
  88. * <td>{@link #getHour()}</td>
  89. * <td>
  90. * 0 to 24 or {@link DatatypeConstants#FIELD_UNDEFINED}.
  91. * For a value of 24, the minute and second field must be zero per
  92. * <a href="http://www.w3.org/2001/05/xmlschema-errata#e2-45">XML Schema Errata</a>.
  93. * </td>
  94. * </tr>
  95. * <a name="datetimefield-minute"/>
  96. * <tr>
  97. * <td> minute </td>
  98. * <td> {@link #getMinute()} </td>
  99. * <td> 0 to 59 or {@link DatatypeConstants#FIELD_UNDEFINED} </td>
  100. * </tr>
  101. * <a name="datetimefield-second"/>
  102. * <tr>
  103. * <td>second</td>
  104. * <td>
  105. * {@link #getSecond()} + {@link #getMillisecond()}/1000 or<br/>
  106. * {@link #getSecond()} + {@link #getFractionalSecond()}
  107. * </td>
  108. * <td>
  109. * {@link #getSecond()} from 0 to 60 or {@link DatatypeConstants#FIELD_UNDEFINED}.<br/>
  110. * <i>(Note: 60 only allowable for leap second.)</i><br/>
  111. * {@link #getFractionalSecond()} allows for infinite precision over the range from 0.0 to 1.0 when
  112. * the {@link #getSecond()} is defined.<br/>
  113. * <code>FractionalSecond</code> is optional and has a value of <code>null</code> when it is undefined.<br />
  114. * {@link #getMillisecond()} is the convenience
  115. * millisecond precision of value of {@link #getFractionalSecond()}.
  116. * </td>
  117. * </tr>
  118. * <tr id="datetimefield-timezone">
  119. * <td> timezone </td>
  120. * <td> {@link #getTimezone()} </td>
  121. * <td> Number of minutes or {@link DatatypeConstants#FIELD_UNDEFINED}.
  122. * Value range from -14 hours (-14 * 60 minutes) to 14 hours (14 * 60 minutes).
  123. * </td>
  124. * </tr>
  125. * </tbody>
  126. * </table>
  127. *
  128. * <p>All maximum value space constraints listed for the fields in the table
  129. * above are checked by factory methods, @{link DatatypeFactory},
  130. * setter methods and parse methods of
  131. * this class. <code>IllegalArgumentException</code> is thrown when a
  132. * parameter's value is outside the value constraint for the field or
  133. * if the composite
  134. * values constitute an invalid XMLGregorianCalendar instance (for example, if
  135. * the 31st of June is specified).
  136. * </p>
  137. *
  138. * <p>The following operations are defined for this class:
  139. * <ul>
  140. * <li>accessors/mutators for independent date/time fields</li>
  141. * <li>conversion between this class and W3C XML Schema 1.0 lexical representation,
  142. * {@link #toString()}, {@link DatatypeFactory#newXMLGregorianCalendar(String lexicalRepresentation)}</li>
  143. * <li>conversion between this class and {@link GregorianCalendar},
  144. * {@link #toGregorianCalendar(java.util.TimeZone timezone, java.util.Locale aLocale, XMLGregorianCalendar defaults)},
  145. * {@link DatatypeFactory}</li>
  146. * <li>partial order relation comparator method, {@link #compare(XMLGregorianCalendar xmlGregorianCalendar)}</li>
  147. * <li>{@link #equals(Object)} defined relative to {@link #compare(XMLGregorianCalendar xmlGregorianCalendar)}.</li>
  148. * <li>addition operation with {@link Duration}
  149. * instance as defined in <a href="http://www.w3.org/TR/xmlschema-2/#adding-durations-to-dateTimes">
  150. * W3C XML Schema 1.0 Part 2, Appendix E, <i>Adding durations to dateTimes</i></a>.
  151. * </li>
  152. * </ul>
  153. * </p>
  154. *
  155. * @author <a href="mailto:Joseph.Fialli@Sun.com">Joseph Fialli</a>
  156. * @author <a href="mailto:Kohsuke.Kawaguchi@Sun.com">Kohsuke Kawaguchi</a>
  157. * @author <a href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a>
  158. * @version $Revision: 1.28.4.2.2.4.2.1.2.2.2.6 $, $Date: 2004/06/09 19:06:01 $
  159. * @see Duration
  160. * @see DatatypeFactory
  161. * @since 1.5
  162. */
  163. public abstract class XMLGregorianCalendar
  164. implements Cloneable {
  165. /**
  166. * <p>Unset all fields to undefined.</p>
  167. *
  168. * <p>Set all int fields to {@link DatatypeConstants#FIELD_UNDEFINED} and reference fields
  169. * to null.</p>
  170. */
  171. public abstract void clear();
  172. /**
  173. * <p>Reset this <code>XMLGregorianCalendar</code> to its original values.</p>
  174. *
  175. * <p><code>XMLGregorianCalendar</code> is reset to the same values as when it was created with
  176. * {@link DatatypeFactory#newXMLGregorianCalendar()},
  177. * {@link DatatypeFactory#newXMLGregorianCalendar(String lexicalRepresentation)},
  178. * {@link DatatypeFactory#newXMLGregorianCalendar(
  179. * BigInteger year,
  180. * int month,
  181. * int day,
  182. * int hour,
  183. * int minute,
  184. * int second,
  185. * BigDecimal fractionalSecond,
  186. * int timezone)},
  187. * {@link DatatypeFactory#newXMLGregorianCalendar(
  188. * int year,
  189. * int month,
  190. * int day,
  191. * int hour,
  192. * int minute,
  193. * int second,
  194. * int millisecond,
  195. * int timezone)},
  196. * {@link DatatypeFactory#newXMLGregorianCalendar(GregorianCalendar cal)},
  197. * {@link DatatypeFactory#newXMLGregorianCalendarDate(
  198. * int year,
  199. * int month,
  200. * int day,
  201. * int timezone)},
  202. * {@link DatatypeFactory#newXMLGregorianCalendarTime(
  203. * int hours,
  204. * int minutes,
  205. * int seconds,
  206. * int timezone)},
  207. * {@link DatatypeFactory#newXMLGregorianCalendarTime(
  208. * int hours,
  209. * int minutes,
  210. * int seconds,
  211. * BigDecimal fractionalSecond,
  212. * int timezone)} or
  213. * {@link DatatypeFactory#newXMLGregorianCalendarTime(
  214. * int hours,
  215. * int minutes,
  216. * int seconds,
  217. * int milliseconds,
  218. * int timezone)}.
  219. * </p>
  220. *
  221. * <p><code>reset()</code> is designed to allow the reuse of existing <code>XMLGregorianCalendar</code>s
  222. * thus saving resources associated with the creation of new <code>XMLGregorianCalendar</code>s.</p>
  223. */
  224. public abstract void reset();
  225. /**
  226. * <p>Set low and high order component of XSD <code>dateTime</code> year field.</p>
  227. *
  228. * <p>Unset this field by invoking the setter with a parameter value of <code>null</code>.</p>
  229. *
  230. * @param year value constraints summarized in <a href="#datetimefield-year">year field of date/time field mapping table</a>.
  231. *
  232. * @throws IllegalArgumentException if <code>year</code> parameter is
  233. * outside value constraints for the field as specified in
  234. * <a href="#datetimefieldmapping">date/time field mapping table</a>.
  235. */
  236. public abstract void setYear(BigInteger year);
  237. /**
  238. * <p>Set year of XSD <code>dateTime</code> year field.</p>
  239. *
  240. * <p>Unset this field by invoking the setter with a parameter value of
  241. * {@link DatatypeConstants#FIELD_UNDEFINED}.</p>
  242. *
  243. * <p>Note: if the absolute value of the <code>year</code> parameter
  244. * is less than 10^9, the eon component of the XSD year field is set to
  245. * <code>null</code> by this method.</p>
  246. *
  247. * @param year value constraints are summarized in <a href="#datetimefield-year">year field of date/time field mapping table</a>.
  248. * If year is {@link DatatypeConstants#FIELD_UNDEFINED}, then eon is set to <code>null</code>.
  249. */
  250. public abstract void setYear(int year);
  251. /**
  252. * <p>Set month.</p>
  253. *
  254. * <p>Unset this field by invoking the setter with a parameter value of {@link DatatypeConstants#FIELD_UNDEFINED}.</p>
  255. *
  256. * @param month value constraints summarized in <a href="#datetimefield-month">month field of date/time field mapping table</a>.
  257. *
  258. * @throws IllegalArgumentException if <code>month</code> parameter is
  259. * outside value constraints for the field as specified in
  260. * <a href="#datetimefieldmapping">date/time field mapping table</a>.
  261. */
  262. public abstract void setMonth(int month);
  263. /**
  264. * <p>Set days in month.</p>
  265. *
  266. * <p>Unset this field by invoking the setter with a parameter value of {@link DatatypeConstants#FIELD_UNDEFINED}.</p>
  267. *
  268. * @param day value constraints summarized in <a href="#datetimefield-day">day field of date/time field mapping table</a>.
  269. *
  270. * @throws IllegalArgumentException if <code>day</code> parameter is
  271. * outside value constraints for the field as specified in
  272. * <a href="#datetimefieldmapping">date/time field mapping table</a>.
  273. */
  274. public abstract void setDay(int day);
  275. /**
  276. * <p>Set the number of minutes in the timezone offset.</p>
  277. *
  278. * <p>Unset this field by invoking the setter with a parameter value of {@link DatatypeConstants#FIELD_UNDEFINED}.</p>
  279. *
  280. * @param offset value constraints summarized in <a href="#datetimefield-timezone">
  281. * timezone field of date/time field mapping table</a>.
  282. *
  283. * @throws IllegalArgumentException if <code>offset</code> parameter is
  284. * outside value constraints for the field as specified in
  285. * <a href="#datetimefieldmapping">date/time field mapping table</a>.
  286. */
  287. public abstract void setTimezone(int offset);
  288. /**
  289. * <p>Set time as one unit.</p>
  290. *
  291. * @param hour value constraints are summarized in
  292. * <a href="#datetimefield-hour">hour field of date/time field mapping table</a>.
  293. * @param minute value constraints are summarized in
  294. * <a href="#datetimefield-minute">minute field of date/time field mapping table</a>.
  295. * @param second value constraints are summarized in
  296. * <a href="#datetimefield-second">second field of date/time field mapping table</a>.
  297. *
  298. * @see #setTime(int, int, int, BigDecimal)
  299. *
  300. * @throws IllegalArgumentException if any parameter is
  301. * outside value constraints for the field as specified in
  302. * <a href="#datetimefieldmapping">date/time field mapping table</a>.
  303. */
  304. public void setTime(int hour, int minute, int second) {
  305. setTime(
  306. hour,
  307. minute,
  308. second,
  309. null // fractional
  310. );
  311. }
  312. /**
  313. * <p>Set hours.</p>
  314. *
  315. * <p>Unset this field by invoking the setter with a parameter value of {@link DatatypeConstants#FIELD_UNDEFINED}.</p>
  316. *
  317. * @param hour value constraints summarized in <a href="#datetimefield-hour">hour field of date/time field mapping table</a>.
  318. *
  319. * @throws IllegalArgumentException if <code>hour</code> parameter is outside value constraints for the field as specified in
  320. * <a href="#datetimefieldmapping">date/time field mapping table</a>.
  321. */
  322. public abstract void setHour(int hour);
  323. /**
  324. * <p>Set minutes.</p>
  325. *
  326. * <p>Unset this field by invoking the setter with a parameter value of {@link DatatypeConstants#FIELD_UNDEFINED}.</p>
  327. *
  328. * @param minute value constraints summarized in <a href="#datetimefield-minute">minute field of date/time field mapping table</a>.
  329. *
  330. * @throws IllegalArgumentException if <code>minute</code> parameter is outside value constraints for the field as specified in
  331. * <a href="#datetimefieldmapping">date/time field mapping table</a>.
  332. */
  333. public abstract void setMinute(int minute);
  334. /**
  335. * <p>Set seconds.</p>
  336. *
  337. * <p>Unset this field by invoking the setter with a parameter value of {@link DatatypeConstants#FIELD_UNDEFINED}.</p>
  338. *
  339. * @param second value constraints summarized in <a href="#datetimefield-second">second field of date/time field mapping table</a>.
  340. *
  341. * @throws IllegalArgumentException if <code>second</code> parameter is outside value constraints for the field as specified in
  342. * <a href="#datetimefieldmapping">date/time field mapping table</a>.
  343. */
  344. public abstract void setSecond(int second);
  345. /**
  346. * <p>Set milliseconds.</p>
  347. *
  348. * <p>Unset this field by invoking the setter with a parameter value of {@link DatatypeConstants#FIELD_UNDEFINED}.</p>
  349. *
  350. * @param millisecond value constraints summarized in
  351. * <a href="#datetimefield-millisecond">millisecond field of date/time field mapping table</a>.
  352. *
  353. * @throws IllegalArgumentException if <code>millisecond</code> parameter is outside value constraints for the field as specified
  354. * in <a href="#datetimefieldmapping">date/time field mapping table</a>.
  355. */
  356. public abstract void setMillisecond(int millisecond);
  357. /**
  358. * <p>Set fractional seconds.</p>
  359. *
  360. * <p>Unset this field by invoking the setter with a parameter value of <code>null</code>.</p>
  361. *
  362. * @param fractional value constraints summarized in
  363. * <a href="#datetimefield-fractional">fractional field of date/time field mapping table</a>.
  364. *
  365. * @throws IllegalArgumentException if <code>fractional</code> parameter is outside value constraints for the field as specified
  366. * in <a href="#datetimefieldmapping">date/time field mapping table</a>.
  367. */
  368. public abstract void setFractionalSecond(BigDecimal fractional);
  369. /**
  370. * <p>Set time as one unit, including the optional infinite precision
  371. * fractional seconds.</p>
  372. *
  373. * @param hour value constraints are summarized in
  374. * <a href="#datetimefield-hour">hour field of date/time field mapping table</a>.
  375. * @param minute value constraints are summarized in
  376. * <a href="#datetimefield-minute">minute field of date/time field mapping table</a>.
  377. * @param second value constraints are summarized in
  378. * <a href="#datetimefield-second">second field of date/time field mapping table</a>.
  379. * @param fractional value of <code>null</code> indicates this optional
  380. * field is not set.
  381. *
  382. * @throws IllegalArgumentException if any parameter is
  383. * outside value constraints for the field as specified in
  384. * <a href="#datetimefieldmapping">date/time field mapping table</a>.
  385. */
  386. public void setTime(
  387. int hour,
  388. int minute,
  389. int second,
  390. BigDecimal fractional) {
  391. setHour(hour);
  392. setMinute(minute);
  393. setSecond(second);
  394. setFractionalSecond(fractional);
  395. }
  396. /**
  397. * <p>Set time as one unit, including optional milliseconds.</p>
  398. *
  399. * @param hour value constraints are summarized in
  400. * <a href="#datetimefield-hour">hour field of date/time field mapping table</a>.
  401. * @param minute value constraints are summarized in
  402. * <a href="#datetimefield-minute">minute field of date/time field mapping table</a>.
  403. * @param second value constraints are summarized in
  404. * <a href="#datetimefield-second">second field of date/time field mapping table</a>.
  405. * @param millisecond value of {@link DatatypeConstants#FIELD_UNDEFINED} indicates this
  406. * optional field is not set.
  407. *
  408. * @throws IllegalArgumentException if any parameter is
  409. * outside value constraints for the field as specified in
  410. * <a href="#datetimefieldmapping">date/time field mapping table</a>.
  411. */
  412. public void setTime(int hour, int minute, int second, int millisecond) {
  413. setHour(hour);
  414. setMinute(minute);
  415. setSecond(second);
  416. setMillisecond(millisecond);
  417. }
  418. /**
  419. * <p>Return high order component for XML Schema 1.0 dateTime datatype field for
  420. * <code>year</code>.
  421. * <code>null</code> if this optional part of the year field is not defined.</p>
  422. *
  423. * <p>Value constraints for this value are summarized in
  424. * <a href="#datetimefield-year">year field of date/time field mapping table</a>.</p>
  425. * @return eon of this <code>XMLGregorianCalendar</code>. The value
  426. * returned is an integer multiple of 10^9.
  427. *
  428. * @see #getYear()
  429. * @see #getEonAndYear()
  430. */
  431. public abstract BigInteger getEon();
  432. /**
  433. * <p>Return low order component for XML Schema 1.0 dateTime datatype field for
  434. * <code>year</code> or {@link DatatypeConstants#FIELD_UNDEFINED}.</p>
  435. *
  436. * <p>Value constraints for this value are summarized in
  437. * <a href="#datetimefield-year">year field of date/time field mapping table</a>.</p>
  438. *
  439. * @return year of this <code>XMLGregorianCalendar</code>.
  440. *
  441. * @see #getEon()
  442. * @see #getEonAndYear()
  443. */
  444. public abstract int getYear();
  445. /**
  446. * <p>Return XML Schema 1.0 dateTime datatype field for
  447. * <code>year</code>.</p>
  448. *
  449. * <p>Value constraints for this value are summarized in
  450. * <a href="#datetimefield-year">year field of date/time field mapping table</a>.</p>
  451. *
  452. * @return sum of <code>eon</code> and <code>BigInteger.valueOf(year)</code>
  453. * when both fields are defined. When only <code>year</code> is defined,
  454. * return it. When both <code>eon</code> and <code>year</code> are not
  455. * defined, return <code>null</code>.
  456. *
  457. * @see #getEon()
  458. * @see #getYear()
  459. */
  460. public abstract BigInteger getEonAndYear();
  461. /**
  462. * <p>Return number of month or {@link DatatypeConstants#FIELD_UNDEFINED}.</p>
  463. *
  464. * <p>Value constraints for this value are summarized in
  465. * <a href="#datetimefield-month">month field of date/time field mapping table</a>.</p>
  466. *
  467. * @return year of this <code>XMLGregorianCalendar</code>.
  468. *
  469. */
  470. public abstract int getMonth();
  471. /**
  472. * Return day in month or {@link DatatypeConstants#FIELD_UNDEFINED}.</p>
  473. *
  474. * <p>Value constraints for this value are summarized in
  475. * <a href="#datetimefield-day">day field of date/time field mapping table</a>.</p>
  476. *
  477. * @see #setDay(int)
  478. */
  479. public abstract int getDay();
  480. /**
  481. * Return timezone offset in minutes or
  482. * {@link DatatypeConstants#FIELD_UNDEFINED} if this optional field is not defined.
  483. *
  484. * <p>Value constraints for this value are summarized in
  485. * <a href="#datetimefield-timezone">timezone field of date/time field mapping table</a>.</p>
  486. *
  487. * @see #setTimezone(int)
  488. */
  489. public abstract int getTimezone();
  490. /**
  491. * Return hours or {@link DatatypeConstants#FIELD_UNDEFINED}.
  492. * Returns {@link DatatypeConstants#FIELD_UNDEFINED} if this field is not defined.
  493. *
  494. * <p>Value constraints for this value are summarized in
  495. * <a href="#datetimefield-hour">hour field of date/time field mapping table</a>.</p>
  496. * @see #setTime(int, int, int)
  497. */
  498. public abstract int getHour();
  499. /**
  500. * Return minutes or {@link DatatypeConstants#FIELD_UNDEFINED}.</p>
  501. * Returns {@link DatatypeConstants#FIELD_UNDEFINED} if this field is not defined.
  502. *
  503. * <p>Value constraints for this value are summarized in
  504. * <a href="#datetimefield-minute">minute field of date/time field mapping table</a>.</p>
  505. * @see #setTime(int, int, int)
  506. */
  507. public abstract int getMinute();
  508. /**
  509. * <p>Return seconds or {@link DatatypeConstants#FIELD_UNDEFINED}.</p>
  510. *
  511. * <p>Returns {@link DatatypeConstants#FIELD_UNDEFINED} if this field is not defined.
  512. * When this field is not defined, the optional xs:dateTime
  513. * fractional seconds field, represented by
  514. * {@link #getFractionalSecond()} and {@link #getMillisecond()},
  515. * must not be defined.</p>
  516. *
  517. * <p>Value constraints for this value are summarized in
  518. * <a href="#datetimefield-second">second field of date/time field mapping table</a>.</p>
  519. *
  520. * @return Second of this <code>XMLGregorianCalendar</code>.
  521. *
  522. * @see #getFractionalSecond()
  523. * @see #getMillisecond()
  524. * @see #setTime(int, int, int)
  525. */
  526. public abstract int getSecond();
  527. /**
  528. * <p>Return millisecond precision of {@link #getFractionalSecond()}.</p>
  529. *
  530. * <p>This method represents a convenience accessor to infinite
  531. * precision fractional second value returned by
  532. * {@link #getFractionalSecond()}. The returned value is the rounded
  533. * down to milliseconds value of
  534. * {@link #getFractionalSecond()}. When {@link #getFractionalSecond()}
  535. * returns <code>null</code>, this method must return
  536. * {@link DatatypeConstants#FIELD_UNDEFINED}.</p>
  537. *
  538. * <p>Value constraints for this value are summarized in
  539. * <a href="#datetimefield-second">second field of date/time field mapping table</a>.</p>
  540. *
  541. * @return Millisecond of this <code>XMLGregorianCalendar</code>.
  542. *
  543. * @see #getFractionalSecond()
  544. * @see #setTime(int, int, int)
  545. */
  546. public int getMillisecond() {
  547. BigDecimal fractionalSeconds = getFractionalSecond();
  548. // is field undefined?
  549. if (fractionalSeconds == null) {
  550. return DatatypeConstants.FIELD_UNDEFINED;
  551. }
  552. return getFractionalSecond().movePointRight(3).intValue();
  553. }
  554. /**
  555. * <p>Return fractional seconds.</p>
  556. *
  557. * <p><code>null</code> is returned when this optional field is not defined.</p>
  558. *
  559. * <p>Value constraints are detailed in
  560. * <a href="#datetimefield-second">second field of date/time field mapping table</a>.</p>
  561. *
  562. * <p>This optional field can only have a defined value when the
  563. * xs:dateTime second field, represented by {@link #getSecond()},
  564. * does not return {@link DatatypeConstants#FIELD_UNDEFINED}.</p>
  565. *
  566. * @return fractional seconds of this <code>XMLGregorianCalendar</code>.
  567. *
  568. * @see #getSecond()
  569. * @see #setTime(int, int, int, BigDecimal)
  570. */
  571. public abstract BigDecimal getFractionalSecond();
  572. // comparisons
  573. /**
  574. * <p>Compare two instances of W3C XML Schema 1.0 date/time datatypes
  575. * according to partial order relation defined in
  576. * <a href="http://www.w3.org/TR/xmlschema-2/#dateTime-order">W3C XML Schema 1.0 Part 2, Section 3.2.7.3,
  577. * <i>Order relation on dateTime</i></a>.</p>
  578. *
  579. * <p><code>xsd:dateTime</code> datatype field mapping to accessors of
  580. * this class are defined in
  581. * <a href="#datetimefieldmapping">date/time field mapping table</a>.</p>
  582. *
  583. * @param xmlGregorianCalendar Instance of <code>XMLGregorianCalendar</code> to compare
  584. *
  585. * @return The relationship between <code>this</code> <code>XMLGregorianCalendar</code> and
  586. * the specified <code>xmlGregorianCalendar</code> as
  587. * {@link DatatypeConstants#LESSER},
  588. * {@link DatatypeConstants#EQUAL},
  589. * {@link DatatypeConstants#GREATER} or
  590. * {@link DatatypeConstants#INDETERMINATE}.
  591. *
  592. * @throws NullPointerException if <code>xmlGregorianCalendar</code> is null.
  593. */
  594. public abstract int compare(XMLGregorianCalendar xmlGregorianCalendar);
  595. /**
  596. * <p>Normalize this instance to UTC.</p>
  597. *
  598. * <p>2000-03-04T23:00:00+03:00 normalizes to 2000-03-04T20:00:00Z</p>
  599. * <p>Implements W3C XML Schema Part 2, Section 3.2.7.3 (A).</p>
  600. *
  601. * @return <code>this</code> <code>XMLGregorianCalendar</code> normalized to UTC.
  602. */
  603. public abstract XMLGregorianCalendar normalize();
  604. /**
  605. * <p>Indicates whether parameter <code>obj</code> is "equal to" this one.</p>
  606. *
  607. * @param obj to compare.
  608. *
  609. * @return <code>true</code> when <code>obj</code> is an instance of <code>XMLGregorianCalendar</code>
  610. * and {@link #compare(XMLGregorianCalendar obj)} returns {@link DatatypeConstants#EQUAL}, otherwise <code>false</code>.
  611. *
  612. * @throws NullPointerException If <code>obj</code> is <code>null</code>.
  613. */
  614. public boolean equals(Object obj) {
  615. if (obj == null) {
  616. throw new NullPointerException("Cannot test null for equality with this XMLGregorianCalendar");
  617. }
  618. boolean result = false;
  619. if (obj instanceof XMLGregorianCalendar) {
  620. result = compare((XMLGregorianCalendar) obj) == DatatypeConstants.EQUAL;
  621. }
  622. return result;
  623. }
  624. /**
  625. * <p>Returns a hash code consistent with the definition of the equals method.</p>
  626. *
  627. * @return hash code of this object.
  628. */
  629. public int hashCode() {
  630. // Following two dates compare to EQUALS since in different timezones.
  631. // 2000-01-15T12:00:00-05:00 == 2000-01-15T13:00:00-04:00
  632. //
  633. // Must ensure both instances generate same hashcode by normalizing
  634. // this to UTC timezone.
  635. int timezone = getTimezone();
  636. if (timezone == DatatypeConstants.FIELD_UNDEFINED) {
  637. timezone = 0;
  638. }
  639. XMLGregorianCalendar gc = this;
  640. if (timezone != 0) {
  641. gc = this.normalize();
  642. }
  643. return gc.getYear()
  644. + gc.getMonth()
  645. + gc.getDay()
  646. + gc.getHour()
  647. + gc.getMinute()
  648. + gc.getSecond();
  649. }
  650. /**
  651. * <p>Return the lexical representation of <code>this</code> instance.
  652. * The format is specified in
  653. * <a href="http://www.w3.org/TR/xmlschema-2/#dateTime-order">XML Schema 1.0 Part 2, Section 3.2.[7-14].1,
  654. * <i>Lexical Representation</i>".</a></p>
  655. *
  656. * <p>Specific target lexical representation format is determined by
  657. * {@link #getXMLSchemaType()}.</p>
  658. *
  659. * @return XML, as <code>String</code>, representation of this <code>XMLGregorianCalendar</code>
  660. *
  661. * @throws IllegalStateException if the combination of set fields
  662. * does not match one of the eight defined XML Schema builtin date/time datatypes.
  663. */
  664. public abstract String toXMLFormat();
  665. /**
  666. * <p>Return the name of the XML Schema date/time type that this instance
  667. * maps to. Type is computed based on fields that are set.</p>
  668. *
  669. * <table border="2" rules="all" cellpadding="2">
  670. * <thead>
  671. * <tr>
  672. * <th align="center" colspan="7">
  673. * Required fields for XML Schema 1.0 Date/Time Datatypes.<br/>
  674. * <i>(timezone is optional for all date/time datatypes)</i>
  675. * </th>
  676. * </tr>
  677. * </thead>
  678. * <tbody>
  679. * <tr>
  680. * <td>Datatype</td>
  681. * <td>year</td>
  682. * <td>month</td>
  683. * <td>day</td>
  684. * <td>hour</td>
  685. * <td>minute</td>
  686. * <td>second</td>
  687. * </tr>
  688. * <tr>
  689. * <td>{@link DatatypeConstants#DATETIME}</td>
  690. * <td>X</td>
  691. * <td>X</td>
  692. * <td>X</td>
  693. * <td>X</td>
  694. * <td>X</td>
  695. * <td>X</td>
  696. * </tr>
  697. * <tr>
  698. * <td>{@link DatatypeConstants#DATE}</td>
  699. * <td>X</td>
  700. * <td>X</td>
  701. * <td>X</td>
  702. * <td></td>
  703. * <td></td>
  704. * <td></td>
  705. * </tr>
  706. * <tr>
  707. * <td>{@link DatatypeConstants#TIME}</td>
  708. * <td></td>
  709. * <td></td>
  710. * <td></td>
  711. * <td>X</td>
  712. * <td>X</td>
  713. * <td>X</td>
  714. * </tr>
  715. * <tr>
  716. * <td>{@link DatatypeConstants#GYEARMONTH}</td>
  717. * <td>X</td>
  718. * <td>X</td>
  719. * <td></td>
  720. * <td></td>
  721. * <td></td>
  722. * <td></td>
  723. * </tr>
  724. * <tr>
  725. * <td>{@link DatatypeConstants#GMONTHDAY}</td>
  726. * <td></td>
  727. * <td>X</td>
  728. * <td>X</td>
  729. * <td></td>
  730. * <td></td>
  731. * <td></td>
  732. * </tr>
  733. * <tr>
  734. * <td>{@link DatatypeConstants#GYEAR}</td>
  735. * <td>X</td>
  736. * <td></td>
  737. * <td></td>
  738. * <td></td>
  739. * <td></td>
  740. * <td></td>
  741. * </tr>
  742. * <tr>
  743. * <td>{@link DatatypeConstants#GMONTH}</td>
  744. * <td></td>
  745. * <td>X</td>
  746. * <td></td>
  747. * <td></td>
  748. * <td></td>
  749. * <td></td>
  750. * </tr>
  751. * <tr>
  752. * <td>{@link DatatypeConstants#GDAY}</td>
  753. * <td></td>
  754. * <td></td>
  755. * <td>X</td>
  756. * <td></td>
  757. * <td></td>
  758. * <td></td>
  759. * </tr>
  760. * </tbody>
  761. * </table>
  762. *
  763. * @throws java.lang.IllegalStateException if the combination of set fields
  764. * does not match one of the eight defined XML Schema builtin
  765. * date/time datatypes.
  766. * @return One of the following class constants:
  767. * {@link DatatypeConstants#DATETIME},
  768. * {@link DatatypeConstants#TIME},
  769. * {@link DatatypeConstants#DATE},
  770. * {@link DatatypeConstants#GYEARMONTH},
  771. * {@link DatatypeConstants#GMONTHDAY},
  772. * {@link DatatypeConstants#GYEAR},
  773. * {@link DatatypeConstants#GMONTH} or
  774. * {@link DatatypeConstants#GDAY}.
  775. */
  776. public abstract QName getXMLSchemaType();
  777. /**
  778. * <p>Returns a <code>String</code> representation of this <code>XMLGregorianCalendar</code> <code>Object</code>.</p>
  779. *
  780. * <p>The result is a lexical representation generated by {@link #toXMLFormat()}.</p>
  781. *
  782. * @return A non-<code>null</code> valid <code>String</code> representation of this <code>XMLGregorianCalendar</code>.
  783. *
  784. * @throws IllegalStateException if the combination of set fields
  785. * does not match one of the eight defined XML Schema builtin date/time datatypes.
  786. *
  787. * @see #toXMLFormat()
  788. */
  789. public String toString() {
  790. return toXMLFormat();
  791. }
  792. /**
  793. * Validate instance by <code>getXMLSchemaType()</code> constraints.
  794. * @return true if data values are valid.
  795. */
  796. public abstract boolean isValid();
  797. /**
  798. * <p>Add <code>duration</code> to this instance.</p>
  799. *
  800. * <p>The computation is specified in
  801. * <a href="http://www.w3.org/TR/xmlschema-2/#adding-durations-to-dateTimes">XML Schema 1.0 Part 2, Appendix E,
  802. * <i>Adding durations to dateTimes</i>></a>.
  803. * <a href="#datetimefieldsmapping">date/time field mapping table</a>
  804. * defines the mapping from XML Schema 1.0 <code>dateTime</code> fields
  805. * to this class' representation of those fields.</p>
  806. *
  807. * @param duration Duration to add to this <code>XMLGregorianCalendar</code>.
  808. *
  809. * @throws NullPointerException when <code>duration</code> parameter is <code>null</code>.
  810. */
  811. public abstract void add(Duration duration);
  812. /**
  813. * <p>Convert this <code>XMLGregorianCalendar</code> to a {@link GregorianCalendar}.</p>
  814. *
  815. * <p>When <code>this</code> instance has an undefined field, this
  816. * conversion relies on the <code>java.util.GregorianCalendar</code> default
  817. * for its corresponding field. A notable difference between
  818. * XML Schema 1.0 date/time datatypes and <code>java.util.GregorianCalendar</code>
  819. * is that Timezone value is optional for date/time datatypes and it is
  820. * a required field for <code>java.util.GregorianCalendar</code>. See javadoc
  821. * for <code>java.util.TimeZone.getDefault()</code> on how the default
  822. * is determined. To explicitly specify the <code>TimeZone</code>
  823. * instance, see
  824. * {@link #toGregorianCalendar(TimeZone, Locale, XMLGregorianCalendar)}.</p>
  825. *
  826. * <table border="2" rules="all" cellpadding="2">
  827. * <thead>
  828. * <tr>
  829. * <th align="center" colspan="2">
  830. * Field by Field Conversion from this class to
  831. * <code>java.util.GregorianCalendar</code>
  832. * </th>
  833. * </tr>
  834. * </thead>
  835. * <tbody>
  836. * <tr>
  837. * <td><code>java.util.GregorianCalendar</code> field</td>
  838. * <td><code>javax.xml.datatype.XMLGregorianCalendar</code> field</td>
  839. * </tr>
  840. * <tr>
  841. * <td><code>ERA</code></td>
  842. * <td>{@link #getEonAndYear()}<code>.signum() < 0 ? GregorianCalendar.BC : GregorianCalendar.AD</code></td>
  843. * </tr>
  844. * <tr>
  845. * <td><code>YEAR</code></td>
  846. * <td>{@link #getEonAndYear()}<code>.abs().intValue()</code><i>*</i></td>
  847. * </tr>
  848. * <tr>
  849. * <td><code>MONTH</code></td>
  850. * <td>{@link #getMonth()} - {@link DatatypeConstants#JANUARY} + {@link GregorianCalendar#JANUARY}</td>
  851. * </tr>
  852. * <tr>
  853. * <td><code>DAY_OF_MONTH</code></td>
  854. * <td>{@link #getDay()}</td>
  855. * </tr>
  856. * <tr>
  857. * <td><code>HOUR_OF_DAY</code></td>
  858. * <td>{@link #getHour()}</td>
  859. * </tr>
  860. * <tr>
  861. * <td><code>MINUTE</code></td>
  862. * <td>{@link #getMinute()}</td>
  863. * </tr>
  864. * <tr>
  865. * <td><code>SECOND</code></td>
  866. * <td>{@link #getSecond()}</td>
  867. * </tr>
  868. * <tr>
  869. * <td><code>MILLISECOND</code></td>
  870. * <td>get millisecond order from {@link #getFractionalSecond()}<i>*</i> </td>
  871. * </tr>
  872. * <tr>
  873. * <td><code>GregorianCalendar.setTimeZone(TimeZone)</code></td>
  874. * <td>{@link #getTimezone()} formatted into Custom timezone id</td>
  875. * </tr>
  876. * </tbody>
  877. * </table>
  878. * <i>*</i> designates possible loss of precision during the conversion due
  879. * to source datatype having higher precision than target datatype.
  880. *
  881. * <p>To ensure consistency in conversion implementations, the new
  882. * <code>GregorianCalendar</code> should be instantiated in following
  883. * manner.
  884. * <ul>
  885. * <li>Using <code>timeZone</code> value as defined above, create a new
  886. * <code>java.util.GregorianCalendar(timeZone,Locale.getDefault())</code>.
  887. * </li>
  888. * <li>Initialize all GregorianCalendar fields by calling {(@link GegorianCalendar#clear()}.</li>
  889. * <li>Obtain a pure Gregorian Calendar by invoking
  890. * <code>GregorianCalendar.setGregorianChange(
  891. * new Date(Long.MIN_VALUE))</code>.</li>
  892. * <li>Its fields ERA, YEAR, MONTH, DAY_OF_MONTH, HOUR_OF_DAY,
  893. * MINUTE, SECOND and MILLISECOND are set using the method
  894. * <code>Calendar.set(int,int)</code></li>
  895. * </ul>
  896. * </p>
  897. *
  898. * @see #toGregorianCalendar(java.util.TimeZone, java.util.Locale, XMLGregorianCalendar)
  899. */
  900. public abstract GregorianCalendar toGregorianCalendar();
  901. /**
  902. * <p>Convert this <code>XMLGregorianCalendar</code> along with provided parameters
  903. * to a {@link GregorianCalendar} instance.</p>
  904. *
  905. * <p> Since XML Schema 1.0 date/time datetypes has no concept of
  906. * timezone ids or daylight savings timezone ids, this conversion operation
  907. * allows the user to explicitly specify one with
  908. * <code>timezone</code> parameter.</p>
  909. *
  910. * <p>To compute the return value's <code>TimeZone</code> field,
  911. * <ul>
  912. * <li>when parameter <code>timeZone</code> is non-null,
  913. * it is the timezone field.</li>
  914. * <li>else when <code>this.getTimezone() != FIELD_UNDEFINED</code>,
  915. * create a <code>java.util.TimeZone</code> with a custom timezone id
  916. * using the <code>this.getTimezone()</code>.</li>
  917. * <li>else when <code>defaults.getTimezone() != FIELD_UNDEFINED</code>,
  918. * create a <code>java.util.TimeZone</code> with a custom timezone id
  919. * using <code>defaults.getTimezone()</code>.</li>
  920. * <li>else use the <code>GregorianCalendar</code> default timezone value
  921. * for the host is defined as specified by
  922. * <code>java.util.TimeZone.getDefault()</code>.</li></p>
  923. *
  924. * <p>To ensure consistency in conversion implementations, the new
  925. * <code>GregorianCalendar</code> should be instantiated in following
  926. * manner.
  927. * <ul>
  928. * <li>Create a new <code>java.util.GregorianCalendar(TimeZone,
  929. * Locale)</code> with TimeZone set as specified above and the
  930. * <code>Locale</code> parameter.
  931. * </li>
  932. * <li>Initialize all GregorianCalendar fields by calling {@link GregorianCalendar#clear()}</li>
  933. * <li>Obtain a pure Gregorian Calendar by invoking
  934. * <code>GregorianCalendar.setGregorianChange(
  935. * new Date(Long.MIN_VALUE))</code>.</li>
  936. * <li>Its fields ERA, YEAR, MONTH, DAY_OF_MONTH, HOUR_OF_DAY,
  937. * MINUTE, SECOND and MILLISECOND are set using the method
  938. * <code>Calendar.set(int,int)</code></li>
  939. * </ul>
  940. *
  941. * @param timezone provide Timezone. <code>null</code> is a legal value.
  942. * @param aLocale provide explicit Locale. Use default GregorianCalendar locale if
  943. * value is <code>null</code>.
  944. * @param defaults provide default field values to use when corresponding
  945. * field for this instance is FIELD_UNDEFINED or null.
  946. * If <code>defaults</code>is <code>null</code> or a field
  947. * within the specified <code>defaults</code> is undefined,
  948. * just use <code>java.util.GregorianCalendar</code> defaults.
  949. * @return a java.util.GregorianCalendar conversion of this instance.
  950. */
  951. public abstract GregorianCalendar toGregorianCalendar(
  952. java.util.TimeZone timezone,
  953. java.util.Locale aLocale,
  954. XMLGregorianCalendar defaults);
  955. /**
  956. * <p>Returns a <code>java.util.TimeZone</code> for this class.</p>
  957. *
  958. * <p>If timezone field is defined for this instance,
  959. * returns TimeZone initialized with custom timezone id
  960. * of zoneoffset. If timezone field is undefined,
  961. * try the defaultZoneoffset that was passed in.
  962. * If defaultZoneoffset is FIELD_UNDEFINED, return
  963. * default timezone for this host.
  964. * (Same default as java.util.GregorianCalendar).</p>
  965. *
  966. * @param defaultZoneoffset default zoneoffset if this zoneoffset is
  967. * {@link DatatypeConstants#FIELD_UNDEFINED}.
  968. *
  969. * @return TimeZone for this.
  970. */
  971. public abstract TimeZone getTimeZone(int defaultZoneoffset);
  972. /**
  973. * <p>Creates and returns a copy of this object.</p>
  974. *
  975. * @return copy of this <code>Object</code>
  976. */
  977. public abstract Object clone();
  978. }