-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