-1

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

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) 

回答

0

我需要在YML文件细看给:

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

但运行gradle build integrationTest当我收到运行时错误这里最好的answer..But是我hunch-

预期的格式是MM/dd/YY.

可否请您尝试改变YML文件,像这样

.. 

date 
    A=07/24/17 
.. 
+0

改变它之后是工作,但根据业务我想DD/MM/YY –

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; 
    } 
} 
+0

如何将这一转换器@Value财产占位? –

0

日期应在en_US区域设置中指定,所以你需要交换月份和日期:

date.A=7/24/17