Java Code Examples for javax.xml.datatype.XMLGregorianCalendar#getTimezone()

The following examples show how to use javax.xml.datatype.XMLGregorianCalendar#getTimezone() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultTimeLib.java    From jdmn with Apache License 2.0 5 votes vote down vote up
public Duration timeOffset(XMLGregorianCalendar date) {
    if (date == null) {
        return null;
    }

    int secondsOffset = date.getTimezone();
    if (secondsOffset == DatatypeConstants.FIELD_UNDEFINED) {
        return null;
    } else {
        return this.dataTypeFactory.newDuration((long) secondsOffset * 1000);
    }
}
 
Example 2
Source File: SimpleCaptureDemo.java    From fosstrak-epcis with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Retrieves the timezone from the given eventTime and formats it
 * appropriately.
 */
protected static String getTimeZoneOffset(XMLGregorianCalendar eventTime) {
	int timezone = eventTime.getTimezone();
	int h = Math.abs(timezone / 60);
	int m = Math.abs(timezone % 60);
	DecimalFormat format = new DecimalFormat("00");
	String sign = (timezone < 0) ? "-" : "+";
	return sign + format.format(h) + ":" + format.format(m);
}
 
Example 3
Source File: CalendarUtilTest.java    From hsac-fitnesse-fixtures with Apache License 2.0 5 votes vote down vote up
/**
 * Test AddMonths.
 * Result should be months +2 and no TimeZoneShift
 */
@Test
public void testAddMonths() {
    XMLGregorianCalendar cal = calendarUtil.buildXMLGregorianCalendar();
    int tz = cal.getTimezone();
    XMLGregorianCalendar result = addMonths(2, cal);
    assertEquals(8, result.getMonth());
    assertEquals(0, result.getTimezone() - tz);
}
 
Example 4
Source File: CalendarUtilTest.java    From hsac-fitnesse-fixtures with Apache License 2.0 5 votes vote down vote up
/**
 * Test AddMonths.
 * Result should be months +2 and no TimeZoneShift
 */
@Test
public void testSubstractMonths() {
    XMLGregorianCalendar cal = calendarUtil.buildXMLGregorianCalendar();
    int tz = cal.getTimezone();
    XMLGregorianCalendar result = addMonths(-2, cal);
    assertEquals(4, result.getMonth());
    assertEquals(0, result.getTimezone() - tz);
}
 
Example 5
Source File: CalendarUtilTest.java    From hsac-fitnesse-fixtures with Apache License 2.0 5 votes vote down vote up
/**
 * Test addYears.
 */
@Test
public void testAddYears() {
    XMLGregorianCalendar cal = calendarUtil.buildXMLGregorianCalendar();
    int tz = cal.getTimezone();
    XMLGregorianCalendar result = addYears(2, cal);
    assertEquals(2002, result.getYear());
    assertEquals(0, result.getTimezone() - tz);
}
 
Example 6
Source File: CalendarUtilTest.java    From hsac-fitnesse-fixtures with Apache License 2.0 5 votes vote down vote up
/**
 * Test addYears.
 */
@Test
public void testSubstractYears() {
    XMLGregorianCalendar cal = calendarUtil.buildXMLGregorianCalendar();
    int tz = cal.getTimezone();
    XMLGregorianCalendar result = addYears(-2, cal);
    assertEquals(1998, result.getYear());
    assertEquals(0, result.getTimezone() - tz);
}
 
