简体   繁体   中英

Time & Timezone is not corresponding - Linux

I am stuck on why I cannot fix the time. The time of my Server is messed up. Actually, I tried "ntpdate" command as some sites suggest. But, "date" command still shows the issue.

In more detail, my Server is using UTC0. This shows 23:28:45. But, actually, the real time is 16:31:XX. So, there is roughly 7 hours difference.

Because Timezone & the time itself is not consistent, my programs are broken. My service is getting today date by Date class of Java. And, "new Date()" depends on the server date.

Is there any way to fix this difference? I tried "ntpdate" to synch time with some servers. But, it never works.

Faulty Hardware or OS

If your server computer's hardware clock is faulty or you have serious OS problems, then programming may not be able to save you. Look for help on other sites such as:

Java Can Mislead You With Date-Time

Are you aware that the toString methods on java.util.Date and .Calendar applies the JVM's current default time zone? This creates the illusion of a time zone on the date-time object when in fact there is none. And this behavior may or may not be confusing you in your situation.

The JVM has its own current default time zone which can differ from that of the operating system. Read below for details.

So use your OS utilities, rather than Java, to verify the current date-time and time zone / region settings. After you are certain of your hardware and OS, then turn your attention to Java.

Specify Desired Time Zone

Also, if the current date time is correct on your OS but the OS is set to an undesired time zone, you can deal with that in your Java code.

Indeed, you should write your Java code in such a way that you specify the desired/expected time zone rather than rely an the implicit default. As may be your case, we cannot always trust the host operating system is set to the correct time zone which in turn informs the JVM's default time zone. Furthermore, Java code can change the JVM's current default time, which affects all code in all threads running in that JVM. So this is yet another reason to specify your time zone rather than rely on default.

Joda-Time

To specify a time zone in your code you must use a decent date-time library. That means Joda-Time or the new java.time package in Java 8. Avoid the java.util.Date and .Calendar classes as they are notoriously troublesome.

In both Joda-Time and java.time, the date-time class does know its own assigned time zone. This stands in contrast to java.util.Date which does not have an assigned time zone. And you can easily adjust to another time zone.

Note that both Joda-Time and java.time use immutable objects , where rather than modify ("mutate") an object's values we create new instances based on the original object's properties. This provides built-in thread-safety .

// Three date-time objects, but all represent the same simultaneous moment in the timeline of the Universe.
DateTime nowUtc = DateTime.now( DateTimeZone.UTC );
DateTime nowParis = nowUtc.withZone( DateTimeZone.forID( "Europe/Paris" ) );
DateTime nowKolkata = nowUtc.withZone( DateTimeZone.forID( "Asia/Kolkata" ) );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM