Example usage for java.time LocalDateTime parse

List of usage examples for java.time LocalDateTime parse

Introduction

In this page you can find the example usage for java.time LocalDateTime parse.

Prototype

public static LocalDateTime parse(CharSequence text, DateTimeFormatter formatter) 

Source Link

Document

Obtains an instance of LocalDateTime from a text string using a specific formatter.

Usage

From source file:org.codelibs.fess.service.FavoriteLogService.java

private void buildSearchCondition(final FavoriteLogPager favoriteLogPager, final FavoriteLogCB cb) {

    if (StringUtil.isNotBlank(favoriteLogPager.userCode)) {
        cb.setupSelect_UserInfo();//from  w w w  . ja  va  2 s .c o  m
        cb.query().queryUserInfo().setCode_Equal(favoriteLogPager.userCode);
    }

    if (StringUtil.isNotBlank(favoriteLogPager.startDate)) {
        final StringBuilder buf = new StringBuilder(20);
        buf.append(favoriteLogPager.startDate);
        buf.append('+');
        if (StringUtil.isNotBlank(favoriteLogPager.startHour)) {
            buf.append(favoriteLogPager.startHour);
        } else {
            buf.append("00");
        }
        buf.append(':');
        if (StringUtil.isNotBlank(favoriteLogPager.startMin)) {
            buf.append(favoriteLogPager.startMin);
        } else {
            buf.append("00");
        }

        final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd+HH:mm");
        try {
            cb.query().setCreatedTime_GreaterEqual(LocalDateTime.parse(buf.toString(), formatter));
        } catch (final DateTimeParseException e) {
            favoriteLogPager.startDate = null;
            favoriteLogPager.startHour = null;
            favoriteLogPager.startMin = null;
        }
    }

    if (StringUtil.isNotBlank(favoriteLogPager.endDate)) {
        final StringBuilder buf = new StringBuilder(20);
        buf.append(favoriteLogPager.endDate);
        buf.append('+');
        if (StringUtil.isNotBlank(favoriteLogPager.endHour)) {
            buf.append(favoriteLogPager.endHour);
        } else {
            buf.append("00");
        }
        buf.append(':');
        if (StringUtil.isNotBlank(favoriteLogPager.endMin)) {
            buf.append(favoriteLogPager.endMin);
        } else {
            buf.append("00");
        }

        final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd+HH:mm");
        try {
            cb.query().setCreatedTime_LessThan(LocalDateTime.parse(buf.toString(), formatter));
        } catch (final DateTimeParseException e) {
            favoriteLogPager.endDate = null;
            favoriteLogPager.endHour = null;
            favoriteLogPager.endMin = null;
        }
    }

}

From source file:org.talend.dataprep.transformation.actions.date.DateParser.java

/**
 * Parse the date from the given patterns.
 *
 * @param value the text to parse./*from   ww  w.  j av  a2s.c o  m*/
 * @param patterns the patterns to use.
 * @return the parsed date-time
 */
public LocalDateTime parseDateFromPatterns(String value, List<DatePattern> patterns) {

    // take care of the null value
    if (value == null) {
        throw new DateTimeException("cannot parse null");
    }

    for (DatePattern pattern : patterns) {
        final DateTimeFormatter formatter = new DateTimeFormatterBuilder().parseCaseInsensitive()
                .append(pattern.getFormatter()).toFormatter(Locale.ENGLISH);

        // first try to parse directly as LocalDateTime
        try {
            return LocalDateTime.parse(value, formatter);
        } catch (DateTimeException e) {
            LOGGER.trace("Unable to parse date '{}' using LocalDateTime.", value, e);
            // if it fails, let's try the LocalDate first
            try {
                LocalDate temp = LocalDate.parse(value, formatter);
                return temp.atStartOfDay();
            } catch (DateTimeException e2) {
                LOGGER.trace("Unable to parse date '{}' using LocalDate.", value, e2);
                // nothing to do here, just try the next formatter
            }
        }
    }
    throw new DateTimeException("'" + value + "' does not match any known pattern");
}

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 v  a2s.c  om*/
        }

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

    return tsc;
}

