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?

- 71,965
- 6
- 74
- 110

- 153
- 1
- 9
-
The gold is here: [Java Best Practice for Date Manipulation/Storage for Geographically Diverse Users](https://stackoverflow.com/questions/40075780/java-best-practice-for-date-manipulation-storage-for-geographically-diverse-user). And here: [Best practices with saving datetime & timezone info in database when data is dependant on datetime](https://stackoverflow.com/questions/44965545/best-practices-with-saving-datetime-timezone-info-in-database-when-data-is-dep). – Ole V.V. Oct 06 '20 at 19:40
3 Answers
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

- 801
- 5
- 12
-
I see when i store zonedatetime in firestore there are hell lot of information store in form of map, so can't i just store localdatetime and then convert them using ZonedDateTime zdtAtAsia = localdatetime.atZone( zoneId ); – Rafa Oct 07 '20 at 02:11
-
Recommended types for zoned time are ZonedDateTime, OffsetDateTime, that are by default already tied to UTC time zone with an offset if you want. If it is more convient for you to convert LocalDateTime to ZonedDateTime due to Firestore, by tying time zone to LocalDateTime, then just go ahead. – DigitShifter Oct 07 '20 at 05:48
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.
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
withLocalTime
. 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 aZonedDateTime
or anOffsetDateTime
, 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.

- 71,965
- 6
- 74
- 110
-
-
@Rafa - I've already mentioned this in my answer. I've also posted some links for you to explore it further. Feel free to comment in case of any doubt/issue. – Arvind Kumar Avinash Oct 06 '20 at 08:55
-
My point is what the use of localdatetime if i save it today and later on i have no clue about exact date, because if we retrieve date later on i do not have information about which zone this date belong to... – Rafa Oct 06 '20 at 08:58
-
2@Rafa: The point is that you *don't* use it when you need to know which time zone it's relevant in. That doesn't make it a useless type, any more than saying "What's the use of `int` if it can't store half values?" Use the right type for your use case. – Jon Skeet Oct 06 '20 at 09:07
-
@Rafa. LocalDateTime is great for Booking appointments, read about LocalDateTime and booking appointments at https://stackoverflow.com/questions/32437550/whats-the-difference-between-instant-and-localdatetime, see answer with 900+ upvotes. – DigitShifter Oct 06 '20 at 11:11
-
I see when i store zonedatetime in firestore there are hell lot of information store in form of map, so can't i just store localdatetime and then convert them using ZonedDateTime zdtAtAsia = localdatetime.atZone( zoneId ); – Rafa Oct 07 '20 at 02:10
-
@Rafa - No e.g. `LocalDateTime.now().atZone(ZoneId.of("Asia/Calcutta"))` and `ZonedDateTime.now(ZoneId.of("Asia/Calcutta"))` will show you different results. There is a way to convert `LocalDateTime` into `ZonedDateTime` but I suggest you store `OffsetDateTime` into the database and then you can convert it into different formats as per your requirement. – Arvind Kumar Avinash Oct 07 '20 at 08:31
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.
-
-
1
-
localdatetime can be converted to date LocalDateTime localDateTime = LocalDateTime.now(); Date loc=Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); – babli1897 Oct 06 '20 at 05:47
-
1No, don’t. Mixing old and outdated (`Date`) with modern (`LocalDatetime`, `ZonedDateTime`) just overcomplicates things. Stick to java.time, the modern Java date and time API exclusively. Forget about `Date`. – Ole V.V. Oct 06 '20 at 19:37
-
I see when i store zonedatetime in firestore there are hell lot of information store in form of map, so can't i just store localdatetime and then convert them using ZonedDateTime zdtAtAsia = localdatetime.atZone( zoneId ); – Rafa Oct 07 '20 at 02:10
-
1@Rafa: So you store the LocalDateTime and the time zone ID, ideally with the offset as well (so that you can differentiate for values which are ambiguous due transitions). – Jon Skeet Oct 07 '20 at 05:59