Understanding SimpleDateFormat, Week Year, and ISO 8601 in Java
This article explains how to use Java's SimpleDateFormat for date‑time formatting and parsing, describes the pattern letters, clarifies the difference between "yyyy" and "YYYY" for week‑year calculations, and shows how ISO 8601 week definitions are supported in Java with practical code examples.
Java provides the SimpleDateFormat class to format and parse dates, allowing conversion between Date objects and textual representations using custom pattern strings.
Formatting a Date to String
The format method converts a Date to a String according to the supplied pattern, e.g. "yyyy-MM-dd HH:mm:ss".
// Date to String
Date data = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dataStr = sdf.format(data);
System.out.println(dataStr);The output follows the pattern, e.g. 2018-11-25 13:00:00 . Changing the pattern yields different formats.
Parsing a String to Date
// String to Date
System.out.println(sdf.parse(dataStr));SimpleDateFormat also supports parsing strings back into Date objects.
Date‑and‑Time Pattern Letters
Pattern letters (e.g., y , M , d , H , m , s ) define the granularity of the output; repeating a letter increases precision (e.g., "yyyy" for a four‑digit year).
What Is a Week Year?
Different locales define the first day of the week differently, and the first week of a year can be defined in several ways. The article illustrates three possible interpretations for the first week of 2020.
ISO 8601 Week Definition
ISO 8601 defines the first week of a year as the week containing the first Thursday of that year (equivalently, the week containing January 4th, or the first week with at least four days in the new year).
According to ISO 8601, the first week of 2020 spans 2019‑12‑29 to 2020‑01‑04, meaning those three days belong to 2020’s week 1.
JDK Support for ISO 8601 Week Year
Java 7 introduced the pattern letter Y (uppercase) to represent the week‑year. Using YYYY with SimpleDateFormat yields the year of the ISO week.
public class WeekYearTest {
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf1 = new SimpleDateFormat("YYYY");
System.out.println(sdf1.format(sdf.parse("2019-12-01")));
System.out.println(sdf1.format(sdf.parse("2019-12-30")));
System.out.println(sdf1.format(sdf.parse("2020-01-01")));
}
}Output:
2019
2020
2020When the pattern uses lowercase yyyy , the same dates produce:
2019
2019
2020Thus, using YYYY can lead to unexpected year values if the week‑year differs from the calendar year, so developers should prefer yyyy-MM-dd for ordinary date formatting.
The article concludes with a reminder to check codebases for accidental use of YYYY patterns.
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.