Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for instant in @DateTimeFormat #19846

Closed
spring-projects-issues opened this issue Feb 22, 2017 · 6 comments
Closed

Add support for instant in @DateTimeFormat #19846

spring-projects-issues opened this issue Feb 22, 2017 · 6 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

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

@spring-projects-issues
Copy link
Collaborator Author

Krzysztof Krason commented

Any updates here? Is this behavior intentional?

@spring-projects-issues
Copy link
Collaborator Author

Krzysztof Krason commented

Juergen Hoeller Any updates?

@spring-projects-issues spring-projects-issues added status: waiting-for-triage An issue we've not yet triaged or decided on type: enhancement A general enhancement in: core Issues in core modules (aop, beans, core, context, expression) and removed type: enhancement A general enhancement labels Jan 11, 2019
@rstoyanchev rstoyanchev added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Nov 8, 2021
@snicoll snicoll changed the title @DateTimeFormat doesn't work for Instant [SPR-15280] Add support for instant in @DateTimeFormat Dec 3, 2021
@snicoll snicoll added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Dec 3, 2021
@snicoll snicoll self-assigned this Dec 3, 2021
@snicoll snicoll added this to the 6.0 M1 milestone Dec 3, 2021
@snicoll snicoll closed this as completed in 110e0f7 Dec 3, 2021
@eldarj
Copy link

eldarj commented Jan 26, 2023

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 example 2022-10-10T10:00:00Z`

@snicoll any ideas?

@sbrannen
Copy link
Member

@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.

@eldarj
Copy link

eldarj commented Jan 27, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

6 participants