0

I have a problem with LocalDateTime.now() returning the wrong time(off by 2 hours, the same as the timezone)

When I check the linux server the date command returns the right time, running hwclock -r shows the right time too.

This is from the command timedatectl https://gyazo.com/9c70f5719a03492e0d7fdff3128f2f1d

The local time is what is shown in date and hwclock -r but LocalDateTime.now() returns the universal time/RTC time

Even MySQL shows the local time (doing SELECT NOW())

Any ideas to why/what I have to change to make LocalDateTime.now() return the correct time - I know I could use a ZonedTimeZone but this is not only for my own projects but it also applies to other java programs/plugins running on the machine that I don't have control over

pppery
  • 3,731
  • 22
  • 33
  • 46
Sumsar1812
  • 616
  • 1
  • 9
  • 32
  • If you know what you’re doing, you can always use `LocalDateTime.now(yourDesiredTimeZoneId)`. For example `LocalDateTime.now(ZoneId.of("Europe/Copenhagen"))`. – Ole V.V. Aug 15 '22 at 22:16
  • Yea I know I could also do that, but thx though! The problem still persist for the programs/plugins that isnt my own that use LocalDateTime.now() – Sumsar1812 Aug 15 '22 at 22:18
  • Maybe the JVM default time zone has been set to UTC. That happens. To check, try `System.out.println(ZoneId.systemDefault());`. – Ole V.V. Aug 15 '22 at 22:19
  • 2
    The problem is that a plugin — the same one or a different one — may set the JVM default time zone. If that’s the issue, I can’t tell you the solution. Other than, as I said already, not rely on it. Which is always a good and sound idea. – Ole V.V. Aug 15 '22 at 22:21
  • @OleV.V. it also happens if I just build a standalone jar only containing a print of LocalDateTime.now() (like this https://gyazo.com/4d76e2614be8b17f8a08e77c610569ee) so I don't suspect its another plugin or am i wrong? – Sumsar1812 Aug 15 '22 at 22:25
  • The ZoneId.systemDefault() do return Etc/UTC – Sumsar1812 Aug 15 '22 at 22:26
  • Bingo! You may start `java` with `-Duser.timezone=Europe/Copenhagen`. It works until some plugin sets it otherwise … :-( Does this answer your question? [How to set a JVM TimeZone Properly](https://stackoverflow.com/questions/2493749/how-to-set-a-jvm-timezone-properly) – Ole V.V. Aug 15 '22 at 22:28
  • @OleV.V. But if this runs by itself, and not as a plugin then nothing should be able to set it right?. I tried to run the jar with your argument but it didnt change the time, it still returns the universal time – Sumsar1812 Aug 15 '22 at 22:34
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/247297/discussion-between-ole-v-v-and-sumsar1812). – Ole V.V. Aug 15 '22 at 22:36

2 Answers2

2

With the help from @OleV.V I got it working with the jvm argument -Duser.timezone=Europe/Paris

For example:

java -Duser.timezone=Europe/Paris -jar TestProject.jar
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Sumsar1812
  • 616
  • 1
  • 9
  • 32
  • Thanks. This is a great working solution on a global level, it is been very useful in my case since i am working on so many different microservices – Ahmad Khundaqji Mar 10 '23 at 13:50
2

Wrong class

I cannot imagine a situation where calling LocalDateTime.now() is the right thing to do.

The LocalDateTime class cannot represent a moment, a specific point on the timeline, as it lacks the context of a time zone or offset from UTC.

When you call the LocalDateTime.now method, the JVM’s current default time zone is implicitly applied. But keep in mind that the current default can be changed at runtime, at any moment, by any code in any app within that JVM.

Simply put, you are using the wrong class.

ZonedDateTime

To capture the current moment as seen in a particular time zone, use ZonedDateTime class.

ZoneId z = ZoneId.of( "Europe/Paris" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • The OP explained to me in a chat that they used a 3rd party plugin in which the dubious/erroneous `LocalDateTIme.now()` call happened. I upvoted your answer as a guide to other readers. Hopefully not many are in the same situation as the OP. – Ole V.V. Aug 15 '22 at 23:05