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

The following examples show how to use javax.xml.datatype.XMLGregorianCalendar#toGregorianCalendar() . 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: 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 2
Source File: Bug7042647Test.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test() throws DatatypeConfigurationException {
    XMLGregorianCalendar xmlCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(1970, 1, 1, 0, 0, 0, 0, 0);
    GregorianCalendar calendar = xmlCalendar.toGregorianCalendar();
    int firstDayOfWeek = calendar.getFirstDayOfWeek();
    Calendar defaultCalendar = Calendar.getInstance();
    int defaultFirstDayOfWeek = defaultCalendar.getFirstDayOfWeek();
    if (firstDayOfWeek != defaultFirstDayOfWeek) {
        Assert.fail("Failed firstDayOfWeek=" + firstDayOfWeek + " != defaultFirstDayOfWeek=" + defaultFirstDayOfWeek);
    } else {
        System.out.println("Success firstDayOfWeek=" + firstDayOfWeek + " == defaultFirstDayOfWeek=" + defaultFirstDayOfWeek);
    }
}
 
Example 3
Source File: XsDateTimeFormat.java    From cxf with Apache License 2.0 5 votes vote down vote up
public Object parseObject(String pString, ParsePosition pParsePosition) {
    if (pString == null) {
        throw new NullPointerException("The String argument must not be null.");
    }
    if (pParsePosition == null) {
        throw new NullPointerException("The ParsePosition argument must not be null.");
    }
    int offset = pParsePosition.getIndex();
    int idxSpc = pString.indexOf(' ', offset);
    int idxCom = pString.indexOf(',', offset);

    if (idxCom != -1 && idxCom < idxSpc) {
        idxSpc = idxCom;
    }
    String newVal = null;
    if (idxSpc == -1) {
        newVal = pString.substring(offset);
    } else {
        newVal = pString.substring(offset, idxSpc);
    }
    DatatypeFactory factory;
    try {
        factory = DatatypeFactory.newInstance();
        XMLGregorianCalendar cal = factory.newXMLGregorianCalendar(newVal);

        pParsePosition.setIndex(idxSpc);
        return cal.toGregorianCalendar();
    } catch (DatatypeConfigurationException e) {
        pParsePosition.setErrorIndex(offset);
    }
    return null;
}
 
Example 4
Source File: TimeTest.java    From caravan with Apache License 2.0 5 votes vote down vote up
@Test
public void test() {
  Date date = new Date(-62135798400000L);
  GregorianCalendar c = DateValues.toGregorianCalendar(date);
  XMLGregorianCalendar xmlc = dataTypeFactory.newXMLGregorianCalendar(c);
  GregorianCalendar c2 = xmlc.toGregorianCalendar();

  System.out.println(date);
  System.out.println(c);
  System.out.println(xmlc);
  System.out.println(c2.getTime());
}
 
Example 5
Source File: PDTXMLConverter.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Convert the passed {@link XMLGregorianCalendar} to a
 * {@link GregorianCalendar}.
 *
 * @param aCal
 *        Source calendar. May be <code>null</code>.
 * @return <code>null</code> if the parameter is <code>null</code>.
 */
@Nullable
public static GregorianCalendar getGregorianCalendar (@Nullable final XMLGregorianCalendar aCal)
{
  if (aCal == null)
    return null;
  return aCal.toGregorianCalendar (aCal.getTimeZone (aCal.getTimezone ()),
                                   Locale.getDefault (Locale.Category.FORMAT),
                                   null);
}
 
Example 6
Source File: ISO8601.java    From document-management-system with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Parse string date in format "YYYY-MM-DDThh:mm:ss.SSSTZD"
 */
public static Calendar parseExtended(String value) {
	if (value == null) {
		return null;
	} else {
		try {
			DatatypeFactory dtf = DatatypeFactory.newInstance();
			XMLGregorianCalendar xml = dtf.newXMLGregorianCalendar(value);
			return xml.toGregorianCalendar();
		} catch (DatatypeConfigurationException e) {
			throw new IllegalArgumentException(value);
		}
	}
}
 
