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

The following examples show how to use javax.xml.datatype.XMLGregorianCalendar#toString() . 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: CalendarDuration1097Test.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * main method.
 *
 * @param args Standard args.
 */
public static void main(String[] args) {
    try {
        String dateTimeString = "0001-01-01T00:00:00.0000000-05:00";
        DatatypeFactory dtf = DatatypeFactory.newInstance();
        XMLGregorianCalendar cal = dtf.newXMLGregorianCalendar( dateTimeString );
        System.out.println( "Expected: 0001-01-01T00:00:00.0000000-05:00");
        System.out.println( "Actual:" + cal.toString() );
        System.out.println( "toXMLFormat:" + cal.toXMLFormat() );
        String test = cal.toString();
        if (test.indexOf("E-7") > -1) {
            throw new RuntimeException("Expected: 0001-01-01T00:00:00.0000000-05:00");
        }
    } catch (DatatypeConfigurationException ex) {
        throw new RuntimeException(ex.getMessage());
    }
}
 
Example 2
Source File: ExampleItemEditor.java    From Analyze with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Converts a {@link Calendar} to a {@link CompoundPropertyValue} that
 * contains a time stamp.
 * 
 * @param calendar
 *            The {@link Calendar} that contains the time stamp information.
 * @return A {@link CompoundPropertyValue} that contains a time stamp.
 */
private CompoundPropertyValue createTimestamp(final Calendar calendar)
{
    final XMLGregorianCalendar timestamp = mGregorianCalendarProvider
            .createSpecifiedTimeGregorianCalendar(calendar
                    .getTimeInMillis());

    final String calendarAsXmlString = timestamp.toString();
    final List<String> elements = new ArrayList<String>();

    elements.add(calendarAsXmlString);
    elements.add(UTC);
    elements.add(Boolean.FALSE.toString());
    elements.add(calendarAsXmlString);

    return new CompoundPropertyValue(elements);
}
 
Example 3
Source File: SendUsernameServlet.java    From keycloak with Apache License 2.0 6 votes vote down vote up
private String getSessionInfo() {
    HttpSession session = httpServletRequest.getSession(false);

    if (session != null) {
        final SamlSession samlSession = (SamlSession) httpServletRequest.getSession(false).getAttribute(SamlSession.class.getName());

        if (samlSession != null) {
            String output = "Session ID: " + samlSession.getSessionIndex() + "\n";
            XMLGregorianCalendar sessionNotOnOrAfter = samlSession.getSessionNotOnOrAfter();
            output += "SessionNotOnOrAfter: " + (sessionNotOnOrAfter == null ? "null" : sessionNotOnOrAfter.toString());
            return output;
        }

        return "SamlSession doesn't exists";
    }

    return "Session doesn't exists";
}
 
Example 4
Source File: XMLGregorianCalendarTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testToString() {
    try {
        String inputDateTime = "2006-10-23T22:15:01.000000135+08:00";
        DatatypeFactory factory = DatatypeFactory.newInstance();
        XMLGregorianCalendar calendar = factory.newXMLGregorianCalendar(inputDateTime);
        String toStr = calendar.toString();
        Assert.assertTrue(toStr.indexOf("E") == -1, "String value cannot contain exponent");
    } catch (DatatypeConfigurationException dce) {
        dce.printStackTrace();
        Assert.fail("Failed to create instance of DatatypeFactory " + dce.getMessage());
    }
}
 
Example 5
Source File: XmlCteUtil.java    From Java_CTe with MIT License 5 votes vote down vote up
public static String dataCte(LocalDateTime dataASerFormatada, ZoneId zoneId) {
    try {
        GregorianCalendar calendar = GregorianCalendar.from(dataASerFormatada.atZone(ObjetoCTeUtil.verifica(zoneId).orElse(ZoneId.of("Brazil/East"))));

        XMLGregorianCalendar xmlCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);
        xmlCalendar.setMillisecond(DatatypeConstants.FIELD_UNDEFINED);
        return xmlCalendar.toString();

    } catch (DatatypeConfigurationException e) {
        LoggerUtil.log(XmlCteUtil.class, e.getMessage());
    }
    return null;
}
 
Example 6
Source File: ISO8601.java    From document-management-system with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Format date with format "YYYY-MM-DDThh:mm:ss.SSSTZD"
 */
public static String formatExtended(Calendar value) {
	if (value == null) {
		return null;
	} else {
		try {
			DatatypeFactory dtf = DatatypeFactory.newInstance();
			XMLGregorianCalendar xml = dtf.newXMLGregorianCalendar((GregorianCalendar) value);
			return xml.toString();
		} catch (DatatypeConfigurationException e) {
			throw new IllegalArgumentException();
		}
	}
}
 
Example 7
Source File: XmlNfeUtil.java    From Java_NFe with MIT License 5 votes vote down vote up
public static String dataNfe(LocalDateTime dataASerFormatada, ZoneId zoneId) {
    try {
        GregorianCalendar calendar = GregorianCalendar.from(dataASerFormatada.atZone(ObjetoUtil.verifica(zoneId).orElse(ZoneId.of("Brazil/East"))));

        XMLGregorianCalendar xmlCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);
        xmlCalendar.setMillisecond(DatatypeConstants.FIELD_UNDEFINED);
        return xmlCalendar.toString();

    } catch (DatatypeConfigurationException e) {
        LoggerUtil.log(XmlNfeUtil.class, e.getMessage());
    }
    return null;
}
 
