Skip to main content

Today I faced one issue.

While convert System.Date to To String with “YYYY” format. it is returning result “2025” and if you use “yyyy” then it is returning “2024”.

This is happen on 30th of Dec 2024. 

 

Please let me know the permanent solution.

 

Thanks

 

 

 

This is not a bug.

 

From the DateTimeFormatter documentation:, indicating the symbol, meaning and examples:

Y week-based-year year 1996; 96

So you're formatting the week-based-year, not the regular year. December 30th 2019 belongs to the first week of 2020, hence your output.

Use yyyy (year-of-era) or uuuu (year) instead of YYYY and you'll get 2019 instead.

Basically, YYYY should usually be used with w (week-of-week-based-year) and E (day-of-week).

Source: java - DateTimeFormatter adding a year to date after formatting - Stack Overflow


Reply