Do not use YYYY (uppercase) in Java date formatting

Hi,

At the end of year 2021, with the new year just around the corner, my customer was shocked to find an odd behavior of out-of-the-box @CurrentDate fuction. System returned “20221226” for 26th of December in 2021, while customer was expecting “20211226”. In this post, I am sharing the issue and how to avoid confusion.

  • How to replicate the issue
  1. When a year is coming to a close, try using @CurrentDate(YYYYMMdd, “Asia/Tokyo”). In this particular example, I am running this on the 26th of December, 2021.

  1. System returns “20221226”, against our expecations. This is one year ahead! Weird, isn’t it?

One year ahead.png

  • “yyyy” vs “YYYY”

This lowercase vs uppercase in date formatting is not a Pega-specific issue, but a general topic to Java. Java 7 introduced “YYYY” as a new date pattern to identify the date week year. An average year is 52.1775 weeks long, which means that a year might have either 52 or 53 weeks considering indivisible weeks. Using “YYYY” unintendedly while formatting a date could cause severe issues in your Java application. First, let’s look at the calendar of December, 2021.

Now, below is a result when I tested on respective day.

No Operating System Date Returned Date Correct?
1 1st of November, 2021 “20211101” Correct
2 1st of December, 2021 “20211201” Correct
3 25th of December, 2021 “20211225” Correct
4 26th of December, 2021 “20221226” Incorrect
5 27th of December, 2021 “20221227” Incorrect
6 28th of December, 2021 “20221228” Incorrect
7 29th of December, 2021 “20221229” Incorrect
8 30th of December, 2021 “20221230” Incorrect
9 31st of December, 2021 “20221231” Incorrect
10 1st of January, 2022 “20220101” Correct
11 2nd of January, 2022 “20220102” Correct

As you can see, the reason for this odd behavior is the week that the 26th - 31th of December 2021 falls in technically is the first week of 2022. In summary, “yyyy” represents the calendar year whereas “YYYY” represents the year of the week, used in the ISO year-week calendar. In most cases, “yyyy” and “YYYY” yield the same number, however they may be different. That’s a subtle difference that only causes problems around a year change so your code could have been running perfectly fine all year around only to cause a problem in the new year. This issue can be a pain to notice and debug. In general, you should almost always (in 99% of use cases) use the calendar year - “yyyy”. In my opinion, this new specification post Java 7 is very confusing, and I would advise you to add some form of linting or checking to make sure your code does not have any date formats referencing “YYYY”.

Hope this helps.

Thanks,

This can be seen 2024 as well.

@KenshoTsuchihashi

Thanks for posting this, it helped us.

@Yas_ITO

Yes, we observed this in 2024 - Is there a way to address it irrespective of case or only using y is the only way ?

@R K

The only way to address it is to change “Y” to “y”. This is a Java-level behavior, not anything to do with Pega.

  • Michael

@KenshoTsuchihashiAt the end of 2021, a customer encountered an unexpected behavior with the @CurrentDate function in Java, which returned “20221226” instead of the expected “20211226” for December 26, 2021. This issue arose from using the uppercase “YYYY” in the date format, which represents the week-based year according to the ISO-8601 standard, rather than the lowercase “yyyy” that signifies the calendar year. The discrepancy occurs because dates at the end of December may fall into the first week of the next year, causing the year to appear incorrectly incremented. To avoid such confusion and potential bugs, it is recommended to use “yyyy” for most applications that require standard year representations. Additionally, implementing linting tools or code reviews can help ensure that date formats are correctly specified. Adopting modern date-time APIs like java.time introduced in Java 8 can further enhance date handling accuracy and reliability. Understanding the distinction between “yyyy” and “YYYY” is crucial for developers to prevent subtle errors, especially around year transitions, thereby maintaining the integrity of date-related functionalities in their applications.

@KenshoTsuchihashi - Thank you for providing this article. It has been reviewed by Pega Product Engineering, and we confirm that the content is accurate. Please note that this issue persists even in later versions of Java. We appreciate your effort in sharing this resource.