Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java8 : LocalDateTime or TimeStamp

Tags:

java

java-8

Which is more preferable at when or where ?

I don't know exactly what are actual differences between them.

From documentation of LocalDateTime

...Time is represented to nanosecond precision. For example, the value "2nd October 2007 at 13:45.30.123456789" can be stored in a LocalDateTime.

I assumed LocalDateTime can also accept until nanoseconds.So I think , I can replace my codes with LocalDateTime these are declared as TimeStamp. Please correct me if I am wrong.

Scenario : we had planned to upgrade our project with Java-8. Modified old codes styles with new features of JAVA-8 (eg:Lambda,Streams etc;). But we got trouble while deciding for Dates and Times. Most of codes with java.util.Date were changed to java.time.LocalDate or java.time.LocalDateTime. For the cases of TimeStamp , I have no idea about the question

Should we replace them with LocalDateTime ?

like image 289
Cataclysm Avatar asked Feb 02 '17 09:02

Cataclysm


People also ask

What is the difference between timestamp and LocalDateTime?

Timestamp are actually equivalents of java. time. Instant , not LocalDateTime , Date Timestamp and Instant are instances of Unix time, while LocalDateTime is a DateTime in current time zone.

Should I use LocalDateTime or instant?

Instant and LocalDateTime are two entirely different animals: One represents a moment, the other does not. Instant represents a moment, a specific point in the timeline. LocalDateTime represents a date and a time-of-day. But lacking a time zone or offset-from-UTC, this class cannot represent a moment.

What is difference between LocalDateTime and date?

LocalDate, you need to ignore the time part. Another key difference between Date and LocalDate is the time zone. The old date doesn't have a timezone associated with it but when you print it will print in the system's default time zone.

How do I set timestamp in LocalDateTime?

Timestamp : LocalDateTime ldt = new LocalDateTime(); DateTimeFormatter dtf = DateTimeFormatter. forPattern("yyyy-MM-dd HH:mm:ss"); Timestamp ts = Timestamp.


2 Answers

In current development you should prefer LocalDateTime and the other Java8 time classes.

  • They provide the advantage of a much clearer separation between point-in-time definitions (Instant) and duration (Duration) or fragment based definitions (LocalDate, LocalTime).

  • They allow a really good set of methods for manipulation/calculation logic (in difference to java.util.Date).

  • Also unit conversion is covered (Duration.toDays()).

  • Last but not least the Timezone hell is covered (ZonedDateTime).

A little disadvantage is the lack of support by quite a lot third-party API you may want to take advantage of. But this should just be a matter of time and conversion from Java8 time API to Calendar/Date is no showstopper.

If you have a mature software then replacing the old Date/Calendar based interfaces by Java8 based ones is just a risk until you make use of some advantages mentioned above.

If you want to replace an old TimeStamp parameter with something out of the Java8 time toolbox then you may either use Instant, LocalDateTime or ZonedDateTime. The difference is that Instant value is handled as ZoneOffset.UTC based, when it comes to calculation, while LocalDateTime is by definition without any timezone relation.

Hint: Using a LocalDateTime is a quite nice thing if something should happen at 2018-01-02 10:24:12 for the system in e.g. India and the system in the US. In nearly all other cases you may prefer explicitly defining a timezone using Instant or ZonedDateTime.

like image 96
Alexander Avatar answered Oct 20 '22 00:10

Alexander


Both java.util.Date and and java.sql.Timestamp are actually equivalents of java.time.Instant, not LocalDateTime, Date Timestamp and Instant are instances of Unix time, while LocalDateTime is a DateTime in current time zone.

You can clearly see that because both classes feature this nice method (inherited from java.util.Date):

java.util.Date::toInstant

What I assume you meant by replacing Date with LocalDate is you was actually replacing java.sql.Date, not java.util.Date. Now, sql.Date IS an equivalent of LocalDate, and is not equivalent to Instant, because sql.Date lacks Time component (despite sql.Date being subclass of util.Date, calling getSeconds() on it will result in exception).

like image 29
M. Prokhorov Avatar answered Oct 19 '22 23:10

M. Prokhorov