Example usage for java.time.format DateTimeFormatter ISO_DATE_TIME

List of usage examples for java.time.format DateTimeFormatter ISO_DATE_TIME

Introduction

In this page you can find the example usage for java.time.format DateTimeFormatter ISO_DATE_TIME.

Prototype

DateTimeFormatter ISO_DATE_TIME

To view the source code for java.time.format DateTimeFormatter ISO_DATE_TIME.

Click Source Link

Document

The ISO-like date-time formatter that formats or parses a date-time with the offset and zone if available, such as '2011-12-03T10:15:30', '2011-12-03T10:15:30+01:00' or '2011-12-03T10:15:30+01:00[Europe/Paris]'.

Usage

From source file:fr.pilato.elasticsearch.crawler.fs.tika.TikaDocParser.java

public static void generate(FsSettings fsSettings, InputStream inputStream, String filename, Doc doc,
        MessageDigest messageDigest, long filesize) throws IOException {
    logger.trace("Generating document [{}]", filename);
    // Extracting content with Tika
    // See #38: https://github.com/dadoonet/fscrawler/issues/38
    int indexedChars = 100000;
    if (fsSettings.getFs().getIndexedChars() != null) {
        if (fsSettings.getFs().getIndexedChars().percentage()) {
            indexedChars = (int) Math.round(filesize * fsSettings.getFs().getIndexedChars().asDouble());
            logger.trace("using percentage [{}] to define indexed chars: [{}]",
                    fsSettings.getFs().getIndexedChars(), indexedChars);
        } else {//from   ww w  .j a v a  2 s.  c o m
            indexedChars = (int) fsSettings.getFs().getIndexedChars().value();
            logger.trace("indexed chars [{}]",
                    indexedChars == -1 ? "has been disabled. All text will be extracted" : indexedChars);
        }
    }
    Metadata metadata = new Metadata();

    String parsedContent = null;

    if (messageDigest != null) {
        logger.trace("Generating hash with [{}]", messageDigest.getAlgorithm());
        inputStream = new DigestInputStream(inputStream, messageDigest);
    }

    ByteArrayOutputStream bos = null;
    if (fsSettings.getFs().isStoreSource()) {
        logger.debug("Using a TeeInputStream as we need to store the source");
        bos = new ByteArrayOutputStream();
        inputStream = new TeeInputStream(inputStream, bos);
    }

    try {
        // Set the maximum length of strings returned by the parseToString method, -1 sets no limit
        logger.trace("Beginning Tika extraction");
        parsedContent = tika().parseToString(inputStream, metadata, indexedChars);
        logger.trace("End of Tika extraction");
    } catch (Throwable e) {
        logger.debug("Failed to extract [" + indexedChars + "] characters of text for [" + filename + "]", e);
    }

    // Adding what we found to the document we want to index

    // File
    doc.getFile().setContentType(metadata.get(Metadata.CONTENT_TYPE));
    doc.getFile().setExtension(FilenameUtils.getExtension(filename));

    // We only add `indexed_chars` if we have other value than default or -1
    if (fsSettings.getFs().getIndexedChars() != null && fsSettings.getFs().getIndexedChars().value() != -1) {
        doc.getFile().setIndexedChars(indexedChars);
    }

    if (fsSettings.getFs().isAddFilesize()) {
        if (metadata.get(Metadata.CONTENT_LENGTH) != null) {
            // We try to get CONTENT_LENGTH from Tika first
            doc.getFile().setFilesize(Long.parseLong(metadata.get(Metadata.CONTENT_LENGTH)));
        }
    }
    if (messageDigest != null) {
        byte[] digest = messageDigest.digest();
        String result = "";
        // Convert to Hexa
        for (int i = 0; i < digest.length; i++) {
            result += Integer.toString((digest[i] & 0xff) + 0x100, 16).substring(1);
        }
        doc.getFile().setChecksum(result);
    }
    // File

    // Meta
    doc.getMeta().setAuthor(metadata.get(TikaCoreProperties.CREATOR));
    doc.getMeta().setTitle(metadata.get(TikaCoreProperties.TITLE));
    String sDate = metadata.get(TikaCoreProperties.MODIFIED);
    if (sDate != null) {
        try {
            LocalDateTime date = LocalDateTime.parse(sDate, DateTimeFormatter.ISO_DATE_TIME);
            doc.getMeta().setDate(date);
        } catch (DateTimeParseException e) {
            logger.warn("Can not parse date [{}] for [{}]. Skipping date field...", sDate, filename);
        }
    }
    doc.getMeta().setKeywords(commaDelimitedListToStringArray(metadata.get(TikaCoreProperties.KEYWORDS)));

    if (fsSettings.getFs().isRawMetadata()) {
        logger.trace("Listing all available metadata:");
        for (String metadataName : metadata.names()) {
            String value = metadata.get(metadataName);
            // This is a logger trick which helps to generate our unit tests
            // You need to change test/resources/log4j2.xml fr.pilato.elasticsearch.crawler.fs.tika level to trace
            logger.trace("  assertThat(raw, hasEntry(\"{}\", \"{}\"));", metadataName, value);
            doc.getMeta().addRaw(metadataName, value);
        }
    }
    // Meta

    // Doc content
    doc.setContent(parsedContent);

    // Doc as binary attachment
    if (fsSettings.getFs().isStoreSource()) {
        doc.setAttachment(Base64.getEncoder().encodeToString(bos.toByteArray()));
    }
    logger.trace("End document generation");
    // End of our document
}

