Why Using “YYYY‑MM‑dd” Breaks Your Java Dates on New Year's Day

A Java developer discovers that formatting dates with the pattern “YYYY‑MM‑dd” causes a year‑off bug on New Year's Day, learns that “YYYY” denotes a week‑based year, and is advised to always use “yyyy‑MM‑dd” for reliable calendar dates.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Why Using “YYYY‑MM‑dd” Breaks Your Java Dates on New Year's Day

Problem Overview

During a project a bug surfaced on New Year's Day when dates were formatted with the pattern YYYY-MM-dd. The resulting dates were off by one year, leading to failures in the application.

Below are screenshots showing the incorrect outputs:

Root Cause

In Java date‑time formatting, the pattern YYYY stands for the week‑based year , not the calendar year. A week is considered to start on Sunday and end on Saturday; if a week spans the turn of the year, the whole week is counted as belonging to the next year.

Consequently, using YYYY‑MM‑dd on dates near the year boundary (e.g., 2019‑12‑31) yields a year value of 2020, while the correct calendar year is 2019. The proper pattern for calendar dates is yyyy, which always reflects the actual year.

Recommendation

For everyday date formatting, always use yyyy‑MM‑dd. Reserve YYYY only for calculations that specifically require the week‑based year.

By paying attention to this subtle difference, developers can avoid similar bugs in the future.

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.

bugDate FormattingYYYY
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

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.