From source file:io.manasobi.utils.DateUtils.java

public static LocalDateTime convertToDateTime(String dateTime, String pattern) {

    if (StringUtils.isEmpty(dateTime)) {
        return null;
    }// w w  w.j  av a 2 s . c om

    if (dateTime.length() == 8) {
        return DateUtils.convertToDate(dateTime, "yyyyMMdd").atTime(0, 0, 0);
    }

    if (dateTime.length() == 10) {
        return DateUtils.convertToDate(dateTime).atTime(0, 0, 0);
    }

    java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter.ofPattern(pattern);
    return LocalDateTime.parse(dateTime, formatter);
}

From source file:org.codelibs.fess.service.SearchLogService.java

public void deleteAll(final SearchLogPager searchLogPager) {
    clickLogBhv.varyingQueryDelete(cb2 -> {
        if (StringUtil.isNotBlank(searchLogPager.searchWord)) {
            cb2.query().querySearchLog().setSearchWord_LikeSearch(searchLogPager.searchWord,
                    op -> op.likeContain());
        }//from ww  w. j  a va  2  s . c  o  m

        if (StringUtil.isNotBlank(searchLogPager.startDate)) {
            final StringBuilder buf = new StringBuilder(20);
            buf.append(searchLogPager.startDate);
            buf.append('+');
            if (StringUtil.isNotBlank(searchLogPager.startHour)) {
                buf.append(searchLogPager.startHour);
            } else {
                buf.append("00");
            }
            buf.append(':');
            if (StringUtil.isNotBlank(searchLogPager.startMin)) {
                buf.append(searchLogPager.startMin);
            } else {
                buf.append("00");
            }

            final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd+HH:mm");
            try {
                final LocalDateTime startDate = LocalDateTime.parse(buf.toString(), formatter);
                cb2.query().querySearchLog().setRequestedTime_GreaterEqual(startDate);
            } catch (final DateTimeParseException e) {
                searchLogPager.startDate = null;
                searchLogPager.startHour = null;
                searchLogPager.startMin = null;
            }
        }

        if (StringUtil.isNotBlank(searchLogPager.endDate)) {
            final StringBuilder buf = new StringBuilder(20);
            buf.append(searchLogPager.endDate);
            buf.append('+');
            if (StringUtil.isNotBlank(searchLogPager.endHour)) {
                buf.append(searchLogPager.endHour);
            } else {
                buf.append("00");
            }
            buf.append(':');
            if (StringUtil.isNotBlank(searchLogPager.endMin)) {
                buf.append(searchLogPager.endMin);
            } else {
                buf.append("00");
            }

            final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd+HH:mm");
            try {
                final LocalDateTime endDate = LocalDateTime.parse(buf.toString(), formatter);
                cb2.query().querySearchLog().setRequestedTime_LessThan(endDate);
            } catch (final DateTimeParseException e) {
                searchLogPager.endDate = null;
                searchLogPager.endHour = null;
                searchLogPager.endMin = null;
            }
        }

        if (StringUtil.isNotBlank(searchLogPager.startPage)) {
            cb2.query().querySearchLog().setQueryOffset_Equal(0);
        }
    }, op -> op.allowNonQueryDelete());
    searchFieldLogBhv.varyingQueryDelete(cb2 -> {
    }, op -> op.allowNonQueryDelete());
    searchLogBhv.varyingQueryDelete(cb1 -> {
        if (StringUtil.isNotBlank(searchLogPager.searchWord)) {
            cb1.query().setSearchWord_LikeSearch(searchLogPager.searchWord, op -> op.likeContain());
        }

        if (StringUtil.isNotBlank(searchLogPager.userCode)) {
            cb1.setupSelect_UserInfo();
            cb1.query().queryUserInfo().setCode_Equal(searchLogPager.userCode);
        }

        if (StringUtil.isNotBlank(searchLogPager.startDate)) {
            final StringBuilder buf = new StringBuilder(20);
            buf.append(searchLogPager.startDate);
            buf.append('+');
            if (StringUtil.isNotBlank(searchLogPager.startHour)) {
                buf.append(searchLogPager.startHour);
            } else {
                buf.append("00");
            }
            buf.append(':');
            if (StringUtil.isNotBlank(searchLogPager.startMin)) {
                buf.append(searchLogPager.startMin);
            } else {
                buf.append("00");
            }

            final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd+HH:mm");
            try {
                final LocalDateTime startDate = LocalDateTime.parse(buf.toString(), formatter);
                cb1.query().setRequestedTime_GreaterEqual(startDate);
            } catch (final DateTimeParseException e) {
                searchLogPager.startDate = null;
                searchLogPager.startHour = null;
                searchLogPager.startMin = null;
            }
        }

        if (StringUtil.isNotBlank(searchLogPager.endDate)) {
            final StringBuilder buf = new StringBuilder(20);
            buf.append(searchLogPager.endDate);
            buf.append('+');
            if (StringUtil.isNotBlank(searchLogPager.endHour)) {
                buf.append(searchLogPager.endHour);
            } else {
                buf.append("00");
            }
            buf.append(':');
            if (StringUtil.isNotBlank(searchLogPager.endMin)) {
                buf.append(searchLogPager.endMin);
            } else {
                buf.append("00");
            }

            final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd+HH:mm");
            try {
                final LocalDateTime endDate = LocalDateTime.parse(buf.toString(), formatter);
                cb1.query().setRequestedTime_LessThan(endDate);
            } catch (final DateTimeParseException e) {
                searchLogPager.endDate = null;
                searchLogPager.endHour = null;
                searchLogPager.endMin = null;
            }
        }

        if (StringUtil.isNotBlank(searchLogPager.startPage)) {
            cb1.query().setQueryOffset_Equal(0);
        }
    }, op -> op.allowNonQueryDelete());
}

