The black pot of YYYY-MM-DD, we don't carry it!

Author: Tony rabbit

Writing this blog post is a record of cross year bug s.

Last year, the next group's partner calculated the number of days between two dates, because the calculation was wrong for cross year reasons.

At that time, the little sister of the test group did not simulate this scenario, which led to the production environment directly affecting the online data.

Today, I happened to meet you at the technology forum Java Operation bug of date.

1 yyyy and YYYY

Don't read. Look at the code

public void testWeekBasedYear() {
  Calendar calendar = Calendar.getInstance();
  // 2019-12-31
  calendar.set(2019, Calendar.DECEMBER, 31);
  Date strDate1 = calendar.getTime();
  // 2020-01-01
  calendar.set(2020, Calendar.JANUARY, 1);
  Date strDate2 = calendar.getTime();
  // Capital YYYY
  SimpleDateFormat formatYYYY = new SimpleDateFormat("YYYY/MM/dd");
  System.out.println("2019-12-31 turn YYYY/MM/dd format: " + formatYYYY.format(strDate1));
  System.out.println("2020-01-01 turn YYYY/MM/dd format: " + formatYYYY.format(strDate2));
  // Small YYYY
  SimpleDateFormat formatyyyy = new SimpleDateFormat("yyyy/MM/dd");
  System.out.println("2019-12-31 turn yyyy/MM/dd format: " + formatyyyy.format(strDate1));
  System.out.println("2020-01-01 turn yyyy/MM/dd format: " + formatyyyy.format(strDate2));

Output results:

Transfer from December 31, 2019 to YYYY/MM/dd format: December 31, 2020
 Transfer from January 1, 2020 to YYYY/MM/dd format: January 1, 2020
 Transfer from December 31, 2019 to yyyy/MM/dd format: December 31, 2019
 Transfer from January 1, 2020 to yyyy/MM/dd format: January 1, 2020

Careful students should find that the YYYY/MM/dd used on December 31, 2019 has now become December 31, 2020

?? Why?

YYYY is so capable of running 2020 Time to go?

If you use YYYY to format the delivery date of the goods I bought at the end of 2019, can I receive the goods at the end of 2020? It's a big bug!

**What kind of monster is YYYY? * * 👺

Java's DateTimeFormatter pattern "YYYY" gives you the week-based-year, (by default, ISO-8601 standard) the year of the Thursday of that week.


Here is the format code with YYYY

  • 12 / 29 / 2019 will be formatted to 2019. This week belongs to 2019

  • 12 / 30 / 2019 will be formatted to 2020. This week belongs to 2020

YYYY, week based year is regulated by ISO 8601.

On the day of December 31, 2019, the year of installation and calculation has already belonged to 2020 It's been formatted for years 2020 Year, the date of the following month does not change.

2 dd and DD

private static void tryit(int Y, int M, int D, String pat) {
  DateTimeFormatter fmt = DateTimeFormatter.ofPattern(pat);
  LocalDate dat = LocalDate.of(Y,M,D);
  String str = fmt.format(dat);
  System.out.printf("Y=%04d M=%02d D=%02d " +
    "formatted with " +
    "\"%s\" -> %s\n",Y,M,D,pat,str);
public static void main(String[] args){

Output results:

Y=2020 M=01 D=20 formatted with "MM/DD/YYYY" -> 01/20/2020
Y=2020 M=01 D=21 formatted with "DD/MM/YYYY" -> 21/01/2020
Y=2020 M=01 D=22 formatted with "YYYY-MM-DD" -> 2020-01-22
Y=2020 M=03 D=17 formatted with "MM/DD/YYYY" -> 03/77/2020
Y=2020 M=03 D=18 formatted with "DD/MM/YYYY" -> 78/03/2020
Y=2020 M=03 D=19 formatted with "YYYY-MM-DD" -> 2020-03-79


The last three dates are all wrong. The capitalized DD here represents the day of the year, not the day of the month.

Boys and girls must remember not to make similar mistakes.

3. Conclusion

Yyyy and yyyy are different, and DD and DD are also different.

We don't carry this pot.

Recommend to my blog to read more:

1.Java JVM, collection, multithreading, new features series

2.Spring MVC, Spring Boot, Spring Cloud series tutorials

3.Maven, Git, Eclipse, Intellij IDEA series tools tutorial

4.Latest interview questions of Java, backend, architecture, Alibaba and other large factories

Feel good, don't forget to like + forward!

Tags: Programming Java Spring jvm Maven

Posted on Tue, 19 May 2020 00:14:16 -0700 by subasi