Search code examples
javaspringspring-bootdateformatter

How to bind date @ConfigurationProperties with time only using @DateTimeFormat?


New to spring-boot.

I'm trying to parse the properties from the file with annotation @ConfigurationProperties. I'm able to parse the fields other than date field.

issue is My property file has only time without date. i.e. date=09:30:00.

I'm able to parse it with @DateTimeFormat(pattern = "HH:mm:ss"). But the issue is, it is giving date as date=Thu Jan 01 09:30:00 GST 1970.

I would like to get the date as todays date with time 09:30:00. Is it possible ?

@ConfigurationProperties
public class Config {

    private int id;
    
    private int version;
        
    @DateTimeFormat(pattern = "HH:mm:ss")
    private Date date;
    
}

Property

id=12
version=2
date=09:30:00

Solution

  • Why not use a type that represents time only?

        @DateTimeFormat(pattern = "HH:mm:ss")
        private LocalTime time;
    
        public LocalDateTime getDate() {
            return LocalDateTime.of(LocalDate.now(), time);
        }