Date formatting cross year bug, meet you unexpectedly?

Posted by mlnsharma on Sat, 04 Jan 2020 10:54:03 +0100

Before 2020, date formatting also provided programmers with a cross-year bug. I wonder if your system is experiencing any?

Near the New Year's Day of 2020, many websites show dates like 2020/12/29, 2020/12/30, 2020/12/31.Amazing no?Even the subscription number assistant tool provided by WeChat has made this error.

The next two pictures are from the Subscription Number Assistant on December 31 of this Public Number Program New View.

Add part of the time display for fans.

Part of the time display in the comment area.

In the picture above, the new fans show time and comment time of "2020/12/31".Now let's analyze the cause of this bug.Instances are worth a thousand words. First restore the bug with examples.

Example 1, Restore example:

public class DateFormatBug {

	public static void main(String[] args) throws ParseException {
		// Example 1
		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 what the results of the first and second lines are?It's smart to guess a description because, as already mentioned, this instance is a bug restore.

The print log is:

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

Amazing no?Printing the string "2020-1-1 13:12:12" as a date would turn out to be December 29, 2019!!!And then the date should turn into 2020-12-29!It's a complete mess. We want a date of 2020-1-1.

Example 2, extended 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 time, just change the date from 2020-1-1 to 2019-12-31. Guess what the result is?Make sure you don't guess.Here are the printed results:

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

Dec 30, 2018?This time even the year is wrong.

Okay, no more shutdowns, if you have a plugin installed on your IDE that has the Alibaba Operations Specification Manual.You will find a hint on YYYY-MM-dd HH:mm:ss.Similarly, if you open a new version of the Alibaba Java Development Manual (Huashan edition), this has been clearly stated.

So, where can I get the new Alibaba Java Development Manual?Friends who follow the public number "Program New Horizon" can get a PDF version by simply replying to "005".In the new year, it is OK to look at the development manual of the big factory and learn from the experience to avoid treading pits.Of course, keeping an eye on Public Number continuous learning is another good option.

You can also refer to the description of week-based-year in javadoc with 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.

To summarize, in a week-based year, a week belongs to only one year.The first week of a year starts on the first day of the week and has more than four days, so what year the year spans depends on.

However, the best way is to avoid using uppercase Y to represent the year.

Letter is better than no book. We'll change "Y" to "y" in the code and 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);
}

The printed results are as follows:

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 cross-year bug has been fixed.

After this question, will we ask the question: Where is the difference between a master and a novice?Perhaps the question above can be answered, as well as date formatting, which normally works well with both styles, making it impossible to distinguish between a master and a novice.Only in some extreme situations at a certain time can a master be seen.

So where do experts come from?Jump out of the pit.Maybe he fell in and out sooner than you did, maybe he had such an unexpected encounter with a wide range of books, or maybe he just saw this article like you do now.

Original Link: New year bug, meet you unexpectedly?>

<center><b>Program New Horizon</b>: Don't miss the excitement and growth</center>

Topics: Java Oracle