Conclusions come first
Use in JavaYYYY-MM-dd
When formatting the date, if the day is on the New Year's Eve, the year may be incorrectly calculated as the next year (such as December 26, 2021 as 2022). and useyyyy-MM-dd
The correct year will always be returned. The root of the problem isYYYY
andyyyy
Different definitions of years:YYYY
is a "week-based year", andyyyy
It is a "natural year".
Articles are updated continuously, you can search on WeChat for "Half a head"Read as soon as possible
Problem recurs
Try formatting with the following code2021-12-26
:
import ;
import ;
public class DateFormatDemo {
public static void main(String[] args) {
SimpleDateFormat sdf1 = new SimpleDateFormat("YYYY-MM-dd");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date(121, 11, 26); // December 26, 2021
((date)); // Output: 2022-12-26
((date)); // Output: 2021-12-26
}
}
The results are obvious,YYYY
The year jumped to 2022.
Cause analysis
1. YYYY
It is a "week-based year"
-
definition:
YYYY
Follow ISO 8601 standards, indicating "year to which the date week belongs." -
Weekly calculation rules:
- Default one week fromStarting Sunday,arriveEnded on Saturday(Available through
Locale
Adjustments, such as some areas starting Monday). - If a week eves the New Year (that is, the week includes the first day of the New Year), the week will be attributed to the year when the New Year is located.
- Default one week fromStarting Sunday,arriveEnded on Saturday(Available through
byDecember 26, 2021As an example:
- This weekDecember 26, 2021 (Sunday) to January 1, 2022 (Saturday)。
- Since most days of this week (4 days) belong to 2022,
YYYY
It will be attributed to 2022.
2. yyyy
It is a "natural year"
-
definition:
yyyy
Directly represent the natural year where the date is located, and has nothing to do with the week. - Always return to the actual year regardless of whether the date spans a week or a month.
How to avoid trampling on pitfalls?
-
Priority use
yyyy
Unless it is explicitly necessary to calculate based on the year of the week, the date should be used when formattingyyyy
。 -
Understand business scenarios
- Financial systems, weekly statistics, etc. may be required
YYYY
(If you generate reports by weekly). - Normal date scenarios (such as order time, logging) should be used
yyyy
。
- Financial systems, weekly statistics, etc. may be required
-
Notice
Locale
The impact of- The weekly start days are different in different regions (for example, the United States starts on Sunday by default, and Europe starts on Monday).
- Available
()
or()
Specify the area.
-
Alternatives to Java 8+
useDateTimeFormatter
hour,yyyy
correspondY
andu
Behavior is clearer:LocalDate date = (2021, 12, 26); DateTimeFormatter formatter1 = ("YYYY-MM-dd"); DateTimeFormatter formatter2 = ("yyyy-MM-dd"); ((formatter1)); // 2022-12-26 ((formatter2)); // 2021-12-26
Summarize
-
YYYY
andyyyy
The difference: Year based on week vs natural year. -
root cause: The rules of the New Year’s Eve Week lead to
YYYY
year offset. -
Best Practices: If there are no special needs, always use it
yyyy-MM-dd
Format date.
A small format character difference may lead to serious logical errors during the New Year's Eve. Remember:Nothing trivial to date formatting!