Example 7
Source File: DateUtils.java    From training with MIT License 5 votes vote down vote up
public static String extractHourMinute(final XMLGregorianCalendar date) {
	GregorianCalendar calendar = date.toGregorianCalendar();
	calendar.setTimeZone(TimeZone.getTimeZone("CET"));
	Date date2 = calendar.getTime();
	
	return format(date2, HOUR_MINUTE_FORMAT);
}
 
Example 8
Source File: AbstractParsingTask.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
private Date convertToDate(XMLGregorianCalendar gregorianCalendar) {
	if (gregorianCalendar != null) {
		GregorianCalendar toGregorianCalendar = gregorianCalendar.toGregorianCalendar();
		if (toGregorianCalendar != null) {
			return toGregorianCalendar.getTime();
		}
	}
	return null;
}
 
Example 9
Source File: CalendarConverter.java    From semweb4j with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Parses the supplied calendar value string and returns its value.
 * 
 * @param s
 *            A string representation of an xsd:dateTime, xsd:time,
 *            xsd:date, xsd:gYearMonth, xsd:gMonthDay, xsd:gYear, xsd:gMonth
 *            or xsd:gDay value.
 * @return The calendar value represented by the supplied string argument.
 * @throws RDFDataException
 *             If the supplied string is not a valid calendar value.
 */
public static Calendar parseXSDDateTime_toCalendar(String s)
		throws RDFDataException {
	log.debug("Trying to parse '" + s + "' as an xsd:dateTime");

	XMLGregorianCalendar xmlCalendar = dtFactory.newXMLGregorianCalendar(s);
	xmlCalendar = xmlCalendar.normalize();
	return xmlCalendar.toGregorianCalendar();

	// // TODO get rid of Jena dependency
	// XSDDateTime xsddate = (XSDDateTime) XSDDatatype.XSDdateTime.parse(s);
	//
	// if (xsddate == null)
	// throw new RDFDataException("Could not parse '" + s
	// + "' as an xsd:DateTime.");
	//
	// log.debug(xsddate.getYears() + "-" + xsddate.getMonths() + "-"
	// + xsddate.getDays() + "-" + xsddate.getHours() + "-"
	// + xsddate.getMinutes() + "-" + xsddate.getFullSeconds());
	//
	// Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UCT"));
	// c.set(Calendar.YEAR, xsddate.getYears());
	// c.set(Calendar.MONTH, xsddate.getMonths() - 1);
	// c.set(Calendar.DATE, xsddate.getDays());
	//
	// log.debug("Hour = " + xsddate.getHours());
	//
	// c.set(Calendar.HOUR_OF_DAY, xsddate.getHours());
	// c.set(Calendar.MINUTE, xsddate.getMinutes());
	// c.set(Calendar.SECOND, (int) xsddate.getSeconds());
	// // IMPROVE ... c.set(Calendar.MILLISECOND, ) currently we have only
	// // second-precision
	//
	// return c;
}
 
Example 10
Source File: TimeStringAsCalendar.java    From hyperjaxb3 with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public Calendar unmarshal(String time) throws Exception {
	if (time == null) {
		return null;
	} else {
		final XMLGregorianCalendar xmlGregorianCalendar = datatypeFactory
				.newXMLGregorianCalendar(time);
		final TimeZone timeZone;
		timeZone = xmlGregorianCalendar.getTimeZone(0);
		final Calendar calendar = xmlGregorianCalendar.toGregorianCalendar(
				timeZone, Locale.getDefault(), null);
		return calendar;
	}
}
 
Example 11
Source File: GregorianCalendarMarshall.java    From anno4j with Apache License 2.0 4 votes vote down vote up
public GregorianCalendar deserialize(Literal literal) {
	String label = literal.getLabel();
	XMLGregorianCalendar gc = factory.newXMLGregorianCalendar(label);
	return gc.toGregorianCalendar();
}
 
