Example usage for java.time DateTimeException DateTimeException

List of usage examples for java.time DateTimeException DateTimeException

Introduction

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

Prototype

public DateTimeException(String message) 

Source Link

Document

Constructs a new date-time exception with the specified message.

Usage

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

/**
 * Guess the pattern from the given value.
 *
 * @param value the value to get the date time from.
 * @param column the column metadata//from   ww  w .ja  v a  2s .  c o  m
 * @return the wanted parsed date time. For date only value, time is set to 00:00:00.
 */
DatePattern guessPattern(String value, ColumnMetadata column) {
    if (StringUtils.isEmpty(value)) {
        throw new DateTimeException("No pattern can be found out of '" + value + "'");
    }
    // call DQ on the given value
    try (Analyzer<Analyzers.Result> analyzer = analyzerService.build(column,
            AnalyzerService.Analysis.PATTERNS)) {
        analyzer.analyze(value);
        analyzer.end();

        // only one value --> only one result
        final Analyzers.Result result = analyzer.getResult().get(0);
        if (result.exist(PatternFrequencyStatistics.class)) {
            final PatternFrequencyStatistics patternFrequencyStatistics = result
                    .get(PatternFrequencyStatistics.class);
            final Map<String, Long> topTerms = patternFrequencyStatistics.getTopK(1);
            List<PatternFrequency> patterns = new ArrayList<>(1);
            topTerms.forEach((s, o) -> patterns.add(new PatternFrequency(s, o)));

            // get & check the results
            final List<DatePattern> results = getPatterns(patterns);
            if (results.isEmpty()) {
                throw new DateTimeException("DQ did not find any pattern for '" + value + "'");
            }

            // as Christopher L. said : "there can be only one" :-)
            return getPatterns(patterns).get(0);
        } else {
            throw new DateTimeException("DQ did not find any pattern for '" + value + "'");
        }
    } catch (Exception e) {
        throw new DateTimeException("Unable to close analyzer after analyzing value '" + value + "'", e);
    }
}

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  w ww  . j a  v  a 2s  . 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");
}