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

Different behavior of the @DateTimeFormat when running on Local env and on Cloud env (Heroku) #42430

Closed
rayman245 opened this issue Sep 24, 2024 · 6 comments
Labels
for: external-project For an external project and not something we can fix status: duplicate A duplicate of another issue

Comments

@rayman245
Copy link

rayman245 commented Sep 24, 2024

I noticed some inconsistency in the behavior of the @DateTimeFormat (DTF) annotation where passing the date "30-9-2024 7:41:00 pm" using @DateTimeFormat(pattern="d-M-yyyy h:mm:ss a") worked on my local but failed in cloud environment, and this was after upgrading the project from SB2 to SB3.3.3.

Tested out a few scenarios and below is my results:

image

image

not sure if could be related to spring-projects/spring-framework#30649 ?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Sep 24, 2024
@bclozel
Copy link
Member

bclozel commented Sep 24, 2024

This is a duplicate of spring-projects/spring-framework#33151

@bclozel bclozel closed this as not planned Won't fix, can't repro, duplicate, stale Sep 24, 2024
@bclozel bclozel added status: duplicate A duplicate of another issue for: external-project For an external project and not something we can fix and removed status: waiting-for-triage An issue we've not yet triaged labels Sep 24, 2024
@sbrannen
Copy link
Member

Hi @rayman245,

For such issues, we are planning to suggest the use of fallback patterns that take a narrow non-breaking space (\u202F) into account.

In light of that, can you please confirm that the following addresses your issue?

@DateTimeFormat(pattern = "d-M-yyyy h:mm:ss a", fallbackPatterns = "d-M-yyyy h:mm:ss\u202Fa")

Thanks,

Sam

@rayman245
Copy link
Author

Hi @sbrannen, for me the fix was more of changing in the format used. I suppose yes the above pattern would have worked as well.

But in my opinion, the workaround above isn't clean, as the non-breaking space is only for use prior to the 'a', and there is still normal space between the date and time. This would potentially lead to confusion and poor readability.

@sbrannen
Copy link
Member

sbrannen commented Nov 6, 2024

Thanks for the feedback, @rayman245.

for me the fix was more of changing in the format used.

What was the exact change you made?

as the non-breaking space is only for use prior to the 'a', and there is still normal space between the date and time.

That's correct. The primary pattern contains normal spaces, and the fallback pattern contains a narrow non-breaking space before the a in order to support that use case as well.

But in my opinion, the workaround above isn't clean,

I'm not quite sure what you mean by that.

Can you please explain?


p.s. For additional information on the subject, you may find the Date and Time Formatting with JDK 20 and higher wiki page I'm working on useful.

@rayman245
Copy link
Author

rayman245 commented Nov 6, 2024

Hello @sbrannen

What was the exact change you made?

We decided to use ISO_LOCAL_DATE_TIME instead, and we handle showing the AM/PM by formatting it again on the frontend.

I'm not quite sure what you mean by that.

Can you please explain?

Thanks again for providing the link to the wiki page, what I meant by 'not clean' was my initial concern with using a fallback pattern like '\u202F' readability. Most developers might not be aware of this issue and therefore not expect there to be a point in failure. Due to the elusiveness there is a chance that it could only be detected in production.

Another point I see is that the fallback pattern only works with UTF, I believe most projects may already be using UTF but some older projects may still be on the ISO-8859-1 encoding, and would be harder for them to refactor this.

Do let me know if I may be mistaken, as it could be a concern only on my end.

@sbrannen
Copy link
Member

Thanks for the feedback, @rayman245.

We decided to use ISO_LOCAL_DATE_TIME instead, and we handle showing the AM/PM by formatting it again on the frontend.

That's a great way to handle that: use ISO standard formats for communication with the server and handle localization in the client. 👍

In the wiki page we also promote the use of ISO standard formats.

what I meant by 'not clean' was my initial concern with using a fallback pattern like '\u202F' readability. Most developers might not be aware of this issue and therefore not expect there to be a point in failure. Due to the elusiveness there is a chance that it could only be detected in production.

I'm pretty confident that most developers will not be aware of this, but that won't make the problem go away. That's why we created that wiki page... to raise awareness.

As for the readability of \u202F, yes that's not intuitive. So, I'd recommend documenting that it's a narrow non-breaking space in the code or Javadoc. That's exactly what I did in Spring Framework's own test suite, because I don't expect my colleagues to know what \u202F is. In fact, in a few years I'll probably forget as well. 😉

Another point I see is that the fallback pattern only works with UTF, I believe most projects may already be using UTF but some older projects may still be on the ISO-8859-1 encoding, and would be harder for them to refactor this.

That's true; however, Java source code should always be compiled with UTF-8 or UTF-16 (see JEP 400).

Though, the other potentially tricky part is that your application has to support UTF-8 for both input and output for date/time parsing and formatting, respectively. That's why I added a note about ISO-8859-1 and UTF-8 in that wiki page.

Do let me know if I may be mistaken, as it could be a concern only on my end.

I hope I've addressed your concerns.

Regards,

Sam

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: external-project For an external project and not something we can fix status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

4 participants