January 26, 2023August 19, 2024 Difference between YYYY vs yyyy -Java Date Formatter Intro Dates in programming are hard. The ISO-8601 Date standard made them easier, but to be entirely honest I’m not ISO-8601 Certified, so I’m not very good at my job. What I do know though is the very important difference between YYYY and yyyy when formatting dates. The Difference between yyyy and YYYY According to the DateTimeFormatter Java Docs (which implements the ISO-8601 specification) y (lowercase) is year Y (uppercase) is ‘week-based-year’. This difference will cause your code to work perfectly fine, except for when dealing with dates at the very end of some years. Here is an example that I had the absolute PLEASURE of dealing with last year. For the date 31/12/2019 (December 31st, 2019), yyyy will output 2019 but YYYY will output 2020. The reason for this is the week that the 31st of December falls in, technically is the first week of 2020. This issue can be a pain to notice and debug. In my case, 31/12/2019 was being sent from the frontend, being stored correctly in the DB, but was turning into 31/12/2020 when being returned and no one had any idea why. Our default JSON date formatter was the culprit, using YYYY where it should have been using yyyy (some clumsily copied and pasted quick solution by myself, of course). Example Declare formatters // y (Lowercase) DateTimeFormatter formatteryyyy = DateTimeFormatter.ofPattern("yyyy-MM-dd"); // Y (Uppercase) DateTimeFormatter formatterYYYY = DateTimeFormatter.ofPattern("YYYY-MM-dd"); When converting from Date to String // Contains value from Database (December 31st 2019) LocalDateTime dateTime; // String value will be 2019-12-31 String yyyy = dateTime.format(formatteryyyy); // String value will be 2020-12-31 String YYYY = dateTime.format(formatterYYYY); When convert from String to Date // Actual date value will be 2020-12-31 LocalDateTime parsedDateyyyy = LocalDateTime.parse("2020-12-31", formatteryyyy); // Actual date value will be 2019-12-31 LocalDateTime parsedDateYYYY= LocalDateTime.parse("2020-12-31", formatterYYYY); It is always advisable to use yyyy instead of YYYY Java Java Interview Question cast and convert differencedate and time formats in javascriptdate as numbers to actual datesdatesdates in javascriptdifference between cast and convertglobal standard date and time formatinternational date standardiso 8601 advantages and disadvantagesparsing datesprogrammingprogramming language (software genre)statistical programmingwhat is the difference between cast and convertworking with dates in javascriptyyyy vs YYYY