springdatetimespring-mvcgethttp-request-parameters

Spring trucantes time part from a Date GET RequestParam


I have a Spring MVC REST controller with an object as param:

public List<AccessResponse> getAccesses(AccessRequestParams params) throws Exception {
    log.debug("Received get request");
}

The param is a simple bean with some properties, I copy here just the important date property:

public class AccessRequestParams  {

    Date since=null;
    ...

    public SincePageFilter() {
        super();
    }

    public Date getSince() {
        return since;
    }

    public void setSince(Date since) {
        this.since = since;
    }
    
    
}

When I call this URL:

http://localhost:8080/v1/accesses?since=2017-05-04T12:08:56

The since property is filled with 2017-05-04 00:00:00, Why it truncates the time part?

I also tried to:

But I did not find any solution.

My Spring config has this BEAN:

@Bean
public FormattingConversionService mvcConversionService() {
    DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService(false);

    DateTimeFormatterRegistrar dateTimeRegistrar = new DateTimeFormatterRegistrar();
    dateTimeRegistrar.setDateFormatter(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    dateTimeRegistrar.setDateTimeFormatter(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    dateTimeRegistrar.registerFormatters(conversionService);

    DateFormatterRegistrar dateRegistrar = new DateFormatterRegistrar();
    dateRegistrar.setFormatter(new DateFormatter("yyyy-MM-dd"));
    dateRegistrar.registerFormatters(conversionService);

    return conversionService;
}

Solution

  • You have registered a global DateFormatter for the type java.util.Date with format yyyy-MM-dd. This works as expected.