-1

Hi I read some article about new java.time package.And some of articles say we shouldnt use java.util.Date family.We can use offsetDateTime or LocalDateTime instead of java.util.Date. And I am wondering about what should we use instead of Timestamp in new java.time package? As I check Timestamp use

public class Timestamp extends java.util.Date
Bilgehan
  • 1,135
  • 1
  • 14
  • 41

2 Answers2

1

There are two types of time spans in Java 8:

  • Period - represents time difference between two points in years, months, days
  • Duration - represents a time duration - the actual physical time-span, doesn't depend on what is the start time

For example 1 month "period" will be 30 days in April, 31 in may, so the values might be calculated differently in different contexts. Also time zone changes (daylight saving) are taken into account. Duration of certain number of seconds, minutes, hours, days, will not depend on the context. For example 30 days will always be 30 days.

You have a good description in the doc: https://docs.oracle.com/javase/tutorial/datetime/iso/period.html

0

Timestamp equivalent

I am assuming that you are referring to the outdated java.sql.Timestamp and/or to the timestamp (without time zone) and timestamp with time zone datatypes of SQL. The first was designed for use with the last two. The answer is different for the two.

For a timestamp in SQL timestamp with time zone is clearly recommended since it actually unambiguously defines a point in time, which is in the definition of what a timestamp is. For a timestamp with time zone you should use the java.time class that you already mentioned, OffsetDateTime. Some JDBC drivers and JPA implementations will accept Instant too.

In most SQL dialects a mere timestamp is a date and time of day with high resolution (for example microseconds) without time zone. Lacking time zone it does not define a point in time, so calling it a timestamp is really a lie. In any case the corresponding java.time type is the other class you mentioned, LocalDateTime.

All of the mentioned java.time classes have resolution of nanoseconds. I know of no SQL dialect that would demand more than that.

You are fully correct. Not only is java.util.Date poorly designed and long outdated. Timestamp is a true hack on top of that class. I recommend you don’t use any of them.

Timespan equivalent

Artur Olszak in another answer has already nicely given the basis of Period and Duration. There is no need for me to repeat that. As a supplement, please be aware that even though Duration has methods for converting to and from a number of days, it isn’t really well suited for days since it assumes that a day is always 24 hours, which is not always the case because of summer time (DST) and other anomalies. As soon as you need to count days, I recommend either Period or a simple number of days.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • I need to store date with time in database?Should I use offsetdatetime this purpose and change all my date types to offsetdatetime?When I check java.time package as I understand offsedatetime has more capabilities than others. – Bilgehan Apr 13 '20 at 06:12
  • I can’t tell you exactly what’s right in your situation. If what you need in your program is a point in time, `Instant` may be a better choice. Then you can convert to `OffsetDateTime` when you save it and back again when you retrieve it. And yes, finding a time when you can make that change and test that everything still works sounds to me like a good investment for the future. – Ole V.V. Apr 13 '20 at 06:16