Fundamentals 3 min read

Why Using ‘YYYY’ Instead of ‘yyyy’ Can Break Your Java Dates

A subtle mistake of using the week‑based year pattern ‘YYYY’ instead of the calendar year ‘yyyy’ in Java DateTimeFormatter can cause unexpected date bugs, especially around year boundaries, and this article explains the symptoms, root cause, and how to avoid it.

Programmer DD
Programmer DD
Programmer DD
Why Using ‘YYYY’ Instead of ‘yyyy’ Can Break Your Java Dates

Yesterday on v2ex I encountered a bug caused by using the pattern YYYY -MM-dd in Java date formatting. Similar issues often appear when details are overlooked, leading to unexpected behavior in specific times or environments.

Whether you have faced this bug or not, after reading this article you should pay attention to this detail to avoid future mistakes.

Bug Manifestation

Bug Cause

The pattern YYYY represents the week‑based‑year. For example, today is already considered year 2020 according to this rule, while yyyy would still be 2019.

YYYY means the year of the week to which the day belongs; a week starts on Sunday and ends on Saturday, and if a week spans the new year, that week is counted as part of the next year.

Related Resources

Java DateTimeFormatter Patterns Documentation

Article on Y2K‑like bugs

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaprogrammingDateTimeFormatterDate Bug
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.