Week Year In Date Pattern

ID

java.week_year_in_date_pattern

Severity

high

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Java

Tags

reliability

Description

Reports a date-pattern string literal passed to SimpleDateFormat’s constructor or to `DateTimeFormatter.ofPattern that uses the week-year letter Y (Y, YY or YYYY) without a week-of-year field (w or W) in the same pattern.

Rationale

Week year and calendar year agree for every day of the year except the first and last week, where they silently diverge. Formatting 31 December 2015 with YYYY under SimpleDateFormat yields 2016: a wrong year, produced without any error, that only shows up a few days a year — exactly the kind of bug that survives ordinary testing.

// Bad - under SimpleDateFormat, '2015-12-31' formats as '2016/12/31'
new SimpleDateFormat("YYYY/MM/dd");
DateTimeFormatter.ofPattern("YYYY/MM/dd");
The exact year the two APIs disagree on is not the same, because the week rule is not the same. SimpleDateFormat uses the locale’s week definition, which for the default US locale starts the week on Sunday and needs only one day in the new year, putting 31 December 2015 in week 1 of week-year 2016. DateTimeFormatter follows ISO-8601 unless told otherwise — weeks start on Monday and need four days — and leaves that same date in week-year 2015. Both are wrong for the same reason, on different days: the point of the rule is that Y is not the calendar year in either API, not that a particular date shifts.

Remediation

Use the lowercase y calendar-year field unless the week year is genuinely intended, in which case pair it with a week-of-year field (w).

// Good - calendar year
new SimpleDateFormat("yyyy/MM/dd");
DateTimeFormatter.ofPattern("yyyy/MM/dd");

// Good - week year is intended, paired with week-of-year
new SimpleDateFormat("YYYY-ww");

© 2026 Xygeni. All rights reserved