/** * <p>Normalize this instance to UTC.</p> * * <p>2000-03-04T23:00:00+03:00 normalizes to 2000-03-04T20:00:00Z</p> * <p>Implements W3C XML Schema Part 2, Section 3.2.7.3 (A).</p> */ public XMLGregorianCalendar normalize() { XMLGregorianCalendar normalized = normalizeToTimezone(timezone); // if timezone was undefined, leave it undefined if (getTimezone() == DatatypeConstants.FIELD_UNDEFINED) { normalized.setTimezone(DatatypeConstants.FIELD_UNDEFINED); } // if milliseconds was undefined, leave it undefined if (getMillisecond() == DatatypeConstants.FIELD_UNDEFINED) { normalized.setMillisecond(DatatypeConstants.FIELD_UNDEFINED); } return normalized; }
/** * <p>Create a Java instance of XML Schema builtin datatype dateTime.</p> * * @param year represents both high-order eons and low-order year. * @param month of <code>dateTime</code> * @param day of <code>dateTime</code> * @param hour of <code>dateTime</code> * @param minute of <code>dateTime</code> * @param second of <code>dateTime</code> * * @return <code>XMLGregorianCalendar</code> created from parameter values. * * @throws IllegalArgumentException if any parameter is outside value constraints for the field as specified in * <a href="#datetimefieldmapping">date/time field mapping table</a>. * * @see DatatypeConstants#FIELD_UNDEFINED */ public static XMLGregorianCalendar createDateTime( int year, int month, int day, int hour, int minute, int second) { return new XMLGregorianCalendarImpl( year, month, day, hour, minute, second, DatatypeConstants.FIELD_UNDEFINED, //millisecond DatatypeConstants.FIELD_UNDEFINED //timezone ); }
/** * Create a Java instance of XML Schema builtin datatype <code>time</code>. * @param hours number of hours * @param minutes number of minutes * @param seconds number of seconds * @param timezone offset in minutes. {@link DatatypeConstants#FIELD_UNDEFINED} indicates optional field is not set. * * @return <code>XMLGregorianCalendar</code> created from parameter values. * * @see DatatypeConstants#FIELD_UNDEFINED * * @throws IllegalArgumentException if any parameter is outside value * constraints for the field as specified in * <a href="#datetimefieldmapping">date/time field mapping table</a>. */ public static XMLGregorianCalendar createTime( int hours, int minutes, int seconds, int timezone) { return new XMLGregorianCalendarImpl( DatatypeConstants.FIELD_UNDEFINED, // Year DatatypeConstants.FIELD_UNDEFINED, // Month DatatypeConstants.FIELD_UNDEFINED, // Day hours, minutes, seconds, DatatypeConstants.FIELD_UNDEFINED, //Millisecond timezone); }
/** * <p>Create a Java instance of XML Schema builtin datatype time.</p> * * @param hours number of hours * @param minutes number of minutes * @param seconds number of seconds * @param fractionalSecond value of <code>null</code> indicates that this optional field is not set. * @param timezone offset in minutes. {@link DatatypeConstants#FIELD_UNDEFINED} indicates optional field is not set. * * @return <code>XMLGregorianCalendar</code> created from parameter values. * * @see DatatypeConstants#FIELD_UNDEFINED * * @throws IllegalArgumentException if any parameter is outside value * constraints for the field as specified in * <a href="#datetimefieldmapping">date/time field mapping table</a>. */ public static XMLGregorianCalendar createTime( int hours, int minutes, int seconds, BigDecimal fractionalSecond, int timezone) { return new XMLGregorianCalendarImpl( null, // Year DatatypeConstants.FIELD_UNDEFINED, // month DatatypeConstants.FIELD_UNDEFINED, // day hours, minutes, seconds, fractionalSecond, timezone); }
/** * <p>Create a Java instance of XML Schema builtin datatype time.</p> * * @param hours number of hours * @param minutes number of minutes * @param seconds number of seconds * @param milliseconds number of milliseconds * @param timezone offset in minutes. {@link DatatypeConstants#FIELD_UNDEFINED} indicates optional field is not set. * * @return <code>XMLGregorianCalendar</code> created from parameter values. * * @see DatatypeConstants#FIELD_UNDEFINED * * @throws IllegalArgumentException if any parameter is outside value * constraints for the field as specified in * <a href="#datetimefieldmapping">date/time field mapping table</a>. */ public static XMLGregorianCalendar createTime( int hours, int minutes, int seconds, int milliseconds, int timezone) { return new XMLGregorianCalendarImpl( DatatypeConstants.FIELD_UNDEFINED, // year DatatypeConstants.FIELD_UNDEFINED, // month DatatypeConstants.FIELD_UNDEFINED, // day hours, minutes, seconds, milliseconds, timezone); }
/** * <p>Return XML Schema 1.0 dateTime datatype field for * <code>year</code>.</p> * * <p>Value constraints for this value are summarized in * <a href="#datetimefield-year">year field of date/time field mapping table</a>.</p> * * @return sum of <code>eon</code> and <code>BigInteger.valueOf(year)</code> * when both fields are defined. When only <code>year</code> is defined, * return it. When both <code>eon</code> and <code>year</code> are not * defined, return <code>null</code>. * * @see #getEon() * @see #getYear() */ public BigInteger getEonAndYear() { // both are defined if (year != DatatypeConstants.FIELD_UNDEFINED && eon != null) { return eon.add(BigInteger.valueOf((long) year)); } // only year is defined if (year != DatatypeConstants.FIELD_UNDEFINED && eon == null) { return BigInteger.valueOf((long) year); } // neither are defined // or only eon is defined which is not valid without a year return null; }
/** * <p>Implement Step B from * http://www.w3.org/TR/xmlschema-2/#dateTime-order.</p> */ private static int compareField(int Pfield, int Qfield) { if (Pfield == Qfield) { //fields are either equal in value or both undefined. // Step B. 1.1 AND optimized result of performing 1.1-1.4. return DatatypeConstants.EQUAL; } else { if (Pfield == DatatypeConstants.FIELD_UNDEFINED || Qfield == DatatypeConstants.FIELD_UNDEFINED) { // Step B. 1.2 return DatatypeConstants.INDETERMINATE; } else { // Step B. 1.3-4. return (Pfield < Qfield ? DatatypeConstants.LESSER : DatatypeConstants.GREATER); } } }
private static int compareField(BigDecimal Pfield, BigDecimal Qfield) { // optimization. especially when both arguments are null. if (Pfield == Qfield) { return DatatypeConstants.EQUAL; } if (Pfield == null) { Pfield = DECIMAL_ZERO; } if (Qfield == null) { Qfield = DECIMAL_ZERO; } return Pfield.compareTo(Qfield); }
/** * <p>Returns a hash code consistent with the definition of the equals method.</p> * * @return hash code of this object. */ public int hashCode() { // Following two dates compare to EQUALS since in different timezones. // 2000-01-15T12:00:00-05:00 == 2000-01-15T13:00:00-04:00 // // Must ensure both instances generate same hashcode by normalizing // this to UTC timezone. int timezone = getTimezone(); if (timezone == DatatypeConstants.FIELD_UNDEFINED) { timezone = 0; } XMLGregorianCalendar gc = this; if (timezone != 0) { gc = this.normalizeToTimezone(getTimezone()); } return gc.getYear() + gc.getMonth() + gc.getDay() + gc.getHour() + gc.getMinute() + gc.getSecond(); }
/** * <p>Constructs a new Duration object by specifying each field * individually.</p> * * <p>This method is functionally equivalent to * invoking another constructor by wrapping * all non-zero parameters into {@link BigInteger} and {@link BigDecimal}. * Zero value of int parameter is equivalent of null value of * the corresponding field.</p> * * @see #DurationImpl(boolean, BigInteger, BigInteger, BigInteger, BigInteger, * BigInteger, BigDecimal) */ protected DurationImpl( final boolean isPositive, final int years, final int months, final int days, final int hours, final int minutes, final int seconds) { this( isPositive, wrap(years), wrap(months), wrap(days), wrap(hours), wrap(minutes), seconds != DatatypeConstants.FIELD_UNDEFINED ? new BigDecimal(String.valueOf(seconds)) : null); }
/** * <p>Gets the value of the field as a {@link BigDecimal}.</p> * * <p>If the field is unset, return 0.</p> * * @param f Field to get value for. * * @return non-null valid {@link BigDecimal}. */ private BigDecimal getFieldAsBigDecimal(DatatypeConstants.Field f) { if (f == DatatypeConstants.SECONDS) { if (seconds != null) { return seconds; } else { return ZERO; } } else { BigInteger bi = (BigInteger) getField(f); if (bi == null) { return ZERO; } else { return new BigDecimal(bi); } } }
/** * <p>Set year of XSD <code>dateTime</code> year field.</p> * * <p>Unset this field by invoking the setter with a parameter value of * {@link DatatypeConstants#FIELD_UNDEFINED}.</p> * * <p>Note: if the absolute value of the <code>year</code> parameter * is less than 10^9, the eon component of the XSD year field is set to * <code>null</code> by this method.</p> * * @param year value constraints are summarized in <a href="#datetimefield-year">year field of date/time field mapping table</a>. * If year is {@link DatatypeConstants#FIELD_UNDEFINED}, then eon is set to <code>null</code>. */ public final void setYear(int year) { if (year == DatatypeConstants.FIELD_UNDEFINED) { this.year = DatatypeConstants.FIELD_UNDEFINED; this.eon = null; } else if (Math.abs(year) < BILLION_I) { this.year = year; this.eon = null; } else { BigInteger theYear = BigInteger.valueOf((long) year); BigInteger remainder = theYear.remainder(BILLION_B); this.year = remainder.intValue(); setEon(theYear.subtract(remainder)); } }
/** * <p>Indicates whether parameter <code>obj</code> is "equal to" this one.</p> * * @param obj to compare. * * @return <code>true</code> when <code>compare(this,(XMLGregorianCalendar)obj) == EQUAL.</code>. */ public boolean equals(Object obj) { if (obj == null || !(obj instanceof XMLGregorianCalendar)) { return false; } if (obj == this) { return true; } return compare((XMLGregorianCalendar) obj) == DatatypeConstants.EQUAL; }
public static XMLGregorianCalendar getXmlDate(Date dateAndTime) { ParamUtil.requireNonNull("dateAndTime", dateAndTime); GregorianCalendar cal = new GregorianCalendar(); cal.setTimeZone(UTC); cal.setTime(dateAndTime); try { XMLGregorianCalendar ret = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal); ret.setMillisecond(DatatypeConstants.FIELD_UNDEFINED); return ret; } catch (DatatypeConfigurationException ex) { return null; } }
/** * <p>Create a Java representation of XML Schema builtin datatype <code>date</code> or <code>g*</code>.</p> * * <p>For example, an instance of <code>gYear</code> can be created invoking this factory * with <code>month</code> and <code>day</code> parameters set to * {@link DatatypeConstants#FIELD_UNDEFINED}.</p> * * @param year of <code>XMLGregorianCalendar</code> to be created. * @param month of <code>XMLGregorianCalendar</code> to be created. * @param day of <code>XMLGregorianCalendar</code> to be created. * @param timezone offset in minutes. {@link DatatypeConstants#FIELD_UNDEFINED} indicates optional field is not set. * * @return <code>XMLGregorianCalendar</code> created from parameter values. * * @see DatatypeConstants#FIELD_UNDEFINED * * @throws IllegalArgumentException if any parameter is outside value * constraints for the field as specified in * <a href="#datetimefieldmapping">date/time field mapping table</a>. */ public static XMLGregorianCalendar createDate( int year, int month, int day, int timezone) { return new XMLGregorianCalendarImpl( year, month, day, DatatypeConstants.FIELD_UNDEFINED, // hour DatatypeConstants.FIELD_UNDEFINED, // minute DatatypeConstants.FIELD_UNDEFINED, // second DatatypeConstants.FIELD_UNDEFINED, // millisecond timezone); }
/** * @return result of adding second and fractional second field */ private BigDecimal getSeconds() { if (second == DatatypeConstants.FIELD_UNDEFINED) { return DECIMAL_ZERO; } BigDecimal result = BigDecimal.valueOf((long) second); if (fractionalSecond != null) { return result.add(fractionalSecond); } else { return result; } }
/** * <p>Set year of XSD <code>dateTime</code> year field.</p> * * <p>Unset this field by invoking the setter with a parameter value of * {@link DatatypeConstants#FIELD_UNDEFINED}.</p> * * <p>Note: if the absolute value of the <code>year</code> parameter * is less than 10^9, the eon component of the XSD year field is set to * <code>null</code> by this method.</p> * * @param year value constraints are summarized in <a href="#datetimefield-year">year field of date/time field mapping table</a>. * If year is {@link DatatypeConstants#FIELD_UNDEFINED}, then eon is set to <code>null</code>. */ public void setYear(int year) { if (year == DatatypeConstants.FIELD_UNDEFINED) { this.year = DatatypeConstants.FIELD_UNDEFINED; this.eon = null; } else if (Math.abs(year) < BILLION.intValue()) { this.year = year; this.eon = null; } else { BigInteger theYear = BigInteger.valueOf((long) year); BigInteger remainder = theYear.remainder(BILLION); this.year = remainder.intValue(); setEon(theYear.subtract(remainder)); } }
private void setHour(int hour, boolean validate) { if (hour < 0 || hour > 24) { if (hour != DatatypeConstants.FIELD_UNDEFINED) { invalidFieldValue(HOUR, hour); } } this.hour = hour; if (validate) { testHour(); } }
/** * <p>Converts the years and months fields into the days field * by using a specific time instant as the reference point.</p> * * <p>For example, duration of one month normalizes to 31 days * given the start time instance "July 8th 2003, 17:40:32".</p> * * <p>Formally, the computation is done as follows:</p> * <ol> * <li>The given Calendar object is cloned. * <li>The years, months and days fields will be added to * the {@link Calendar} object * by using the {@link Calendar#add(int,int)} method. * <li>The difference between two Calendars are computed in terms of days. * <li>The computed days, along with the hours, minutes and seconds * fields of this duration object is used to construct a new * Duration object. * </ol> * * <p>Note that since the Calendar class uses <code>int</code> to * hold the value of year and month, this method may produce * an unexpected result if this duration object holds * a very large value in the years or months fields.</p> * * @param startTimeInstant <code>Calendar</code> reference point. * * @return <code>Duration</code> of years and months of this <code>Duration</code> as days. * * @throws NullPointerException If the startTimeInstant parameter is null. */ public Duration normalizeWith(Calendar startTimeInstant) { Calendar c = (Calendar) startTimeInstant.clone(); // using int may cause overflow, but // Calendar internally treats value as int anyways. c.add(Calendar.YEAR, getYears() * signum); c.add(Calendar.MONTH, getMonths() * signum); c.add(Calendar.DAY_OF_MONTH, getDays() * signum); // obtain the difference in terms of days long diff = getCalendarTimeInMillis(c) - getCalendarTimeInMillis(startTimeInstant); int days = (int) (diff / (1000L * 60L * 60L * 24L)); return new DurationImpl( days >= 0, null, null, wrap(Math.abs(days)), (BigInteger) getField(DatatypeConstants.HOURS), (BigInteger) getField(DatatypeConstants.MINUTES), (BigDecimal) getField(DatatypeConstants.SECONDS)); }
private static int compareField(BigInteger Pfield, BigInteger Qfield) { if (Pfield == null) { return (Qfield == null ? DatatypeConstants.EQUAL : DatatypeConstants.INDETERMINATE); } if (Qfield == null) { return DatatypeConstants.INDETERMINATE; } return Pfield.compareTo(Qfield); }
/** * <p>Indicates whether parameter <code>obj</code> is "equal to" this one.</p> * * @param obj to compare. * * @return <code>true</code> when <code>compare(this,(XMLGregorianCalendar)obj) == EQUAL.</code>. */ public boolean equals(Object obj) { if (obj == null || !(obj instanceof XMLGregorianCalendar)) { return false; } return compare((XMLGregorianCalendar) obj) == DatatypeConstants.EQUAL; }
/** * <p>Return the lexical representation of <code>this</code> instance. * The format is specified in * <a href="http://www.w3.org/TR/xmlschema-2/#dateTime-order">XML Schema 1.0 Part 2, Section 3.2.[7-14].1, * <i>Lexical Representation</i>".</a></p> * * <p>Specific target lexical representation format is determined by * {@link #getXMLSchemaType()}.</p> * * @return XML, as <code>String</code>, representation of this <code>XMLGregorianCalendar</code> * * @throws java.lang.IllegalStateException if the combination of set fields * does not match one of the eight defined XML Schema builtin date/time datatypes. */ public String toXMLFormat() { QName typekind = getXMLSchemaType(); String formatString = null; // Fix 4971612: invalid SCCS macro substitution in data string // no %{alpha}% to avoid SCCS macro substitution if (typekind == DatatypeConstants.DATETIME) { formatString = "%Y-%M-%DT%h:%m:%s" + "%z"; } else if (typekind == DatatypeConstants.DATE) { formatString = "%Y-%M-%D" + "%z"; } else if (typekind == DatatypeConstants.TIME) { formatString = "%h:%m:%s" + "%z"; } else if (typekind == DatatypeConstants.GMONTH) { formatString = "--%M" + "%z"; } else if (typekind == DatatypeConstants.GDAY) { formatString = "---%D" + "%z"; } else if (typekind == DatatypeConstants.GYEAR) { formatString = "%Y" + "%z"; } else if (typekind == DatatypeConstants.GYEARMONTH) { formatString = "%Y-%M" + "%z"; } else if (typekind == DatatypeConstants.GMONTHDAY) { formatString = "--%M-%D" + "%z"; } return format(formatString); }
private static int maximumDayInMonthFor(BigInteger year, int month) { if (month != DatatypeConstants.FEBRUARY) { return daysInMonth[month]; } else { if (year.mod(FOUR_HUNDRED).equals(BigInteger.ZERO) || (!year.mod(HUNDRED).equals(BigInteger.ZERO) && year.mod(FOUR).equals(BigInteger.ZERO))) { // is a leap year. return 29; } else { return daysInMonth[month]; } } }
private static int maximumDayInMonthFor(int year, int month) { if (month != DatatypeConstants.FEBRUARY) { return daysInMonth[month]; } else { if (((year % 400) == 0) || (((year % 100) != 0) && ((year % 4) == 0))) { // is a leap year. return 29; } else { return daysInMonth[DatatypeConstants.FEBRUARY]; } } }
private void newDurationDayTimeTester(boolean isPositive, boolean normalizedIsPositive, BigInteger years, BigInteger normalizedYears, BigInteger months, BigInteger normalizedMonths, BigInteger days, BigInteger normalizedDays, BigInteger hours, BigInteger normalizedHours, BigInteger minutes, BigInteger normalizedMinutes, BigDecimal seconds, BigDecimal normalizedSeconds, long durationInMilliSeconds, long normalizedDurationInMilliSeconds, String lexicalRepresentation, String normalizedLexicalRepresentation) { DatatypeFactory datatypeFactory = null; try { datatypeFactory = DatatypeFactory.newInstance(); } catch (DatatypeConfigurationException ex) { ex.printStackTrace(); Assert.fail(ex.toString()); } // create 4 dayTime Durations using the 4 different constructors Duration durationDayTimeBigInteger = datatypeFactory.newDurationDayTime(isPositive, days, hours, minutes, seconds.toBigInteger()); durationAssertEquals(durationDayTimeBigInteger, DatatypeConstants.DURATION_DAYTIME, normalizedIsPositive, normalizedYears.intValue(), normalizedMonths.intValue(), normalizedDays.intValue(), normalizedHours.intValue(), normalizedMinutes.intValue(), normalizedSeconds.intValue(), normalizedDurationInMilliSeconds, normalizedLexicalRepresentation); /* * Duration durationDayTimeInt = datatypeFactory.newDurationDayTime( * isPositive, days.intValue(), hours.intValue(), minutes.intValue(), * seconds.intValue()); Duration durationDayTimeMilliseconds = * datatypeFactory.newDurationDayTime( durationInMilliSeconds); Duration * durationDayTimeLexical = datatypeFactory.newDurationDayTime( * lexicalRepresentation); * Duration durationYearMonthBigInteger = * datatypeFactory.newDurationYearMonth( isPositive, years, months); * Duration durationYearMonthInt = datatypeFactory.newDurationYearMonth( * isPositive, years.intValue(), months.intValue()); Duration * durationYearMonthMilliseconds = datatypeFactory.newDurationYearMonth( * durationInMilliSeconds); Duration durationYearMonthLexical = * datatypeFactory.newDurationYearMonth( lexicalRepresentation) ; */ }
/** * <p>Constructs a new Duration object by specifying each field individually.</p> * * <p>All the parameters are optional as long as at least one field is present. * If specified, parameters have to be zero or positive.</p> * * @param isPositive Set to <code>false</code> to create a negative duration. When the length * of the duration is zero, this parameter will be ignored. * @param years of this <code>Duration</code> * @param months of this <code>Duration</code> * @param days of this <code>Duration</code> * @param hours of this <code>Duration</code> * @param minutes of this <code>Duration</code> * @param seconds of this <code>Duration</code> * * @throws IllegalArgumentException * If years, months, days, hours, minutes and * seconds parameters are all <code>null</code>. Or if any * of those parameters are negative. */ protected DurationImpl( boolean isPositive, BigInteger years, BigInteger months, BigInteger days, BigInteger hours, BigInteger minutes, BigDecimal seconds) { this.years = years; this.months = months; this.days = days; this.hours = hours; this.minutes = minutes; this.seconds = seconds; this.signum = calcSignum(isPositive); // sanity check if (years == null && months == null && days == null && hours == null && minutes == null && seconds == null) { throw new IllegalArgumentException( //"all the fields are null" DatatypeMessageFormatter.formatMessage(null, "AllFieldsNull", null) ); } testNonNegative(years, DatatypeConstants.YEARS); testNonNegative(months, DatatypeConstants.MONTHS); testNonNegative(days, DatatypeConstants.DAYS); testNonNegative(hours, DatatypeConstants.HOURS); testNonNegative(minutes, DatatypeConstants.MINUTES); testNonNegative(seconds, DatatypeConstants.SECONDS); }
/** * <p>Makes sure that the given number is non-negative. If it is not, * throw {@link IllegalArgumentException}.</p> * * @param n Number to test. * @param f Field to test. */ protected static void testNonNegative(BigInteger n, DatatypeConstants.Field f) { if (n != null && n.signum() < 0) { throw new IllegalArgumentException( DatatypeMessageFormatter.formatMessage(null, "NegativeField", new Object[]{f.toString()}) ); } }
/** * <p>Makes sure that the given number is non-negative. If it is not, * throw {@link IllegalArgumentException}.</p> * * @param n Number to test. * @param f Field to test. */ protected static void testNonNegative(BigDecimal n, DatatypeConstants.Field f) { if (n != null && n.signum() < 0) { throw new IllegalArgumentException( DatatypeMessageFormatter.formatMessage(null, "NegativeField", new Object[]{f.toString()}) ); } }
/** * TODO: Javadoc * * @param i int to convert to BigInteger. * * @return BigInteger representation of int. */ protected static BigInteger wrap(final int i) { // field may not be set if (i == DatatypeConstants.FIELD_UNDEFINED) { return null; } // int -> BigInteger return new BigInteger(String.valueOf(i)); }
private int compareResults(int resultA, int resultB){ if ( resultB == DatatypeConstants.INDETERMINATE ) { return DatatypeConstants.INDETERMINATE; } else if ( resultA!=resultB) { return DatatypeConstants.INDETERMINATE; } return resultA; }
protected Duration getDuration(DateTimeData date) { int sign = 1; if ( date.year<0 || date.month<0) { sign = -1; } return datatypeFactory.newDuration(sign == 1, date.year != DatatypeConstants.FIELD_UNDEFINED?BigInteger.valueOf(sign*date.year):null, date.month != DatatypeConstants.FIELD_UNDEFINED?BigInteger.valueOf(sign*date.month):null, null, null, null, null); }
/** * <p>Return the requested field value as an int.</p> * * <p>If field is not set, i.e. == null, 0 is returned.</p> * * @param field To get value for. * * @return int value of field or 0 if field is not set. */ private int getInt(DatatypeConstants.Field field) { Number n = getField(field); if (n == null) { return 0; } else { return n.intValue(); } }
public DurationDayTimeImpl( boolean isPositive, int days, int hours, int minutes, int seconds) { this( isPositive, wrap(days), wrap(hours), wrap(minutes), (seconds != DatatypeConstants.FIELD_UNDEFINED ? new BigDecimal(String.valueOf(seconds)) : null)); }