From source file:org.apache.metron.parsers.syslog.Syslog5424Parser.java

@SuppressWarnings("unchecked")
private void setTimestamp(JSONObject message) {
    String timeStampString = (String) message.get(SyslogFieldKeys.HEADER_TIMESTAMP.getField());
    if (!StringUtils.isBlank(timeStampString) && !timeStampString.equals("-")) {
        message.put("timestamp", timeStampString);
    } else {// w  w w .j a va 2 s  .  com
        message.put("timestamp", LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME));
    }
}

From source file:eu.crydee.alignment.aligner.ae.MetricsSummaryAE.java

@Override
public void collectionProcessComplete() throws AnalysisEngineProcessException {
    try {/*from  w ww  .j  a  v  a2  s  . c  om*/
        String template = IOUtils.toString(getClass()
                .getResourceAsStream("/eu/crydee/alignment/aligner/ae/" + "metrics-summarizer-template.html"));
        String titledTemplate = template.replace("@@TITLE@@",
                "Metrics summarizer" + LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME));
        StringBuilder sb = new StringBuilder();
        sb.append("<table class=\"table table-striped ").append("table-condensed\">\n")
                .append("            <thead>\n").append("                <tr>\n")
                .append("                    <th>City\\Metric</th>\n");
        for (String key : keys) {
            sb.append("                    <th>").append(methodsMetadata.get(key).getRight()).append("</th>\n");
        }
        sb.append("                <tr>\n").append("            </thead>\n").append("            <tbody>\n");
        for (String ele : results.rowKeySet()) {
            sb.append("                <tr>\n").append("                    <td>").append(ele)
                    .append("</td>\n");
            Map<String, Samples> metricResults = results.row(ele);
            for (String key : keys) {
                Samples samples = metricResults.get(key);
                SummaryStatistics ss = new SummaryStatistics();
                samples.samples.forEach(d -> ss.addValue(d));
                double mean = ss.getMean();
                boolean significant = TestUtils.tTest(samples.mu,
                        ArrayUtils.toPrimitive(samples.samples.toArray(new Double[0])), 0.05),
                        above = samples.mu > mean;
                String summary = String.format("%.3f", samples.mu) + " <small class=\"text-muted\">"
                        + String.format("%.3f", ss.getMean()) + ""
                        + String.format("%.3f", ss.getStandardDeviation()) + "</small>";
                logger.info(ele + "\t" + key + "\t" + summary + "\t" + significant);
                sb.append("                    <td class=\"")
                        .append(significant ? (above ? "success" : "danger") : "warning").append("\">")
                        .append(summary).append("</td>\n");
            }
            sb.append("                </tr>\n");
        }
        sb.append("            </tbody>\n").append("        </table>");
        FileUtils.write(new File(htmlFilepath), titledTemplate.replace("@@TABLE@@", sb.toString()),
                StandardCharsets.UTF_8);
    } catch (IOException ex) {
        logger.error("IO problem with the HTML output.");
        throw new AnalysisEngineProcessException(ex);
    }
}

From source file:org.jboss.as.test.integration.web.session.SessionManagementTestCase.java