Example 12
Source File: PDTXMLConverterTest.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
@Test
public void testCalendarBackAndForth ()
{
  final ZoneId aZID = PDTConfig.getDefaultZoneId ();
  final Date aDate = PDTFactory.createDateForDate (2018, Month.OCTOBER, 31);
  final int nOffsetMinutes = PDTFactory.getTimezoneOffsetInMinutes (aZID, aDate.toInstant ());
  // Depends on the system timezone
  if (false)
    assertEquals ("Wed Oct 31 00:00:00 CET 2018", aDate.toString ());
  assertEquals (PDTConfig.getDefaultZoneId ()
                         .getRules ()
                         .getOffset (PDTFactory.createLocalDateTime (aDate))
                         .getTotalSeconds () /
                CGlobal.SECONDS_PER_MINUTE,
                PDTFactory.getTimezoneOffsetInMinutes (aDate));

  GregorianCalendar c0 = PDTXMLConverter.getCalendar (aDate);
  assertEquals (2018, c0.get (Calendar.YEAR));
  // 0-based!
  assertEquals (Calendar.OCTOBER, c0.get (Calendar.MONTH));
  assertEquals (31, c0.get (Calendar.DAY_OF_MONTH));
  assertEquals (0, c0.get (Calendar.HOUR));
  assertEquals (0, c0.get (Calendar.MINUTE));
  assertEquals (0, c0.get (Calendar.SECOND));
  assertEquals (0, c0.get (Calendar.MILLISECOND));
  assertEquals (nOffsetMinutes, PDTFactory.getTimezoneOffsetInMinutes (c0));

  final XMLGregorianCalendar c1 = PDTXMLConverter.getXMLCalendarDate (aDate);
  assertNotNull (c1);
  assertEquals (2018, c1.getYear ());
  // 1-based!
  assertEquals (10, c1.getMonth ());
  assertEquals (31, c1.getDay ());
  assertEquals (DatatypeConstants.FIELD_UNDEFINED, c1.getHour ());
  assertEquals (DatatypeConstants.FIELD_UNDEFINED, c1.getMinute ());
  assertEquals (DatatypeConstants.FIELD_UNDEFINED, c1.getSecond ());
  assertEquals (DatatypeConstants.FIELD_UNDEFINED, c1.getMillisecond ());
  // Depends on the system timezone
  if (false)
    assertEquals (60, c1.getTimezone ());

  c0 = c1.toGregorianCalendar (c1.getTimeZone (c1.getTimezone ()), Locale.getDefault (Locale.Category.FORMAT), null);
  assertEquals (2018, c0.get (Calendar.YEAR));
  assertEquals (Calendar.OCTOBER, c0.get (Calendar.MONTH));
  assertEquals (31, c0.get (Calendar.DAY_OF_MONTH));
  assertEquals (0, c0.get (Calendar.HOUR));
  assertEquals (0, c0.get (Calendar.MINUTE));
  assertEquals (0, c0.get (Calendar.SECOND));
  assertEquals (0, c0.get (Calendar.MILLISECOND));
  assertEquals (nOffsetMinutes, PDTFactory.getTimezoneOffsetInMinutes (c0));

  assertEquals (aDate.getTime (), c1.toGregorianCalendar ().getTime ().getTime ());

  final Date aDate2 = PDTXMLConverter.getDate (c1);
  assertNotNull (aDate2);
  assertEquals (aDate.getTime (), aDate2.getTime ());
  assertEquals (aDate, aDate2);
}
 
Example 13
Source File: XsValueImpl.java    From java-client-api with Apache License 2.0 4 votes vote down vote up
TimeValImpl(XMLGregorianCalendar value) {
    this((value == null) ? (Calendar) null : value.toGregorianCalendar());
}
 
Example 14
Source File: XsValueImpl.java    From java-client-api with Apache License 2.0 4 votes vote down vote up
DateTimeValImpl(XMLGregorianCalendar value) {
    this((value == null) ? (Calendar) null : value.toGregorianCalendar());
}
 
Example 15
Source File: XsValueImpl.java    From java-client-api with Apache License 2.0 4 votes vote down vote up
DateValImpl(XMLGregorianCalendar value) {
    this((value == null) ? (Calendar) null : value.toGregorianCalendar());
}
 
Example 16
Source File: WSAttribute.java    From document-management-software with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Sets the attribute value. It can be as String, Long, Double or Date.
 * 
 * @param value The attribute value.
 */