Example 7
Source File: XMLGregorianCalendarTypeInfoCompiler.java    From jsonix-schema-compiler with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public JSAssignmentExpression createValue(MappingCompiler<T, C> mappingCompiler, String item) {
	final JSCodeModel codeModel = mappingCompiler.getCodeModel();
	final JSObjectLiteral result = codeModel.object();
	final XMLGregorianCalendar calendar = datatypeFactory.newXMLGregorianCalendar(item);
	if (calendar.getYear() != DatatypeConstants.FIELD_UNDEFINED) {
		result.append("year", codeModel.integer(calendar.getYear()));
	}
	if (calendar.getMonth() != DatatypeConstants.FIELD_UNDEFINED) {
		result.append("month", codeModel.integer(calendar.getMonth()));
	}
	if (calendar.getDay() != DatatypeConstants.FIELD_UNDEFINED) {
		result.append("day", codeModel.integer(calendar.getDay()));
	}
	if (calendar.getHour() != DatatypeConstants.FIELD_UNDEFINED) {
		result.append("hour", codeModel.integer(calendar.getHour()));
	}
	if (calendar.getMinute() != DatatypeConstants.FIELD_UNDEFINED) {
		result.append("minute", codeModel.integer(calendar.getMinute()));
	}
	if (calendar.getSecond() != DatatypeConstants.FIELD_UNDEFINED) {
		result.append("second", codeModel.integer(calendar.getSecond()));
	}
	if (calendar.getFractionalSecond() != null) {
		result.append("second", codeModel.decimal(calendar.getFractionalSecond().toString()));
	}
	if (calendar.getTimezone() != DatatypeConstants.FIELD_UNDEFINED) {
		result.append("timeZone", codeModel.integer(calendar.getTimezone()));
	}
	return result;
}
 
Example 8
Source File: XMLGregorianCalendarTypeInfoProducer.java    From jsonix-schema-compiler with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public JsonValue createValue(JsonSchemaMappingCompiler<T, C> mappingCompiler, String item) {
	final JsonObjectBuilder objectBuilder = mappingCompiler.getJsonBuilderFactory().createObjectBuilder();
	final XMLGregorianCalendar calendar = datatypeFactory.newXMLGregorianCalendar(item);
	if (calendar.getYear() != DatatypeConstants.FIELD_UNDEFINED) {
		objectBuilder.add("year", calendar.getYear());
	}
	if (calendar.getMonth() != DatatypeConstants.FIELD_UNDEFINED) {
		objectBuilder.add("month", calendar.getMonth());
	}
	if (calendar.getDay() != DatatypeConstants.FIELD_UNDEFINED) {
		objectBuilder.add("day", calendar.getDay());
	}
	if (calendar.getHour() != DatatypeConstants.FIELD_UNDEFINED) {
		objectBuilder.add("hour", calendar.getHour());
	}
	if (calendar.getMinute() != DatatypeConstants.FIELD_UNDEFINED) {
		objectBuilder.add("minute", calendar.getMinute());
	}
	if (calendar.getSecond() != DatatypeConstants.FIELD_UNDEFINED) {
		objectBuilder.add("second", calendar.getSecond());
	}
	if (calendar.getFractionalSecond() != null) {
		objectBuilder.add("second", calendar.getFractionalSecond());
	}
	if (calendar.getTimezone() != DatatypeConstants.FIELD_UNDEFINED) {
		objectBuilder.add("timeZone", calendar.getTimezone());
	}
	return objectBuilder.build();
}
 
Example 9
Source File: AbstractTypeTestClient.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected boolean equalsDate(XMLGregorianCalendar orig, XMLGregorianCalendar actual) {
    boolean result = false;

    if ((orig.getYear() == actual.getYear()) && (orig.getMonth() == actual.getMonth())
        && (orig.getDay() == actual.getDay()) && (actual.getHour() == DatatypeConstants.FIELD_UNDEFINED)
        && (actual.getMinute() == DatatypeConstants.FIELD_UNDEFINED)
        && (actual.getSecond() == DatatypeConstants.FIELD_UNDEFINED)
        && (actual.getMillisecond() == DatatypeConstants.FIELD_UNDEFINED)) {

        result = orig.getTimezone() == actual.getTimezone();
    }
    return result;
}
 
