google / google/error-prone

Prevent more common DateTimeFormatter issues

Open
#1,462 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
7.2k
Forks
820
Avg merge
5h 9m
Merged PRs (30d)
50

Description

### 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/

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.