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
.