public void setValue(Object value) {
	if (getType() == WSAttribute.TYPE_USER && !(value instanceof WSUser)) {
		/*
		 * Needed to fix JAXB logic that will invoke getValue(that returns a
		 * Long) and setValue
		 */
		if (value instanceof Long)
			this.intValue = (Long) value;
		else if (value instanceof String)
			this.stringValue = (String) value;
		return;
	}

	if (value instanceof String) {
		this.type = TYPE_STRING;
		setStringValue((String) value);
	} else if (value instanceof Long) {
		this.type = TYPE_INT;
		setIntValue((Long) value);
	} else if (value instanceof Integer) {
		this.type = TYPE_INT;
		if (value != null)
			setIntValue(((Integer) value).longValue());
	} else if (value instanceof Boolean) {
		setIntValue(((Boolean) value).booleanValue() ? 1L : 0L);
		this.type = TYPE_BOOLEAN;
	} else if (value instanceof Double) {
		this.type = TYPE_DOUBLE;
		setDoubleValue((Double) value);
	} else if (value instanceof Date) {
		this.type = TYPE_DATE;
		setDateValue(WSUtil.convertDateToString((Date) value));
	} else if (value instanceof WSUser) {
		this.stringValue = ((WSUser) value).getFullName();
		this.intValue = ((WSUser) value).getId();
		this.type = TYPE_USER;
	} else if (value instanceof WSFolder) {
		this.stringValue = ((WSFolder) value).getName();
		this.intValue = ((WSFolder) value).getId();
		this.type = TYPE_FOLDER;
	} else {
		this.type = TYPE_DATE;
		if (value != null && value instanceof XMLGregorianCalendar) {
			XMLGregorianCalendar theXGCal = (XMLGregorianCalendar) value;
			GregorianCalendar theGCal = theXGCal.toGregorianCalendar();
			Date theDate = theGCal.getTime();
			setDateValue(WSUtil.convertDateToString((Date) theDate));
		} else if (value != null && value instanceof Date) {
			setDateValue(WSUtil.convertDateToString((Date) value));
		} else
			setDateValue(null);
	}
}
 
Example 17
Source File: XMLValues.java    From caravan with Apache License 2.0 4 votes vote down vote up
public static GregorianCalendar toGregorianCalendar(XMLGregorianCalendar calendar) {
    return calendar.toGregorianCalendar();
}
 
Example 18
Source File: XMLGregorianCalendarTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void checkToGregorianCalendar02() {

    XMLGregorianCalendar calendar = datatypeFactory.newXMLGregorianCalendar("2004-05-19T12:00:00+06:00");
    calendar.toGregorianCalendar(TimeZone.getDefault(), Locale.getDefault(), null);
}
 
Example 19
Source File: CoreXMLSerializers.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
protected Calendar _convert(XMLGregorianCalendar input) {
    return (input == null) ? null : input.toGregorianCalendar();
}
 
Example 20
Source File: XMLGregorianCalendarTest.java    From openjdk-jdk9 with GNU General Public License v2.0 3 votes vote down vote up
@Test
public void checkToGregorianCalendar01() {

    XMLGregorianCalendar time_16_17_18 = datatypeFactory.newXMLGregorianCalendar("16:17:18");
    XMLGregorianCalendar date_2001_02_03 = datatypeFactory.newXMLGregorianCalendar("2001-02-03");
    GregorianCalendar calendar = date_2001_02_03.toGregorianCalendar(null, null, time_16_17_18);

    int year = calendar.get(YEAR);
    int minute = calendar.get(MINUTE);

    assertTrue((year == 2001 && minute == 17), " expecting year == 2001, minute == 17" + ", result is year == " + year + ", minute == " + minute);


    calendar = time_16_17_18.toGregorianCalendar(null, null, date_2001_02_03);

    year = calendar.get(YEAR);
    minute = calendar.get(MINUTE);

    assertTrue((year == 2001 && minute == 17), " expecting year == 2001, minute == 17" + ", result is year == " + year + ", minute == " + minute);


    date_2001_02_03.setMinute(3);
    date_2001_02_03.setYear(null);

    XMLGregorianCalendar date_time = datatypeFactory.newXMLGregorianCalendar("2003-04-11T02:13:01Z");

    calendar = date_2001_02_03.toGregorianCalendar(null, null, date_time);

    year = calendar.get(YEAR);
    minute = calendar.get(MINUTE);
    int hour = calendar.get(HOUR);

    assertTrue((year == 2003 && hour == 2 && minute == 3), " expecting year == 2003, hour == 2, minute == 3" + ", result is year == " + year + ", hour == " + hour + ", minute == " + minute);


}