Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LocalDateTime.now() wrong hour

Tags:

java

datetime

I'm using LocalDateTime.now() to get the date and time of system, but the time is with an hour in past.

If the system has 14:52, now() return 13:52.

OS of system is Windows 10.

like image 934
KunLun Avatar asked Sep 28 '18 11:09

KunLun


2 Answers

LocalDateTime is wrong class

Never use LocalDateTime to represent a moment, a specific point on the timeline. Purposely lacking any concept of time zone or offset-from-UTC, this type represents potential moments along the range of about 26-27 hours (range of time zones around the globe).

Current moment

To get the current moment in UTC, use Instant.

Instant instant = Instant.now() ;

To get the current moment as seen in the wall-clock time used by people in a particular region (a time zone), use ZonedDateTime.

I suspect your problem is that your expected time zone was not actually the current default zone when your code ran. In your code you failed to specify a time zone, and so the JVM’s current default time zone was silently applied. You could verify the current default by calling ZoneId.systemDefault().toString().

Relying implicitly on the JVM’s current default time zone is a bad practice in my opinion. Better to always specify your desired/expected time zone explicitly. Always pass the optional ZoneId argument.

ZoneId z = ZoneId.of( "America/Montreal" ) ; // Or get the JVM’s current default time zone: ZoneId.systemDefault() 
ZonedDateTime zdt = ZonedDateTime.now( z ) ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes. Hibernate 5 & JPA 2.2 support java.time.

Where to obtain the java.time classes?

  • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
    • Java 9 brought some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • Later versions of Android (26+) bundle implementations of the java.time classes.
    • For earlier Android (<26), the process of API desugaring brings a subset of the java.time functionality not originally built into Android.
      • If the desugaring does not offer what you need, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above) to Android. See How to use ThreeTenABP….
like image 97
Basil Bourque Avatar answered Nov 09 '22 21:11

Basil Bourque


java.util.TimeZone.setDefault() is the method in java which is used to set the timeZone in java. It takes TimeZone as input parameter. You can get an object of TimeZone by TimeZone.getTimeZone("id"); There are different id for different time Zone. For example id for me is "Asia/Calcutta" so passing that will return me the TimeZone of my region.

TimeZone tzone = TimeZone.getTimeZone("Asia/Calcutta");
TimeZone.setDefault(tzone);

Above line will change my timezone to calcutta region.

like image 30
Nawnit Sen Avatar answered Nov 09 '22 21:11

Nawnit Sen