如何仅使用 @DateTimeFormat 将日期 @ConfigurationProperties 与时间绑定?

QD_*_*LDN 0 java spring spring-boot dateformatter

spring-boot 的新手。

我正在尝试使用注释@ConfigurationProperties 解析文件中的属性。我能够解析日期字段以外的字段。

问题是我的属性文件只有时间而没有日期。即日期=09:30:00。

我可以用 @DateTimeFormat(pattern = "HH:mm:ss") 解析它。但问题是,它给出的日期为 date=Thu Jan 01 09:30:00 GST 1970。

我想获取今天的日期,时间为 09:30:00。是否可以 ?

@ConfigurationProperties
public class Config {

    private int id;
    
    private int version;
        
    @DateTimeFormat(pattern = "HH:mm:ss")
    private Date date;
    
}
Run Code Online (Sandbox Code Playgroud)

财产

id=12
version=2
date=09:30:00
Run Code Online (Sandbox Code Playgroud)

cri*_*zis 6

为什么不使用仅代表时间的类型?

    @DateTimeFormat(pattern = "HH:mm:ss")
    private LocalTime time;

    public LocalDateTime getDate() {
        return LocalDateTime.of(LocalDate.now(), time);
    } 
Run Code Online (Sandbox Code Playgroud)