LocalDate with Property place holder Spring

时间:2017-07-27 12:17:45

标签: spring-mvc spring-boot integration-testing property-placeholder

我正在使用Spring Boot和属性占位符。我有一个值为date.A=24/07/17的属性文件。 我有一个班级,我正在使用@Value注释:

@Value("${date.A}")
private LocalDate dateA;

但是我在运行gradle build integrationTest时遇到了运行时错误:

Caused by: java.time.format.DateTimeParseException: Text '24/07/17' could not be parsed: Invalid value for MonthOfYear (valid values 1 - 12): 24
    at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1920)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1855)
    at java.time.LocalDate.parse(LocalDate.java:400)

3 个答案:

答案 0 :(得分:0)

我需要仔细查看yml文件以获得最佳答案。但这是我的预感 -

预期格式为MM/dd/YY.

您可以尝试将yml文件更改为此类

..

date
  A=07/24/17
..

答案 1 :(得分:0)

我认为您需要为此编写转换器,如下所示:

public LocalDate convertToDateObject(String value) throws ConversionException {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
    try {
        return LocalDate.parse(value, formatter);

    } catch (DateTimeParseException ex) {
        return null;
    }
}

答案 2 :(得分:0)

日期应在en_US语言环境中指定,因此您需要交换月和日:

date.A=7/24/17