This article is participating in “Java Theme Month – Java Debug Notes Event”, see < Event link > for more details.

Today, I saw another time question on StackOverflow — why is midnight on December 31, 1927 so strange? The person who asked the question gave the following Java code (I made some changes to make sure you can copy it and compile it)

I’ve highlighted a few lines in it, just to compare the difference between ‘1927-12-31 23:54:07’ and ‘1927-12-31 23:54:08’ by a few seconds, obviously by a second. But the program’s output is different.

import java.text.SimpleDateFormat; import java.text.ParseException; import java.util.Date; import java.util.TimeZone; class time{ public static void main(String[] args) throws ParseException { SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); sf.setTimeZone(TimeZone.getTimeZone("Asia/shanghai")); String str3 = "1927-12-31 23:54:07"; String str4 = "1927-12-31 23:54:08"; Date sDt3 = sf.parse(str3); Date sDt4 = sf.parse(str4); long ld3 = sDt3.getTime() /1000; long ld4 = sDt4.getTime() /1000; System.out.println(ld3); System.out.println(ld4); System.out.println(ld4-ld3); }}Copy the code

Now, let’s look at the program’s output :(yes, 353 seconds)

353-1325491905-1325491552

Stackoverflow is really powerful and solved the problem within 15 minutes of everyone asking the questioner for a time zone (Shanghai, China). It’s pretty amazing. The reason? You need to take a look at this page. (FOR fear of being blocked or blocked, I am used to screensaver, if someone can develop a software that can be captured on the go, and then if the source is deleted it can be peer-to-peer from people who have downloaded it, then this software should have a good domestic market. Out of the way, Sorry)

As you can see from the image above, at 23:59:59 on December 31, 1927, the next second should have been 0:0:0 on January 1, 1928, but the time was adjusted by five minutes and 52 seconds to 23:54:08 on December 31, 1927, thus completing the 352 seconds of travel. So our Java program has such a problem, this is really a miracle.

Why this adjustment? I couldn’t find it on Google, but when I checked the time in Beijing on timeanddate.com, it only dates back to 1970, so I assumed that the chaotic succession of regimes in Recent Chinese history might be the reason. So I looked at the physical time difference between Beijing and Shanghai. Sure enough, the time difference between Beijing and Shanghai was about 5 minutes and 50 seconds. Therefore, I think the time change should be from Shanghai time to Beijing time. Believe it or not, I believe it.

From this, I learned the following:

  1. The Time zone implementation of Java is quite powerful. That kind of detail can be taken into account.
  2. Local time is a complete pot, should try not to.
  3. If you’re developing timezone dependent programs, make sure your system uses GMT standard time and only changes to local time when displayed.

Unlicensed programmers, do you feel programming pressure from this example?