New Year's Eve date formatting bug, if you bump into?

Before the advent of 2020, the date format operations prepared a New Year's Eve level programmers bug, I do not know whether your system is experiencing?

A few days approaching, many sites appeared in 2020 New Year's Day of 2020/12 / 29,2020 / 12 / 30,2020 / 12/31 display similar to this date. Not amazing? Even micro-channel subscription number provided by assistant tools have emerged such a mistake.

The following two maps are public number "New Horizons program" subscription number in December 31 Assistant Assistant screenshot.

New fan time display of parts.

image

Comment time zone portion of the displayed content.
image

The figure above, time and comments of new fans show times are "2020/12/31." So, here we come to analyze the causes of this bug occurs. Examples worth a thousand words, first restore it by example this bug.

One example, restore examples:

public class DateFormatBug {

    public static void main(String[] args) throws ParseException {
        // 示例一
        printBugDate();
    }

    private static void printBugDate() throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
        Date date = sdf.parse("2020-1-1 13:12:12");
        System.out.println(date);
        String dateStr = sdf.format(date);
        System.out.println(dateStr);
    }
}

Guess the first line and the second line print results are what? If the guess is smart for a description, as has been said above, this example is to restore bug.

Print log is:

Sun Dec 29 13:12:12 CST 2019
2020-12-29 13:12:12

Not amazing? The string "2020-1-1 13:12:12" resolved to date to actually print out the December 29, 2019 up! ! ! And then processed to date has turned into a day 2020-12-29! Completely messed up, what we want is 2020-1-1 date ah.

Example Two, extending example:

private static void printBugDateExtend() throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
    Date date = sdf.parse("2019-12-31 13:12:12");
    System.out.println(date);
    String dateStr = sdf.format(date);
    System.out.println(dateStr);
}

This is just the date was changed from 2020-1-1 2019-12-31, print guess what the result is? Ensure you guess. Here is the print results:

Sun Dec 30 13:12:12 CST 2018
2019-12-30 13:12:12

30 December 2018? The even years are wrong.

Well, not guessing, if Alibaba practices manual installed on your IDE plug-ins. You will find "YYYY-MM-dd HH: mm: ss" above has prompted the information. Similarly, if you open a new version (version Huashan) Ali Baba Java development manual, this issue has been clearly explained.

image

So, where can you get the new version of "Ali Baba Java Development Manual"? Public concern number "New Horizons program," a friend, simply reply to "005" to get the PDF version. The new year, nothing to see development manual manufacturers, learn about the experience, to avoid stepping pit is good. Of course, public concern number of continuous learning is another good choice.

At the same time, also with reference to the description of the javadoc week-based-year, and the following links: https://docs.oracle.com/javase/8/docs/api/java/time/temporal/WeekFields.html

A week is defined by:

(1) The first day-of-week. For example, the ISO-8601 standard considers Monday to be the first day-of-week.

(2) The minimal number of days in the first week. For example, the ISO-8601 standard counts the first week as needing at least 4 days.

Together these two values allow a year or month to be divided into weeks.

It boils down to this: in the year based on the week, the week belongs to only one year. The first week of the year in claim starts from the first day of the week, and the number of days to more than 4 days (the minimum number of days), so that a specific year New Year circumferential also depends on the specific situation.

But no matter what, the best way is to avoid using uppercase "Y" to represent the year.

Believe everything the book as no book, we will code "Y" changed to "y", then execute it again to see the effect.

private static void printDate() throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = sdf.parse("2020-1-1 13:12:12");
    System.out.println(date);
    String dateStr = sdf.format(date);
    System.out.println(dateStr);
}

private static void printDateExtend() throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = sdf.parse("2019-12-31 13:12:12");
    System.out.println(date);
    String dateStr = sdf.format(date);
    System.out.println(dateStr);
}

Printing results are shown below:

Wed Jan 01 13:12:12 CST 2020
2020-01-01 13:12:12
Tue Dec 31 13:12:12 CST 2019
2019-12-31 13:12:12

Obviously, this is New Year's Eve bug fixes.

After this, we will ponder whether a question: Where is the difference between experts and novices is not? Perhaps you can answer the above questions, the same date format, two way functions well under normal circumstances, can not distinguish between expert and novice. While only seen in high master of a given moment in some extreme cases sometime.

So experts come from? From the pit to jump out. Perhaps he had fallen into earlier than you go out, maybe he read books that have a "chance encounter", that is probably the same now as you read this article.

Original link: " New Year's Eve bug, if you bump into? "


New Horizons program : exciting and growth are not to be missed

New Horizons program - micro-channel public number

Guess you like

Origin www.cnblogs.com/secbro/p/12131361.html