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

Prevent more common DateTimeFormatter issues #1462

Open
codylerum opened this issue Dec 31, 2019 · 3 comments
Open

Prevent more common DateTimeFormatter issues #1462

codylerum opened this issue Dec 31, 2019 · 3 comments

Comments

@codylerum
Copy link
Contributor

Description of the problem / feature request:

Create new bug pattern which alerts if a java.time.format.DateTimeFormatter is created using capital Y (like YYYY-dd-MM) without a w indicating that it is using week based year.

https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

Feature requests: what underlying problem are you trying to solve with this feature?

Prevent users from making a mistake with formatting for example the following code

System.out.println("Pattern: yyyy-MM-dd = " + DateTimeFormatter.ofPattern("yyyy-MM-dd").format(LocalDate.of(2019,12,31)));
System.out.println("Pattern: YYYY-MM-dd = " + DateTimeFormatter.ofPattern("YYYY-MM-dd").format(LocalDate.of(2019,12,31)));

Will Print

Pattern: yyyy-MM-dd = 2019-12-31
Pattern: YYYY-MM-dd = 2020-12-31

This can be a common mistake.

It may also be useful to alert on other patterns such as

D (day of year) in combination with M (month) where

DateTimeFormatter.ofPattern("YYYY-MM-DD").format(LocalDate.of(2019,12,31) will produce 2020-12-365

Bugs: what's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

DateTimeFormatter.ofPattern("YYYY-MM-dd").format(LocalDate.of(2019,12,31)

What version of Error Prone are you using?

2.3.4

Have you found anything relevant by searching the web?

Somewhat of a common issue. Jodatime used x for week based year to limit confusion.

https://www.reddit.com/r/java/comments/ehucj3/important_at_this_time_of_the_year_beware_of_yyyy/

@tbroyer
Copy link
Contributor

tbroyer commented Dec 31, 2019

Isn't that already taken care of by https://errorprone.info/bugpattern/MisusedWeekYear?

@codylerum
Copy link
Contributor Author

@tbroyer Yes, I missed that and it handles the Week Based Year. Thanks.

Less common I'm sure, but maybe there should be a MisuedDayOfYear around using D in combination with M

DateTimeFormatter.ofPattern("yyyy-MM-DD").format(LocalDate.of(2019, 1, 31) Produces 2019-01-31
DateTimeFormatter.ofPattern("yyyy-MM-DD").format(LocalDate.of(2019, 2, 1) Produces 2019-02-32
DateTimeFormatter.ofPattern("yyyy-MM-dd").format(LocalDate.of(2019, 2, 1) Produces 2019-02-01

I confirmed that the following does not throw a compile warning with errorprone.

System.out.println(DateTimeFormatter.ofPattern("yyyy-MM-DD").format(LocalDate.of(2019, 2, 1)));

@codylerum codylerum changed the title Prevent common DateTimeFormatter issues Prevent more common DateTimeFormatter issues Dec 31, 2019
@graememorgan
Copy link
Member

Nice suggestion, thanks! This has been implemented and sent out for review internally. Should be in a future version.

netdpb pushed a commit that referenced this issue Jan 10, 2020
cgdecker pushed a commit that referenced this issue Jan 13, 2020
Fixes external #1462

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=289114572
cgdecker pushed a commit that referenced this issue Jan 13, 2020
Fixes external #1462

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=289114572
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants