List of usage examples for javax.xml.bind DatatypeConverter parseDateTime
public static java.util.Calendar parseDateTime(String lexicalXSDDateTime)
Converts the string argument into a Calendar value.
From source file:iing.uabc.edu.mx.persistencia.util.JSON.java
private static Object parseObject(JsonParser parser, BeanManager manager) { Event evt;// www . j a v a 2s . c o m String keyName = ""; do { evt = parser.next(); switch (evt) { case START_OBJECT: Class objectClass = manager.getType(keyName); System.out.println("Nuevo Objeto: " + objectClass.getSimpleName()); //Create new BeanManager for the new object with its class BeanManager objectManager = new BeanManager(objectClass); Object result = parseObject(parser, objectManager); manager.setProperty(keyName, result); break; case START_ARRAY: //Get element class of the collection Class elementClass = manager.getCollectionElementType(keyName); System.out.println("Nueva Colleccion de clase: " + elementClass.getSimpleName()); //Get collection instance from the bean Collection collection = (Collection) manager.getProperty(keyName); //Create new collection manager with the given class CollectionManager collectionManager = new CollectionManager(collection, elementClass); collection = parseArray(parser, collectionManager); manager.setProperty(keyName, collection); break; case KEY_NAME: //Get attribute name keyName = parser.getString(); System.out.println("KeyName: " + keyName); break; case VALUE_FALSE: manager.setProperty(keyName, false); System.out.println("Valor " + keyName + ": " + false); break; case VALUE_NULL: manager.setProperty(keyName, null); System.out.println("Valor " + keyName + ": null"); break; case VALUE_NUMBER: //double or int if (parser.isIntegralNumber()) { int value = parser.getInt(); manager.setProperty(keyName, value); System.out.println("Valor " + keyName + ": " + value); } else { double value = parser.getBigDecimal().doubleValue(); manager.setProperty(keyName, value); System.out.println("Valor " + keyName + ": " + value); } break; case VALUE_STRING: String value = parser.getString(); //Validate if the field is of type Date if (manager.getType(keyName) == Date.class) { Calendar cal = DatatypeConverter.parseDateTime(value); Date date = cal.getTime(); manager.setProperty(keyName, date); System.out.println("Valor " + keyName + ": " + date); } else { manager.setProperty(keyName, value); System.out.println("Valor " + keyName + ": " + value); } break; case VALUE_TRUE: manager.setProperty(keyName, true); System.out.println("Valor " + keyName + ": " + true); break; } } while (evt != Event.END_OBJECT); return manager.getBean(); }
From source file:com.telefonica.iot.cygnus.utils.CommonUtils.java
/** * Gets the milliseconds version of the given timestamp. * @param timestamp// w ww .j a v a 2 s. co m * @return The milliseconds version of the given timestamp * @throws java.text.ParseException */ public static long getMilliseconds(String timestamp) throws java.text.ParseException { return DatatypeConverter.parseDateTime(timestamp).getTime().getTime(); }
From source file:be.e_contract.mycarenet.sts.EHealthSTSClient.java
/** * Returns the value of the NotOnOrAfter element within the given SAML * assertion.//from w w w. j a v a 2s .c o m * * @param assertionElement * @return */ public DateTime getNotAfter(Element assertionElement) { NodeList conditionsNodeList = assertionElement .getElementsByTagNameNS("urn:oasis:names:tc:SAML:1.0:assertion", "Conditions"); Element conditionsElement = (Element) conditionsNodeList.item(0); String notOnOrAfterAttributeValue = conditionsElement.getAttribute("NotOnOrAfter"); Calendar calendar = DatatypeConverter.parseDateTime(notOnOrAfterAttributeValue); return new DateTime(calendar.getTime()); }
From source file:de.slub.fedora.oai.OaiHarvesterTestIT.java
@Test public void waitWhenLastrunIsInFuture() throws Exception { Calendar cal = Calendar.getInstance(); cal.add(Calendar.SECOND, 3);//from w w w . j av a 2s. c o m esNode.client().prepareIndex("_river", "fedora", "_last") .setSource(jsonBuilder().startObject().field("timestamp", cal).endObject()).execute().actionGet(); embeddedHttpHandler.resourcePath = OAI_LIST_RECORDS_XML; runAndWait(oaiHarvester); GetResponse response = esNode.client().get(new GetRequest("_river", "fedora", "_last")).actionGet(); TimeValue expectedRunTime = TimeValue.timeValueMillis(cal.getTimeInMillis()); TimeValue actualRunTime = TimeValue.timeValueMillis(DatatypeConverter .parseDateTime(String.valueOf(response.getSourceAsMap().get("timestamp"))).getTimeInMillis()); assertFalse("Actual run should not happen before last run", expectedRunTime.getMillis() > actualRunTime.getMillis()); }
From source file:com.amazonaws.hal.client.ConversionUtil.java
private static Object convertFromString(Class<?> clazz, String value) { if (String.class.isAssignableFrom(clazz)) { return value; } else if (int.class.isAssignableFrom(clazz) || Integer.class.isAssignableFrom(clazz)) { return new Integer(value); } else if (long.class.isAssignableFrom(clazz) || Long.class.isAssignableFrom(clazz)) { return new Long(value); } else if (short.class.isAssignableFrom(clazz) || Short.class.isAssignableFrom(clazz)) { return new Short(value); } else if (double.class.isAssignableFrom(clazz) || Double.class.isAssignableFrom(clazz)) { return new Double(value); } else if (float.class.isAssignableFrom(clazz) || Float.class.isAssignableFrom(clazz)) { return new Float(value); } else if (boolean.class.isAssignableFrom(clazz) || Boolean.class.isAssignableFrom(clazz)) { return Boolean.valueOf(value); } else if (char.class.isAssignableFrom(clazz) || Character.class.isAssignableFrom(clazz)) { return value.charAt(0); } else if (byte.class.isAssignableFrom(clazz) || Byte.class.isAssignableFrom(clazz)) { return new Byte(value); } else if (BigDecimal.class.isAssignableFrom(clazz)) { return new BigDecimal(value); } else if (BigInteger.class.isAssignableFrom(clazz)) { return new BigInteger(value); } else if (Date.class.isAssignableFrom(clazz)) { try {//from w w w. j a v a 2s . c o m return new Date(Long.parseLong(value)); } catch (NumberFormatException e) { try { return DatatypeConverter.parseDateTime(value).getTime(); } catch (IllegalArgumentException e1) { throw new RuntimeException("Unexpected date format: " + value + ". We currently parse xsd:datetime and milliseconds."); } } } else if (clazz.isEnum()) { try { //noinspection unchecked return Enum.valueOf((Class<Enum>) clazz, value); } catch (IllegalArgumentException e) { log.error(String.format( "'%s' is not a recognized enum value for %s. Returning default of %s instead.", value, clazz.getName(), clazz.getEnumConstants()[0])); return clazz.getEnumConstants()[0]; } } else { throw new RuntimeException("Not sure how to convert " + value + " to a " + clazz.getSimpleName()); } }
From source file:org.eclipse.lyo.testsuite.oslcv2.CoreResourceXmlTests.java
@Test public void CoreResourceHasAtMostOneCreatedDate() throws XPathExpressionException { String eval = "//" + getNode() + "/" + "dc:created"; NodeList createdDates = (NodeList) OSLCUtils.getXPath().evaluate(eval, doc, XPathConstants.NODESET); assertTrue(getFailureMessage(), createdDates.getLength() <= 1); //If there is a created date, verify the format. if (createdDates.getLength() > 0) { try {/*from w w w . j av a 2 s . c o m*/ DatatypeConverter.parseDateTime(createdDates.item(0).getTextContent()); } catch (Exception e) { fail("Created date not in valid XSD format"); } } }
From source file:org.eclipse.lyo.testsuite.oslcv2.cm.ChangeRequestXmlTests.java
@Test public void changeRequestHasAtMostOneCreatedDate() throws XPathExpressionException { NodeList createdDates = (NodeList) OSLCUtils.getXPath().evaluate("//oslc_cm_v2:ChangeRequest/dc:created", doc, XPathConstants.NODESET); assertTrue(getFailureMessage(), createdDates.getLength() <= 1); //If there is a created date, verify the format. if (createdDates.getLength() > 0) { try {// www . jav a2 s.co m DatatypeConverter.parseDateTime(createdDates.item(0).getTextContent()); } catch (Exception e) { fail("Created date not in valid XSD format"); } } }
From source file:de.slub.fedora.oai.OaiHarvester.java
private void handleXmlResult(InputStream content) throws ParserConfigurationException, IOException, SAXException, XPathExpressionException { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); Document document = documentBuilderFactory.newDocumentBuilder().parse(content); XPath xPath = XPathFactory.newInstance().newXPath(); XPathExpression xSelectIdentifier = xPath.compile("//header/identifier"); NodeList nodes = (NodeList) xSelectIdentifier.evaluate(document, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) { Node n = nodes.item(i);/*from w w w . j a v a 2 s . c o m*/ jobQueue.add(new ObjectIndexJob(IndexJob.Type.CREATE, getLocalIdentifier(n.getTextContent()))); } XPathExpression xSelectResumptionToken = xPath.compile("//resumptionToken"); resumptionToken = (String) xSelectResumptionToken.evaluate(document, XPathConstants.STRING); XPathExpression xSelectExpirationDate = xPath.compile("//resumptionToken/@expirationDate"); String s = (String) xSelectExpirationDate.evaluate(document, XPathConstants.STRING); if (s == null || s.isEmpty()) { expirationDate = null; } else { expirationDate = DatatypeConverter.parseDateTime(s).getTime(); } }
From source file:org.metaservice.frontend.rest.SparqlEndpointResource.java
public @NotNull Response generateResponseResourceOrExport(@NotNull String mimeType, @Nullable String path, @Nullable String date, boolean download) { if (path == null) { return Response.status(Response.Status.BAD_REQUEST).build(); }/* w ww . j a v a2 s . c om*/ Calendar calendar = null; try { if (date != null) { calendar = DatatypeConverter.parseDateTime(date); } } catch (IllegalArgumentException e) { return Response.status(Response.Status.BAD_REQUEST).build(); } if (download) { path = path.replaceFirst("(\\.quad)?\\.(jsonld|rdf|ttl)$", ""); } if (path.contains("http://www.metaservice.org")) { path = path.replaceAll("http://www.metaservice.org", "http://metaservice.org"); } Response.ResponseBuilder builder = Response.ok(resourceService.getResource(path, calendar, mimeType)); if (download) { builder.header("Content-Disposition", "attachment"); } return builder.build(); }