@Test
public void testSessionManagementOperations() throws Exception {
    try (CloseableHttpClient client = HttpClients.createDefault()) {

        ModelNode operation = new ModelNode();
        operation.get(ModelDescriptionConstants.OP).set(LIST_SESSIONS);
        operation.get(ModelDescriptionConstants.OP_ADDR).set(PathAddress
                .parseCLIStyleAddress("/deployment=management.war/subsystem=undertow").toModelNode());
        ModelNode opRes = managementClient.getControllerClient().execute(operation);
        Assert.assertEquals(opRes.toString(), "success",
                opRes.get(ModelDescriptionConstants.OUTCOME).asString());
        Assert.assertEquals(Collections.emptyList(), opRes.get(ModelDescriptionConstants.RESULT).asList());
        long c1 = System.currentTimeMillis();
        HttpGet get = new HttpGet("http://" + TestSuiteEnvironment.getServerAddress()
                + ":8080/management/SessionPersistenceServlet");
        HttpResponse res = client.execute(get);
        long c2 = System.currentTimeMillis();
        String sessionId = null;/* w  ww  .  j  a  va2s .  c o  m*/
        for (Header cookie : res.getHeaders("Set-Cookie")) {
            if (cookie.getValue().startsWith("JSESSIONID=")) {
                sessionId = cookie.getValue().split("=")[1].split("\\.")[0];
                break;
            }
        }
        Assert.assertNotNull(sessionId);
        opRes = managementClient.getControllerClient().execute(operation);
        Assert.assertEquals(opRes.toString(), "success",
                opRes.get(ModelDescriptionConstants.OUTCOME).asString());
        Assert.assertEquals(opRes.toString(), Collections.singletonList(new ModelNode(sessionId)),
                opRes.get(ModelDescriptionConstants.RESULT).asList());

        operation.get(SESSION_ID).set(sessionId);

        opRes = executeOperation(operation, GET_SESSION_CREATION_TIME_MILLIS);
        long time1 = opRes.get(ModelDescriptionConstants.RESULT).asLong();
        Assert.assertTrue(c1 <= time1);
        Assert.assertTrue(time1 <= c2);

        opRes = executeOperation(operation, GET_SESSION_CREATION_TIME);
        long sessionCreationTime = LocalDateTime
                .parse(opRes.get(ModelDescriptionConstants.RESULT).asString(), DateTimeFormatter.ISO_DATE_TIME)
                .toInstant(ZoneId.systemDefault().getRules().getOffset(Instant.now())).toEpochMilli();
        Assert.assertEquals(time1, sessionCreationTime);

        opRes = executeOperation(operation, GET_SESSION_LAST_ACCESSED_TIME_MILLIS);
        Assert.assertEquals(time1, opRes.get(ModelDescriptionConstants.RESULT).asLong());

        opRes = executeOperation(operation, GET_SESSION_LAST_ACCESSED_TIME);
        long aTime2 = LocalDateTime
                .parse(opRes.get(ModelDescriptionConstants.RESULT).asString(), DateTimeFormatter.ISO_DATE_TIME)
                .toInstant(ZoneId.systemDefault().getRules().getOffset(Instant.now())).toEpochMilli();
        Assert.assertEquals(time1, aTime2);
        Assert.assertEquals(sessionCreationTime, aTime2);

        opRes = executeOperation(operation, LIST_SESSION_ATTRIBUTE_NAMES);
        List<ModelNode> resultList = opRes.get(ModelDescriptionConstants.RESULT).asList();
        Assert.assertEquals(1, resultList.size());
        Assert.assertEquals(opRes.toString(), "val", resultList.get(0).asString());

        opRes = executeOperation(operation, LIST_SESSION_ATTRIBUTES);
        List<Property> properties = opRes.get(ModelDescriptionConstants.RESULT).asPropertyList();
        Assert.assertEquals(opRes.toString(), 1, properties.size());
        Property property = properties.get(0);
        Assert.assertEquals(opRes.toString(), "val", property.getName());
        Assert.assertEquals(opRes.toString(), "0", property.getValue().asString());

        //we want to make sure that the values will be different
        //so we wait 10ms
        Thread.sleep(10);
        long a1 = System.currentTimeMillis();
        client.execute(get);
        long a2 = System.currentTimeMillis();

        do {
            //because the last access time is updated after the request returns there is a possible race here
            //to get around this we execute this op in a loop and wait for the value to change
            //in 99% of cases this will only iterate once
            //because of the 10ms sleep above they should ways be different
            //we have a max wait time of 1s if something goes wrong
            opRes = executeOperation(operation, GET_SESSION_LAST_ACCESSED_TIME_MILLIS);
            time1 = opRes.get(ModelDescriptionConstants.RESULT).asLong();
            if (time1 != sessionCreationTime) {
                break;
            }
        } while (System.currentTimeMillis() < a1 + 1000);
        Assert.assertTrue(a1 <= time1);
        Assert.assertTrue(time1 <= a2);

        opRes = executeOperation(operation, GET_SESSION_LAST_ACCESSED_TIME);
        long time2 = LocalDateTime
                .parse(opRes.get(ModelDescriptionConstants.RESULT).asString(), DateTimeFormatter.ISO_DATE_TIME)
                .toInstant(ZoneId.systemDefault().getRules().getOffset(Instant.now())).toEpochMilli();
        Assert.assertEquals(time1, time2);

        operation.get(ATTRIBUTE).set("val");
        opRes = executeOperation(operation, GET_SESSION_ATTRIBUTE);
        Assert.assertEquals("1", opRes.get(ModelDescriptionConstants.RESULT).asString());

        executeOperation(operation, INVALIDATE_SESSION);

        opRes = executeOperation(operation, LIST_SESSIONS);
        Assert.assertEquals(Collections.emptyList(), opRes.get(ModelDescriptionConstants.RESULT).asList());
    }
}

