In Java, the SimpleDateFormat
class is used to convert dates to strings. There is a confusing aspect here, which is the yyyy
and YYYY
formats.
-
yyyy
: This represents the normal year, such as the year 2023. -
YYYY
: This represents the week-based year, which is calculated based on the international standard ISO 8601. This standard divides a year into 52 or 53 weeks, starting from Monday and ending on Sunday. So, if a date falls on the last day of an ISO week, even if it is still in December,YYYY
may display the year of the next year.
In simple terms, if you accidentally use YYYY
instead of yyyy
when formatting a date, the year of the date may be displayed incorrectly in certain situations, such as the first week of a new year. To avoid this error, it is best to always use lowercase yyyy
to represent the year.