Example 10
Source File: AbstractTypeTestClient.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected boolean equalsTime(XMLGregorianCalendar orig, XMLGregorianCalendar actual) {
    boolean result = false;
    if ((orig.getHour() == actual.getHour()) && (orig.getMinute() == actual.getMinute())
        && (orig.getSecond() == actual.getSecond()) && (orig.getMillisecond() == actual.getMillisecond())
        && (orig.getTimezone() == actual.getTimezone())) {
        result = true;
    }
    return result;
}
 
Example 11
Source File: AbstractTypeTestClient.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected boolean equalsDateTime(XMLGregorianCalendar orig, XMLGregorianCalendar actual) {
    boolean result = false;
    if ((orig.getYear() == actual.getYear()) && (orig.getMonth() == actual.getMonth())
        && (orig.getDay() == actual.getDay()) && (orig.getHour() == actual.getHour())
        && (orig.getMinute() == actual.getMinute()) && (orig.getSecond() == actual.getSecond())
        && (orig.getMillisecond() == actual.getMillisecond())) {

        result = orig.getTimezone() == actual.getTimezone();
    }
    return result;
}
 
Example 12
Source File: XmlUtilities.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Converts the given XML Gregorian calendar to a date.
 *
 * @param  context  the current (un)marshalling context, or {@code null} if none.
 * @param  xml      the XML calendar to convert to a date, or {@code null}.
 * @return the date, or {@code null} if {@code xml} was null.
 */
public static Date toDate(final Context context, final XMLGregorianCalendar xml) {
    if (xml != null) {
        final GregorianCalendar calendar =  xml.toGregorianCalendar();
        if (context != null && xml.getTimezone() == FIELD_UNDEFINED) {
            final TimeZone timezone = context.getTimeZone();
            if (timezone != null) {
                calendar.setTimeZone(timezone);
            }
        }
        return calendar.getTime();
    }
    return null;
}
 
Example 13
Source File: DateTimeCast.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected Literal convert(ValueFactory vf, Value value) throws ValueExprEvaluationException {
	if (value instanceof Literal) {
		Literal literal = (Literal) value;
		IRI datatype = literal.getDatatype();

		if (datatype.equals(XMLSchema.DATE)) {
			// If ST is xs:date, then let SYR be eg:convertYearToString(
			// fn:year-from-date( SV )), let SMO be eg:convertTo2CharString(
			// fn:month-from-date( SV )), let SDA be eg:convertTo2CharString(
			// fn:day-from-date( SV )) and let STZ be eg:convertTZtoString(
			// fn:timezone-from-date( SV )); TV is xs:dateTime( fn:concat(
			// SYR , '-', SMO , '-', SDA , 'T00:00:00 ', STZ ) ).
			try {
				XMLGregorianCalendar calValue = literal.calendarValue();

				int year = calValue.getYear();
				int month = calValue.getMonth();
				int day = calValue.getDay();
				int timezoneOffset = calValue.getTimezone();

				if (DatatypeConstants.FIELD_UNDEFINED != year && DatatypeConstants.FIELD_UNDEFINED != month
						&& DatatypeConstants.FIELD_UNDEFINED != day) {
					StringBuilder dtBuilder = new StringBuilder();
					dtBuilder.append(year);
					dtBuilder.append("-");
					if (month < 10) {
						dtBuilder.append("0");
					}
					dtBuilder.append(month);
					dtBuilder.append("-");
					if (day < 10) {
						dtBuilder.append("0");
					}
					dtBuilder.append(day);
					dtBuilder.append("T00:00:00");
					if (DatatypeConstants.FIELD_UNDEFINED != timezoneOffset) {
						int minutes = Math.abs(timezoneOffset);
						int hours = minutes / 60;
						minutes = minutes - (hours * 60);
						if (timezoneOffset > 0) {
							dtBuilder.append("+");
						} else {
							dtBuilder.append("-");
						}
						if (hours < 10) {
							dtBuilder.append("0");
						}
						dtBuilder.append(hours);
						dtBuilder.append(":");
						if (minutes < 10) {
							dtBuilder.append("0");
						}
						dtBuilder.append(minutes);
					}

					return vf.createLiteral(dtBuilder.toString(), XMLSchema.DATETIME);
				} else {
					throw typeError(literal, null);
				}
			} catch (IllegalArgumentException e) {
				throw typeError(literal, e);
			}
		}
	}
	throw typeError(value, null);
}
 