From source file:eu.crydee.alignment.aligner.ae.MetricsOneVsOneC.java

@Override
public void collectionProcessComplete() throws AnalysisEngineProcessException {
    try {/*  www  .j  a  v  a2s .  com*/
        String template = IOUtils.toString(getClass()
                .getResourceAsStream("/eu/crydee/alignment/aligner/ae/" + "metrics-one-vs-one-template.html"));
        template = template.replace("@@TITLE@@",
                "Metrics comparator" + LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME));
        template = template.replace("@@LEFTALGO@@", leftAlgoName);
        template = template.replace("@@RIGHTALGO@@", rightAlgoName);
        StringBuilder sb = new StringBuilder();
        sb.append("<table class=\"table table-condensed\">\n").append("            <thead>\n")
                .append("                <tr>\n").append("                    <th>Document\\Metric</th>\n");
        for (String key : keys) {
            sb.append("                    <th colspan=\"2\">").append(methodsMetadata.get(key).getRight())
                    .append("</th>\n");
        }
        sb.append("                <tr>\n").append("            </thead>\n").append("            <tbody>\n")
                .append("                <tr>\n").append("                    <td>\n")
                .append("                        <strong>Total</strong>\n")
                .append("                    </td>\n");
        for (String key : keys) {
            SummaryStatistics ss1 = new SummaryStatistics(), ss2 = new SummaryStatistics();
            List<Pair<Double, Double>> column = results.column(key).values().stream().peek(p -> {
                ss1.addValue(p.getLeft());
                ss2.addValue(p.getRight());
            }).collect(Collectors.toList());
            boolean significant = TestUtils.pairedTTest(column.stream().mapToDouble(p -> p.getLeft()).toArray(),
                    column.stream().mapToDouble(p -> p.getRight()).toArray(), 0.05);
            double mean1 = ss1.getMean(), mean2 = ss2.getMean();
            boolean above = mean1 > mean2;
            String summary1 = String.format("%.3f", mean1) + "<small class=\"text-muted\">" + ""
                    + String.format("%.3f", ss1.getStandardDeviation()) + "</small>",
                    summary2 = String.format("%.3f", mean2) + "<small class=\"text-muted\">" + ""
                            + String.format("%.3f", ss2.getStandardDeviation()) + "</small>";
            sb.append("                    <td class=\"")
                    .append(significant ? (above ? "success" : "danger") : "warning").append("\">")
                    .append(summary1).append("</td>\n");
            sb.append("                    <td class=\"")
                    .append(significant ? (!above ? "success" : "danger") : "warning").append("\">")
                    .append(summary2).append("</td>\n");
        }
        sb.append("                </tr>\n");
        SortedSet<String> rows = new TreeSet<>(results.rowKeySet());
        for (String row : rows) {
            sb.append("                <tr>\n").append("                    <td>").append(row)
                    .append("</td>\n");
            for (String key : keys) {
                Pair<Double, Double> r = results.get(row, key);
                sb.append("                    <td>").append(String.format("%.3f", r.getLeft()))
                        .append("</td>\n").append("                    <td>")
                        .append(String.format("%.3f", r.getRight())).append("</td>\n");

            }
            sb.append("                </tr>\n");
        }
        sb.append("            </tbody>\n").append("        </table>");
        FileUtils.write(new File(htmlFilepath), template.replace("@@TABLE@@", sb.toString()),
                StandardCharsets.UTF_8);
    } catch (IOException ex) {
        logger.error("IO problem with the HTML output.");
        throw new AnalysisEngineProcessException(ex);
    }
}

From source file:io.pivotal.demo.smartgrid.frontend.timeseries.AggregateCounterTimeSeriesRepository.java

