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