检查字符串并处理无效日期

时间:2019-01-09 20:48:09

标签: java datetime

当我遇到无效的日期(即2018-02-31, 2018-11-31)时,我希望我的代码将其转换为该月的最后一天。

我不确定如何检查传递的字符串中的值。

到目前为止,这是我的代码:

/**
     * If date comes back as invalid, i.e. 2018-11-31
     * convert it to have last day of given month.
     *  
     * @param nextFieldTypeDate
     * @return
     */
    public static LocalDate resolveInvalidDate(String nextFieldTypeDate) {
        LocalDate convertedDate = null;
        try {
            convertedDate = LocalDate.parse(nextFieldTypeDate);
        } catch(DateTimeException dte) {
            //convertedDate = convert date to have last day of the month
            DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM");
            String yearMonthString = nextFieldTypeDate.subSequence(0, 7).toString();
            YearMonth ym = YearMonth.parse(yearMonthString, fmt);
            convertedDate = ym.atEndOfMonth();
        } catch(Exception e) {
            logger.error(e.getMessage());
            throw new ConversionException("Unable to convert nextFieldTypeDate.", e);
        }
        return convertedDate;
    }

2 个答案:

答案 0 :(得分:4)

parseUnresolved的{​​{1}}方法进行解析,而不会试图理解所解析的值。因此,它接受无效的日期,并允许您检查解析的值,然后再尝试使用其中的DatetimeFormatter

LocalDate

让我们尝试使用此版本的方法:

public static LocalDate resolveInvalidDate(String nextFieldTypeDate) {
    ParsePosition position = new ParsePosition(0);
    TemporalAccessor parsed = DateTimeFormatter.ISO_LOCAL_DATE
            .parseUnresolved(nextFieldTypeDate, position);
    if (position.getIndex() < nextFieldTypeDate.length()) {
        throw new IllegalArgumentException("Could not parse entire string");
    }
    YearMonth ym = YearMonth.from(parsed);
    int lastDayOfMonth = ym.lengthOfMonth();
    int parsedDayOfMOnth = parsed.get(ChronoField.DAY_OF_MONTH);
    if (parsedDayOfMOnth > lastDayOfMonth) { // invalid, must be adjusted to lasst day of month
        return ym.atEndOfMonth();
    } else {
        return ym.atDay(parsedDayOfMOnth);
    }
}

输出为:

    System.out.println(resolveInvalidDate("2018-02-31"));
    System.out.println(resolveInvalidDate("2018-02-27"));

因此2月31日无效,并已调整为2月28日,即2018年该月的最后一天。 2月27日有效,并按原样返回。

编辑:出于相关目的,可能考虑了更简单的2018-02-28 2018-02-27 。但是,这会将DateTimeFormatter.ISO_LOCAL_DATE.withResolverStyle(ResolverStyle.LENIENT).parse(nextFieldTypeDate, LocalDate::from)变成了2018-02-31,据我了解,这不是您想要的。

答案 1 :(得分:1)

不是最干净的解决方案,但这应该可以工作:

public static LocalDate resolveInvalidDate(String nextFieldTypeDate) {
        LocalDate convertedDate = null;
        try {
            convertedDate = LocalDate.parse(nextFieldTypeDate);
        } catch(DateTimeException dte) {
            //convertedDate = convert date to have last day of the month
            Integer[] constituents = Arrays.stream(nextFieldTypeDate.split("-"))
                                           .map(constituentString -> Integer.valueOf(constituentString))
                                           .toArray(Integer::new);
            LocalDate defaultDate = LocalDate.of(constituents[0], constituents[1], 1);
            LocalDate convertedDate = LocalDate.of(defaultDate.getYear(), defaultDate.getMonth(), defaultDate.lengthOfMonth());
        } catch(Exception e) {
            logger.error(e.getMessage());
            throw new ConversionException("Unable to convert nextFieldTypeDate.", e);
        }
        return convertedDate;
    }