private TimeSeriesCollection convertToTimeSeriesCollection(AggregateCounterCollection acc) {

    TimeSeriesCollection tsc = new TimeSeriesCollection(acc.getName());

    for (Map.Entry<String, AggregateCounter> entry : acc.getAggregateCounters().entrySet()) {

        String timeSeriesName = entry.getKey();
        AggregateCounter aggregateCounter = entry.getValue();

        List<String> timeAxis = new ArrayList<>();
        List<String> valueAxis = new ArrayList<>();

        for (Map.Entry<String, String> dataPoint : aggregateCounter.getCounts().entrySet()) {

            String pit = dataPoint.getKey();
            String value = dataPoint.getValue();

            LocalDateTime ldt = LocalDateTime.parse(pit, DateTimeFormatter.ISO_DATE_TIME);
            timeAxis.add("" + ldt.toEpochSecond(ZoneOffset.UTC));
            valueAxis.add(value);/*from   w  w  w. j  a va2 s. c  o  m*/
        }

        tsc.registerTimeSeries(timeSeriesName, timeAxis, valueAxis);
    }

    return tsc;
}

From source file:com.nirmata.workflow.details.JsonSerializer.java

public static JsonNode newRunnableTask(RunnableTask runnableTask) {
    ArrayNode taskDags = newArrayNode();
    runnableTask.getTaskDags().forEach(taskDag -> taskDags.add(newRunnableTaskDag(taskDag)));

    ObjectNode tasks = newNode();//from ww w .  jav  a 2 s .  c  o m
    runnableTask.getTasks().entrySet()
            .forEach(entry -> tasks.set(entry.getKey().getId(), newExecutableTask(entry.getValue())));

    ObjectNode node = newNode();
    node.set("taskDags", taskDags);
    node.set("tasks", tasks);
    node.put("startTimeUtc", runnableTask.getStartTimeUtc().format(DateTimeFormatter.ISO_DATE_TIME));
    node.put("completionTimeUtc",
            runnableTask.getCompletionTimeUtc().isPresent()
                    ? runnableTask.getCompletionTimeUtc().get().format(DateTimeFormatter.ISO_DATE_TIME)
                    : null);
    node.put("parentRunId",
            runnableTask.getParentRunId().isPresent() ? runnableTask.getParentRunId().get().getId() : null);
    return node;
}

From source file:de.speexx.jira.jan.command.transition.IssueTransitionFetcher.java

void exportAsCsv(final List<IssueInfo> issues, final AtomicBoolean doHeader) {
    try {/*from  ww  w.  j a  v a 2 s. c o m*/
        final CSVPrinter csvPrinter = new CSVPrinter(new OutputStreamWriter(System.out, StandardCharsets.UTF_8),
                RFC4180);

        final String[] header = new String[] { "issue-key", "type", "issue-creation-datetime", "priority",
                "resolution", "from-stage", "stage", "stage-enter-datetime", "stage-duration" };

        if (!doHeader.get()) {
            csvPrinter.printRecord((Object[]) header);
            doHeader.set(true);
        }

        issues.forEach(info -> {
            info.stageInfoAsDuration().forEach(stageDuration -> {

                final String[] values = new String[header.length];
                values[0] = info.key;
                values[1] = info.issueType;
                values[2] = DateTimeFormatter.ISO_DATE_TIME.format(info.created);
                values[3] = info.priority;
                values[4] = resolutionAdjustment(info);

                values[5] = stageDuration.fromStageName != null ? "" + stageDuration.fromStageName : "";
                values[6] = "" + stageDuration.stageName;
                values[7] = DateTimeFormatter.ISO_DATE_TIME.format(stageDuration.stageStart);
                values[8] = "" + stageDuration.getDurationSeconds();

                try {
                    csvPrinter.printRecord((Object[]) values);
                } catch (final IOException e) {
                    throw new JiraAnalyzeException(e);
                }
            });
        });
        csvPrinter.flush();

    } catch (final IOException e) {
        throw new JiraAnalyzeException(e);
    }
}

From source file:fr.pilato.elasticsearch.crawler.fs.framework.FsCrawlerUtil.java

public static Date localDateTimeToDate(String sDate) {
    if (sDate == null) {
        return null;
    }/*from   w  ww.  ja  v a 2s .co  m*/
    return localDateTimeToDate(LocalDateTime.parse(sDate, DateTimeFormatter.ISO_DATE_TIME));
}

From source file:com.esri.geoportal.commons.gpt.client.Client.java

private Date readLastUpdated(QueryResponse.Source source) {
    if (source != null && source.src_lastupdate_dt != null) {
        try {/*  w  w w  .  j  a  v  a 2  s .co  m*/
            return Date.from(ZonedDateTime.from(DateTimeFormatter.ISO_DATE_TIME.parse(source.src_lastupdate_dt))
                    .toInstant());
        } catch (Exception ex) {
        }
    }
    return null;
}