Can I get correct datetime from LocalDateTime in defferent zone?

290 views Asked by At

I saved current date time in database using LocalDateTime.now(), i see that it is saved as Map of key-value, In the map i see key for time, month , year, second , nano --etc. But i see nowhere information regarding zone. So if retrieve same time date in different zone say USA (data saved from India) then how to do it?

3

There are 3 answers

2
DigitShifter On BEST ANSWER

LocalDateTime is not tied to a locality or time zone

Quoting extensive description of java.time, see answer with 1500+ upvotes, about LocalDateTime:

They are not tied to any one locality or time zone. They are not tied to the timeline. They have no real meaning until you apply them to a locality to find a point on the timeline.

Quoting more from extensive description of java.time, about java-time type usage:

So for business apps, the "Local" types are not often used as they represent just the general idea of a possible date or time not a specific moment on the timeline. Business apps tend to care about the exact moment an invoice arrived, a product shipped for transport, an employee was hired, or the taxi left the garage. So business app developers use Instant and ZonedDateTime classes most commonly.

Here is an example with one of the recommended types for specifying time zone "America/Los_Angeles":

ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("America/Los_Angeles"));

Here is another variation doing the same thing:

ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(Instant.now(), ZoneId.of("America/Los_Angeles"));

And another variation doing the same thing:

ZonedDateTime zonedDateTime = ZonedDateTime.now().withZoneSameInstant(ZoneId.of("America/Los_Angeles"));

You can see Available Zone Ids by using ZoneId.getAvailableZoneIds():

ZoneId.getAvailableZoneIds().stream().forEach(System.out::println);

Learn more about java.time at:

extensive description of java.time, see answer with 1500+ upvotes.

You can read more about ZonId and ZoneOffset here: https://docs.oracle.com/javase/10/docs/api/java/time/ZoneId.html https://docs.oracle.com/javase/10/docs/api/java/time/ZoneOffset.html

7
Arvind Kumar Avinash On

As shown in the overview of modern date-time classes in Java, the classes which have time-zone information are ZonedDateTime, OffsetDateTime, OffsetTime etc. The class, LocalDateTime does not have time-zone information.

enter image description here

As mentioned here,

The class that handles both date and time, without a time zone, is LocalDateTime, one of the core classes of the Date-Time API. This class is used to represent date (month-day-year) together with time (hour-minute-second-nanosecond) and is, in effect, a combination of LocalDate with LocalTime. This class can be used to represent a specific event, such as the first race for the Louis Vuitton Cup Finals in the America's Cup Challenger Series, which began at 1:10 p.m. on August 17, 2013. Note that this means 1:10 p.m. in local time. To include a time zone, you must use a ZonedDateTime or an OffsetDateTime, as discussed in Time Zone and Offset Classes.

Given below is an example code for working with OffsetDateTime:

OffsetDateTime odt = OffsetDateTime.now(ZoneOffset.UTC);// Change ZoneOffset as applicable
PreparedStatement st = conn.prepareStatement("INSERT INTO mytable (columnfoo) VALUES (?)");
st.setObject(1, odt);
st.executeUpdate(); 
st.close();

Learn more about the modern date-time API at Trail: Date Time.

6
babli1897 On
Date date = new Date();
ZonedDateTime zonedDateTime = date.toInstant().atZone(ZoneId.of("US/Eastern"));

There are different Time zones, on the basis of their values you can have datetime.