Krzysztof Krason opened SPR-15280 and commented

@DateTimeFormat annotation is supported by all Java 8 time classes except Instant, which always assumes the date is in the format "2017-02-21T13:00:00Z".

Right now when making a request with start set to 2017-02-21T13:00, following code works (uses LocalDateTime):

@RestController
public final class ReportController {

    @RequestMapping(path = "/test", method = RequestMethod.GET)
    public String report(
        @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm") LocalDateTime start) {
    return start.toString();
}

But following doesn't (fails with parsing exception):

@RestController
public final class ReportController {

    @RequestMapping(path = "/test", method = RequestMethod.GET)
    public String report(
        @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm") Instant start) {
    return start.toString();
}

Affects: 4.3.6

1 votes, 2 watchers

Comment From: spring-projects-issues

Krzysztof Krason commented

Any updates here? Is this behavior intentional?

Comment From: spring-projects-issues

Krzysztof Krason commented

Juergen Hoeller Any updates?

Comment From: YuryYaroshevich

I assume the main difficulty in implementing this feature is passing zone id(since you can't format an Instant without zone id: https://stackoverflow.com/questions/25229124/format-instant-to-string) to this formatter: https://github.com/spring-projects/spring-framework/blob/master/spring-context/src/main/java/org/springframework/format/datetime/standard/InstantFormatter.java#L42 which is registered here: https://github.com/spring-projects/spring-framework/blob/master/spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeFormatterRegistrar.java#L192

Comment From: eldarj

How come this actually doesn't work for me?

The exact same above sample throws an parser exception @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm") Instant start

What I noted is that Instant.parser is the one that tries to parse the request param, and not the TemporalAccessorParser - is this expected?

Secondly, the Instant.parser doesn't make any use of the pattern supplied in @DateTimeFormat annotation, it actually always expect the following yyyy-MM-dd'T'HH:mm:ssz' for example2022-10-10T10:00:00Z`

@snicoll any ideas?

Comment From: sbrannen

@eldarj, this issue was closed over a year ago.

If you feel that you have encountered a bug, please create a new issue and provide a minimal example that reproduces the behavior.

As for why Instant.parse(...) would be invoked without taking into account a custom format, that can happen in a fallback scenario in which org.springframework.format.datetime.standard.TemporalAccessorParser.defaultParse(String) is invoked.

Comment From: eldarj

@sbrannen Thanks a lot for the quick tip, and sure, I'll double check further and open a new issue if needed. Ty!