From source file:org.ops4j.pax.web.resources.jsf.OsgiResource.java

/**
 * <p>/*from w w  w  .  ja v  a2s. c  o m*/
 * RFC 2616 allows three different date-formats:
 * <ul>
 * <li>RFC 1123</li>
 * <li>RFC 1036</li>
 * <li>ASCI-Time</li>
 * </ul>
 * </p>
 *
 * @param headerValue value transmitted from client
 * @return the parsed DateTime
 * @see <a href=
 * "http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3">RFC
 * 2616</a>
 */
private LocalDateTime convertIfModifiedSinceToDate(String headerValue) {

    LocalDateTime time = null;
    try {
        time = LocalDateTime.parse(headerValue, DateTimeFormatter.RFC_1123_DATE_TIME);
    } catch (DateTimeParseException e) {
        logger.trace("could not parse date with RFC-1123. Will try RFC-1036 format...");
    }

    if (time == null) {
        try {
            time = LocalDateTime.parse(headerValue, DateTimeFormatter.ofPattern(PATTERN_RFC_1036));
        } catch (DateTimeParseException e) {
            logger.trace("could not parse date with RFC-1036. Will try ASCITIME format...");
        }
    }

    if (time == null) {
        try {
            time = LocalDateTime.parse(headerValue, DateTimeFormatter.ofPattern(PATTERN_ASCITIME));
        } catch (DateTimeParseException e) {
            logger.trace("could not parse date with ASCITIME.");
        }
    }

    if (time == null) {
        logger.error("Could not parse given if-modified-since header with value '{}'", headerValue);
    }
    return time;
}

From source file:org.codelibs.fess.service.CrawlingSessionService.java

