The temperature in Shanghai dropped below zero today, so I didn’t want to get up at all. I wanted to sleep until lunch. The Bug to be fixed is the same Bug that has been mentioned many times before: “I heard that another brother got hit for using YYYY-MM-DD…” How can anyone fall for that?

But think again, in fact, it is normal, every year there are new people, even the old people, may change jobs to take over the existing problems of the system, of course, they may not pay attention to DD, have not seen the previous push pictures? Whatever the reason, there are probably other people out there who are working on fixing bugs right now, so get up and post about it.

If you happen to encounter one, you can take a look at the cause and do what you need to do; If you haven’t come across one, you’d better check yourself. If you’re already familiar with this, share this article on moments and alert other colleagues, and maybe pop up a few pictures of guys on their way to fixing bugs

The scene of the accident

Let’s write a unit test to reproduce the problem.

Test logic:

Create two date formats:

  • One is the problematic YYYY-MM-DD
  • The other is the correct use of YYYY-MM-DD

Unformat two different dates: December 25, 2021 (Saturday), December 26, 2020 (Sunday)

public class Tests { 

    @Test
    public void test(a) throws Exception {
        SimpleDateFormat df1 = new SimpleDateFormat("YYYY-MM-dd");
        SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");

        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, 2021);
        c.set(Calendar.MONTH, 11);

        // Saturday, December 25, 2021
        c.set(Calendar.DATE, 25);
        System.out.println("YYYY-MM-dd = " + df1.format(c.getTime()));
        System.out.println("yyyy-MM-dd = " + df2.format(c.getTime()));

        / / line
        System.out.println("= = = = = = = = = = = = = = = = = = = = = = = =");

        // Sunday December 26, 2021
        c.set(Calendar.DATE, 26);
        System.out.println("YYYY-MM-dd = " + df1.format(c.getTime()));
        System.out.println("yyyy-MM-dd = "+ df2.format(c.getTime())); }}Copy the code

Run the test and you can see the following output:

YYYY-MM-dd = 2021-12-25
yyyy-MM-dd = 2021-12-25
========================
YYYY-MM-dd = 2022-12-26
yyyy-MM-dd = 2021-12-26
Copy the code

Saturday, December 25, 2021, Both Formats Are correct Sunday, December 26, 2021, YYYY-MM-DD goes wrong, and the year goes to 2022

Question why

Why is yyyY-MM-DD formatted on December 26, 2021, until 2022?

YYYY is week-based-year, indicating the year in which the current week is located. The week starts on Sunday and ends on Saturday. As long as the current week is New Year’s day, the week is counted as the next year.

So December 26, 2021 is 2022 in that sense.

When yyyy is used, it’s still 2021.

Ok, finally share this article to the circle of friends, remind the other partners around it, see how can fry up a few of today to repair the Bug on the road of brother pictures

Finally, those who like to discuss technology can also join our high quality technical communication group. With excellent people, they will also become excellent

DD is hitting the nuggets, if my sharing ever helped you, you can come and vote for me. Different from other bloggers, DD side of the old rules, any activities hit the list prize DD do not want, will give supporters at last! . Click to participate if you are interested

My public account: Program ape DD, focus on sharing the latest industry news and cutting-edge technology information, pay attention to me! The first time to obtain the latest frontier, the accumulation of technical people in the corner of overtaking capital