Example 8
Source File: BigdataValueFactoryImpl.java    From database with GNU General Public License v2.0 5 votes vote down vote up
@Override
  public BigdataLiteralImpl createLiteral(final XMLGregorianCalendar arg0) {

/*
 * Note: QName#toString() does not produce the right representation,
 * which is why we need to go through XMLDatatypeUtil.
 * 
 * @see https://sourceforge.net/apps/trac/bigdata/ticket/117
 */
      return new BigdataLiteralImpl(this, arg0.toString(),
              null/* languageCode */, createURI(XMLDatatypeUtil.qnameToURI(
                      arg0.getXMLSchemaType()).stringValue()));
      
  }
 
Example 9
Source File: XMLGregorianCalendarTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(dataProvider = "calendar")
public void checkToStringPos(final int year, final int month, final int day, final int hour, final int minute, final int second) {
    XMLGregorianCalendar calendar = datatypeFactory.newXMLGregorianCalendar(year, month, day, hour, minute, second, undef, undef);
    calendar.toString();
}
 
Example 10
Source File: XMLGregorianCalendarTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = IllegalStateException.class)
public void checkToStringNeg() {
    XMLGregorianCalendar calendar = datatypeFactory.newXMLGregorianCalendar(undef, undef, undef, undef, undef, undef, undef, undef);
    // expected to fail
    calendar.toString();
}
 
Example 11
Source File: DateTimeExtension.java    From database with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Use the long value of the {@link XSDLongIV} delegate (which represents
 * milliseconds since the epoch) to create a an XMLGregorianCalendar
 * object (GMT timezone).  Use the XMLGregorianCalendar to create a datatype
 * literal value with the appropriate datatype.
 */
public V asValue(final LiteralExtensionIV iv, final BigdataValueFactory vf) {
    
    if (!datatypes.containsKey(iv.getExtensionIV())) {
        throw new IllegalArgumentException("unrecognized datatype");
    }
    
    /*
     * Milliseconds since the epoch.
     */
    final long l = iv.getDelegate().longValue();
    
    final TimeZone tz = BSBMHACK ? TimeZone.getDefault()/*getTimeZone("GMT")*/ : defaultTZ;
    final GregorianCalendar c = new GregorianCalendar(tz);
    c.setGregorianChange(new Date(Long.MIN_VALUE));
    c.setTimeInMillis(l);
    
    try {
        
        final BigdataURI dt = datatypes.get(iv.getExtensionIV());
        
        final DatatypeFactory f = datatypeFactorySingleton;

        final XMLGregorianCalendar xmlGC = f.newXMLGregorianCalendar(c);

        String s = xmlGC.toString();
        
        final int offset = s.startsWith("-") ? 1 : 0;
        if (dt.equals(XSD.DATETIME)) {
            if (BSBMHACK) {
                // Chopping off the milliseconds part and the trailing 'Z'.
                final int i = s.lastIndexOf('.');
                if (i >= 0) {
                    s = s.substring(0, i);
                }
            }
        } else if (dt.equals(XSD.DATE)) {
            // YYYY-MM-DD (10 chars)
            s = s.substring(0, 10+offset);
        } else if (dt.equals(XSD.TIME)) {
            // everything after the date (from 11 chars in)
            s = s.substring(11+offset);
        } else if (dt.equals(XSD.GDAY)) {
            // gDay Defines a part of a date - the day (---DD)
            s = "---" + s.substring(8+offset, 10+offset);
        } else if (dt.equals(XSD.GMONTH)) {
            // gMonth Defines a part of a date - the month (--MM)
            s = "--" + s.substring(5+offset, 7+offset);
        } else if (dt.equals(XSD.GMONTHDAY)) {
            // gMonthDay Defines a part of a date - the month and day (--MM-DD)
            s = "--" + s.substring(5+offset, 10+offset);
        } else if (dt.equals(XSD.GYEAR)) {
            // gYear Defines a part of a date - the year (YYYY)
            s = s.substring(0, 4+offset);
        } else if (dt.equals(XSD.GYEARMONTH)) {
            // gYearMonth    Defines a part of a date - the year and month (YYYY-MM)
            s = s.substring(0, 7+offset);
        } 
        
        return (V) vf.createLiteral(s, dt);

    } catch (RuntimeException ex) {

        if (InnerCause.isInnerCause(ex, InterruptedException.class)) {

            throw ex;

        }
        
        throw new IllegalArgumentException("bad iv: " + iv, ex);
        
    }

}
 
Example 12
Source File: SAMLAssertionWriterTest.java    From keycloak with Apache License 2.0 4 votes vote down vote up
@Test
public void testAuthnStatementSessionNotOnOrAfterExists() throws ProcessingException {
    long sessionLengthInSeconds = 3600;

    XMLGregorianCalendar issueInstant = XMLTimeUtil.getIssueInstant();
    XMLGregorianCalendar sessionExpirationDate = XMLTimeUtil.add(issueInstant, sessionLengthInSeconds);

    AuthnStatementType authnStatementType = new AuthnStatementType(issueInstant);

    authnStatementType.setSessionIndex("9b3cf799-225b-424a-8e5e-ee3c38e06ded::24b2f572-163c-43ad-8011-de6cd3803f76");
    authnStatementType.setSessionNotOnOrAfter(sessionExpirationDate);

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    SAMLAssertionWriter samlAssertionWriter = new SAMLAssertionWriter(StaxUtil.getXMLStreamWriter(byteArrayOutputStream));

    samlAssertionWriter.write(authnStatementType, true);

    String serializedAssertion = new String(byteArrayOutputStream.toByteArray(), GeneralConstants.SAML_CHARSET);
    String expectedXMLAttribute = "SessionNotOnOrAfter=\"" + sessionExpirationDate.toString() + "\"";

    Assert.assertTrue(serializedAssertion.contains(expectedXMLAttribute));
}