0

First of all I'm aware of this excellent answer on the difference between Instant and LocalDateTime in Java. Both have nano-second precision but the value of LocalDateTime.now() depends on the machine timezone.

I save time in Postgres database using timestamp type which saves both date and time (no time zone). I'm wondering if it's OK to de/serialize time objects using LocalDateTime under the condition that the machine which hosts the database and the production server are set to UTC. As far as I see under this condition there's no difference between the classes at least no practical difference, both will return the number of nanoseconds since Unix epoch. The advantage of using LocalDateTime is that I can immediately use time methods like getYear(), getDayOfMonth() which are not available for Instant.

hitchhiker
  • 1,099
  • 5
  • 19
  • 44
  • 1
    `LocalDateTime` does _not_ have a time zone (the question your linked mentions that). `ZonedDateTime` has time zone information. – armandino Jun 16 '22 at 23:38
  • Here is the first line from [Javadoc for LocalDateTime](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html): _"A date-time without a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30."_ – Kaan Jun 17 '22 at 03:16
  • It’s a good question but opinion-based. For my part I wouldn’t be too happy about the setup you envision. Someone happening to set a different time zone somewhere can easily cause everything to break. Also someone reading your code will need to know that times are in UTC; it won’t be stated in the code itself, which could cause confusion. You are correct, in theory you will be able to get the correct results. – Ole V.V. Jun 17 '22 at 04:33

0 Answers0