Example 14
Source File: Timezone.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	if (args.length != 1) {
		throw new ValueExprEvaluationException("TIMEZONE requires 1 argument, got " + args.length);
	}

	Value argValue = args[0];
	if (argValue instanceof Literal) {
		Literal literal = (Literal) argValue;

		IRI datatype = literal.getDatatype();

		if (datatype != null && XMLDatatypeUtil.isCalendarDatatype(datatype)) {
			try {
				XMLGregorianCalendar calValue = literal.calendarValue();

				int timezoneOffset = calValue.getTimezone();

				if (DatatypeConstants.FIELD_UNDEFINED != timezoneOffset) {
					// TODO creating xsd:dayTimeDuration lexical representation
					// manually. Surely there is a better way to do this?
					int minutes = Math.abs(timezoneOffset);
					int hours = minutes / 60;
					minutes = minutes - (hours * 60);

					StringBuilder tzDuration = new StringBuilder();
					if (timezoneOffset < 0) {
						tzDuration.append("-");
					}
					tzDuration.append("PT");
					if (hours > 0) {
						tzDuration.append(hours + "H");
					}
					if (minutes > 0) {
						tzDuration.append(minutes + "M");
					}
					if (timezoneOffset == 0) {
						tzDuration.append("0S");
					}
					return valueFactory.createLiteral(tzDuration.toString(), XMLSchema.DAYTIMEDURATION);
				} else {
					throw new ValueExprEvaluationException("can not determine timezone from value: " + argValue);
				}
			} catch (IllegalArgumentException e) {
				throw new ValueExprEvaluationException("illegal calendar value: " + argValue);
			}
		} else {
			throw new ValueExprEvaluationException("unexpected input value for function: " + argValue);
		}
	} else {
		throw new ValueExprEvaluationException("unexpected input value for function: " + args[0]);
	}
}
 
Example 15
Source File: PDTXMLConverter.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
/**
 * Create a new {@link XMLGregorianCalendar} using separate objects for date
 * and time.
 *
 * @param aDate
 *        Source date. May be <code>null</code>.
 * @param aTime
 *        Source time. May be <code>null</code>.
 * @return <code>null</code> if the passed date and time are <code>null</code>
 *         .
 */
@Nullable
public static XMLGregorianCalendar getXMLCalendar (@Nullable final XMLGregorianCalendar aDate,
                                                   @Nullable final XMLGregorianCalendar aTime)
{
  if (aDate == null && aTime == null)
    return null;
  if (aTime == null)
  {
    // Date only
    return s_aDTFactory.newXMLGregorianCalendar (aDate.getYear (),
                                                 aDate.getMonth (),
                                                 aDate.getDay (),
                                                 0,
                                                 0,
                                                 0,
                                                 0,
                                                 aDate.getTimezone ());
  }
  if (aDate == null)
  {
    // Time only
    return s_aDTFactory.newXMLGregorianCalendar (0,
                                                 0,
                                                 0,
                                                 aTime.getHour (),
                                                 aTime.getMinute (),
                                                 aTime.getSecond (),
                                                 aTime.getMillisecond (),
                                                 aTime.getTimezone ());
  }

  if (aDate.getTimezone () != aTime.getTimezone ())
    LOGGER.warn ("Date and time have different timezones: " + aDate.getTimezone () + " vs. " + aTime.getTimezone ());
  return s_aDTFactory.newXMLGregorianCalendar (aDate.getYear (),
                                               aDate.getMonth (),
                                               aDate.getDay (),
                                               aTime.getHour (),
                                               aTime.getMinute (),
                                               aTime.getSecond (),
                                               aTime.getMillisecond (),
                                               aDate.getTimezone ());
}