Ubuntu zero-based teaching-Ubuntu hardware time and system time are inconsistent, a piece of code teaches you to be a new person!

Environment: Ubuntu20.04
The running project occasionally executes the newly added sql. Whenever the time field is involved, the writing time is 8 o'clock different from Beijing time, which is very outrageous. Obviously, the Ubuntu system display time is consistent with the Windows time. Next, I conducted a piece of God code test!

Try writing a section of the main function to get the time, and it did not surprise me; the running result is in the console.

Good guy, how desperate I am, I am still struggling with code codes at two o'clock in the morning! This must be something wrong, but I clearly remember that I reset the system time after installing Ubuntu. What should I do? I found out after consulting relevant information, it turned out to be like this:

The reason for this situation is that Windows and Ubuntu treat the hardware time (the time displayed by the BOIS on the motherboard) in a different way by default;
On a Linux computer, there are two times, one is the hardware time (the time recorded in the BIOS, called hwclock), and the other is the operating system time (osclock);
If you think about it again, the time obtained by the java program is GMT time (Greenwich Mean Time (time in time zone 0)), and the system is Beijing Time, which should be GMT+8, which is exactly 8 hours away, so it is directly inferred that the Ubuntu hardware time exists Time zone difference

Let's first check the system clock and time settings; use the following commands directly


Sure enough, the time zone defaults to the US time zone, no wonder!

At this point, it is necessary to give popular science the three time concepts obtained above:

  • UTC, namely Universal Time Coordinated, coordinated universal time
  • GMT, namely Greenwich Mean Time, Greenwich Mean Time
  • RTC time, or Real-time clock, is the hardware time of the computer
Windows and Linux see the system hardware time in different ways by default:
Linux clock is divided into system clock (System Clock) and hardware (RTC) clock. Linux treats the hardware time as UTC, and the system time is converted from UTC time. For example, if Beijing time is GMT+8, the time displayed in the system is hardware time+8.
Windows treats the system hardware (RTC) time as the local time (local time), that is, the system time is the same as the time (RTC) displayed in the BIOS.

If you know the reason, you will know how to solve it. You only need to change the time zone to a domestic one!

Set time zone

timedatectl set-timezone "Asia/Shanghai"

Come, check whether the Kangkang time zone has been set successfully:

The time zone has been set successfully; let's run the main function again;

That's it! The hardware time obtained by new Date is also consistent with the system time; have you learned?

In summary, this is the dry goods teaching of this issue. If it is not enough to eat, the bug bacteria will find a way again~

Hot article recommendation:

  1. How to install Ubuntu 20.04 on win10
  2. How to install IDEA on Ubuntu 20.04
  3. How to install maven and environment variable configuration on Ubuntu 20.04
... ...
​If you want to learn more about Ubuntu articles, please pay attention to the bug bacteria column " Ubuntu Zero Basic Teaching ", bug bacteria will continue to be updated; teach you how to eat Ubuntu thoroughly! I use my hands to achieve your dreams. ​
If you think this article is helpful to you, please don't forget to pia it directly in the lower left corner of the article to light it up! ! ! If it's me, don't hesitate to go directly to my favorites and go ashamed! No matter what you need or not in the future, eat the ashes first, hahahahahaha~~

❤If the article is helpful to you, please light up your thumb in the lower left corner at the end of the article! (#^.^#);
❤If you like the articles shared by bug bacteria, please pay attention to bug bacteria! (๑′ᴗ‵๑)づ╭❤~;
❤If you have any questions about the article, please leave a message below or join the group to discuss [group number: 708072830];
❤In view of the limited personal experience, all opinions and technical research points, if you have any objections, please reply directly to participate in the discussion (please do not make offensive remarks, thank you);
❤Copyright statement: This article is the original article of the blogger. Please attach the link to the original source and the statement of this article for reprinting. All rights reserved, piracy must be investigated! (*^▽^*).