public void importCsv(final Reader reader) {
    final CsvReader csvReader = new CsvReader(reader, new CsvConfig());
    final DateTimeFormatter formatter = DateTimeFormatter
            .ofPattern(CoreLibConstants.DATE_FORMAT_ISO_8601_EXTEND);
    try {//from w w w . j  a va 2 s  .  c  o m
        List<String> list;
        csvReader.readValues(); // ignore header
        while ((list = csvReader.readValues()) != null) {
            try {
                final String sessionId = list.get(0);
                CrawlingSession crawlingSession = crawlingSessionBhv.selectEntity(cb -> {
                    cb.query().setSessionId_Equal(sessionId);
                    cb.specify().columnSessionId();
                }).orElse(null);//TODO
                if (crawlingSession == null) {
                    crawlingSession = new CrawlingSession();
                    crawlingSession.setSessionId(list.get(0));
                    crawlingSession.setCreatedTime(LocalDateTime.parse(list.get(1), formatter));
                    crawlingSessionBhv.insert(crawlingSession);
                }

                final CrawlingSessionInfo entity = new CrawlingSessionInfo();
                entity.setCrawlingSessionId(crawlingSession.getId());
                entity.setKey(list.get(2));
                entity.setValue(list.get(3));
                entity.setCreatedTime(LocalDateTime.parse(list.get(4), formatter));
                crawlingSessionInfoBhv.insert(entity);
            } catch (final Exception e) {
                log.warn("Failed to read a click log: " + list, e);
            }
        }
    } catch (final IOException e) {
        log.warn("Failed to read a click log.", e);
    }
}

From source file:org.talend.dataprep.transformation.actions.date.ComputeTimeSince.java

private static LocalDateTime parseSinceDateIfConstant(Map<String, String> parameters) {
    String mode = parameters.containsKey(SINCE_WHEN_PARAMETER) ? parameters.get(SINCE_WHEN_PARAMETER)
            : NOW_SERVER_SIDE_MODE;/*from  www . jav a  2  s .  c  o  m*/

    LocalDateTime since;
    switch (mode) {
    case SPECIFIC_DATE_MODE:
        try {
            since = LocalDateTime.parse(parameters.get(SPECIFIC_DATE_PARAMETER), DEFAULT_FORMATTER);
        } catch (DateTimeException e) {
            LOGGER.info("Error parsing input date. The front-end might have supplied a corrupted value.", e);
            since = null;
        }
        break;
    case OTHER_COLUMN_MODE:
        since = null; // It will be computed in apply
        break;
    case NOW_SERVER_SIDE_MODE:
    default:
        since = LocalDateTime.now();
        break;
    }
    return since;
}

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

public static Date localDateTimeToDate(String sDate) {
    if (sDate == null) {
        return null;
    }//from  www. j  a va2s.c o m
    return localDateTimeToDate(LocalDateTime.parse(sDate, DateTimeFormatter.ISO_DATE_TIME));
}

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

public static RunnableTask getRunnableTask(JsonNode node) {
    List<RunnableTaskDag> taskDags = Lists.newArrayList();
    node.get("taskDags").forEach(n -> taskDags.add(getRunnableTaskDag(n)));

    Map<TaskId, ExecutableTask> tasks = Maps.newHashMap();
    Iterator<Map.Entry<String, JsonNode>> fields = node.get("tasks").fields();
    while (fields.hasNext()) {
        Map.Entry<String, JsonNode> next = fields.next();
        tasks.put(new TaskId(next.getKey()), getExecutableTask(next.getValue()));
    }//from   www  . ja  va 2s . c o m

    LocalDateTime startTime = LocalDateTime.parse(node.get("startTimeUtc").asText(),
            DateTimeFormatter.ISO_DATE_TIME);
    LocalDateTime completionTime = node.get("completionTimeUtc").isNull() ? null
            : LocalDateTime.parse(node.get("completionTimeUtc").asText(), DateTimeFormatter.ISO_DATE_TIME);
    RunId parentRunId = node.get("parentRunId").isNull() ? null : new RunId(node.get("parentRunId").asText());
    return new RunnableTask(tasks, taskDags, startTime, completionTime, parentRunId);
}