Example usage for javax.xml.datatype XMLGregorianCalendar setFractionalSecond

List of usage examples for javax.xml.datatype XMLGregorianCalendar setFractionalSecond

Introduction

In this page you can find the example usage for javax.xml.datatype XMLGregorianCalendar setFractionalSecond.

Prototype

public abstract void setFractionalSecond(BigDecimal fractional);

Source Link

Document

Set fractional seconds.

Usage

From source file:eu.europa.ec.markt.dss.DSSUtils.java

/**
 * Converts a given <code>Date</code> to a new <code>XMLGregorianCalendar</code>.
 *
 * @param date the date to be converted// w ww . ja v a 2s . co  m
 * @return the new <code>XMLGregorianCalendar</code> or null
 */
public static XMLGregorianCalendar createXMGregorianCalendar(Date date) {

    if (date == null) {
        return null;
    }

    final GregorianCalendar calendar = new GregorianCalendar();
    calendar.setTime(date);

    try {
        XMLGregorianCalendar gc = DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);
        gc.setFractionalSecond(null);
        gc = gc.normalize(); // to UTC = Zulu
        return gc;
    } catch (DatatypeConfigurationException e) {

        // LOG.log(Level.WARNING, "Unable to properly convert a Date to an XMLGregorianCalendar",e);
    }

    return null;
}

From source file:eu.europa.esig.dss.DSSXMLUtils.java

/**
 * Converts a given {@code Date} to a new {@code XMLGregorianCalendar}.
 *
 * @param date the date to be converted/*  w  w w.j  a va  2s  .c o  m*/
 * @return the new {@code XMLGregorianCalendar} or null
 */
public static XMLGregorianCalendar createXMLGregorianCalendar(final Date date) {

    if (date == null) {
        return null;
    }
    final GregorianCalendar calendar = new GregorianCalendar();
    calendar.setTime(date);
    try {

        XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance()
                .newXMLGregorianCalendar(calendar);
        xmlGregorianCalendar.setFractionalSecond(null);
        xmlGregorianCalendar = xmlGregorianCalendar.normalize(); // to UTC = Zulu
        return xmlGregorianCalendar;
    } catch (DatatypeConfigurationException e) {
        LOG.warn("Unable to properly convert a Date to an XMLGregorianCalendar " + e.getMessage(), e);
    }
    return null;
}

From source file:org.energyos.espi.datacustodian.web.api.ExportServiceTests.java

private String getXMLTime(int millis) throws DatatypeConfigurationException {
    DatatypeFactory datatypeFactory = DatatypeFactory.newInstance();
    GregorianCalendar cal = getGregorianCalendar(millis);
    XMLGregorianCalendar xmlGregorianCalendar = datatypeFactory.newXMLGregorianCalendar(cal);
    xmlGregorianCalendar.setFractionalSecond(null);
    return xmlGregorianCalendar.toXMLFormat();
}

From source file:org.apache.metron.dataloads.taxii.TaxiiHandler.java

/**
 * The action to be performed by this timer task.
 *///  w  w  w. j a  v  a2s .com
@Override
public void run() {
    if (inProgress) {
        return;
    }
    Date ts = new Date();
    LOG.info("Polling..." + new SimpleDateFormat().format(ts));
    try {
        inProgress = true;
        // Prepare the message to send.
        String sessionID = MessageHelper.generateMessageId();
        PollRequest request = messageFactory.get().createPollRequest().withMessageId(sessionID)
                .withCollectionName(collection);
        if (subscriptionId != null) {
            request = request.withSubscriptionID(subscriptionId);
        } else {
            request = request.withPollParameters(messageFactory.get().createPollParametersType());
        }
        if (beginTime != null) {
            Calendar gc = GregorianCalendar.getInstance();
            gc.setTime(beginTime);
            XMLGregorianCalendar gTime = null;
            try {
                gTime = DatatypeFactory.newInstance().newXMLGregorianCalendar((GregorianCalendar) gc)
                        .normalize();
            } catch (DatatypeConfigurationException e) {
                LOG.error("Unable to set the begin time", e);
            }
            gTime.setFractionalSecond(null);
            LOG.info("Begin Time: " + gTime);
            request.setExclusiveBeginTimestamp(gTime);
        }

        try {
            PollResponse response = call(request, PollResponse.class);
            LOG.info("Got Poll Response with " + response.getContentBlocks().size() + " blocks");
            int numProcessed = 0;
            long avgTimeMS = 0;
            long timeStartedBlock = System.currentTimeMillis();
            for (ContentBlock block : response.getContentBlocks()) {
                AnyMixedContentType content = block.getContent();
                for (Object o : content.getContent()) {
                    numProcessed++;
                    long timeS = System.currentTimeMillis();
                    String xml = null;
                    if (o instanceof Element) {
                        Element element = (Element) o;
                        xml = getStringFromDocument(element.getOwnerDocument());
                        if (LOG.isDebugEnabled() && Math.random() < 0.01) {
                            LOG.debug("Random Stix doc: " + xml);
                        }
                        for (LookupKV<ThreatIntelKey, ThreatIntelValue> kv : extractor.extract(xml)) {
                            String indicatorType = kv.getValue().getMetadata().get("indicator-type");
                            TableInfo tableInfo = tableMap.get(indicatorType);
                            boolean persisted = false;
                            if (tableInfo != null) {
                                kv.getValue().getMetadata().put("source_type", "taxii");
                                kv.getValue().getMetadata().put("taxii_url", endpoint.toString());
                                kv.getValue().getMetadata().put("taxii_collection", collection);
                                Put p = converter.toPut(tableInfo.getColumnFamily(), kv.getKey(),
                                        kv.getValue());
                                HTableInterface table = getTable(tableInfo);
                                table.put(p);
                                persisted = true;
                            }
                            LOG.info("Found Threat Intel: " + persisted + ", " + kv.getKey() + " => "
                                    + kv.getValue());
                        }
                    }
                    avgTimeMS += System.currentTimeMillis() - timeS;
                }
                if ((numProcessed + 1) % 100 == 0) {
                    LOG.info("Processed " + numProcessed + " in "
                            + (System.currentTimeMillis() - timeStartedBlock) + " ms, avg time: "
                            + avgTimeMS / content.getContent().size());
                    timeStartedBlock = System.currentTimeMillis();
                    avgTimeMS = 0;
                    numProcessed = 0;
                }
            }
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            throw new RuntimeException("Unable to make request", e);
        }
    } finally {
        inProgress = false;
        beginTime = ts;
    }
}

From source file:org.apache.taverna.prov.W3ProvenanceExport.java

protected Literal timestampToLiteral(Timestamp timestamp) {
    if (timestamp == null) {
        return null;
    }//from ww w  . j a v a 2s . c  o m
    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(timestamp);
    XMLGregorianCalendar xmlCal = datatypeFactory.newXMLGregorianCalendar(cal);
    // Chop of the trailing 0-s of non-precission
    xmlCal.setFractionalSecond(BigDecimal.valueOf(timestamp.getNanos() / 1000000, NANOSCALE - 6));
    return provModel.model.createTypedLiteral(xmlCal.toXMLFormat(), XSDDatatype.XSDdateTime);
}