简体   繁体   中英

What datetime class should I use for my case classes?

I have case classes that may be used for representing models that originate from either a database table or API object.

Should I use joda or java.util.date or java.sql.date or?

This is for a playframework app, and I will use these models to display the datetime on the UI side of things where I will convert the date to the current users timezone also.

I'm just really confused.

Agreeing with the Answer by mkurz

java.time

Both java.util.Date and java.sql.Date , and their related classes, have been supplanted by the java.time framework built into Java 8 and later. See Oracle Tutorial . Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP .

Search Stack Overflow to learn more (after reading the Oracle Tutorial linked above). Many examples and discussions have been posted.

Basic concepts

  • Date-time handling is surprisingly tricky. Leave your intuition at the door.
  • Strings are not date-time values; they are a representation of date-time values. Focus on using objects, not strings.
  • You may work with a date-only, a time-only, or a date-time.
  • Know that offset-from-UTC is just a number of hours/minutes/seconds while a time zone is a superset, adding rules for handling anomalies such as Daylight Saving Time (DST). Use OffsetDateTime for one, and ZonedDateTime for the other.
  • ISO 8601 standard provides sensible formats when parsing & generating Strings to represent date-time values.
  • The “Local…” types mean “no specific locality”. These have no meaning, are not points on the timeline, until you specify the context of a specific time zone.
  • The 3-4 letter abbreviations such as EST or IST you commonly see in the media are not time zones, are not standardized, and are not unique(!).Proper time zone names are continent/region .
  • Apply a time zone for presentation to the user, but most of your business logic, data storage, and data exchange should be in UTC (the Instant class).

Tips

  • While programming, forget about your own local time zone. Think in UTC .
  • Learn to read & write 24-hour clock times.

I would recommend http://www.joda.org/joda-time/quickstart.html

It works really nicely with play's json formatters and comes with a ton of helpers.

You are looking for java.time.ZonedDateTime which was introduced in Java 8 and should be used for representing date and time related information that have a timezone. You can save values like 2016-05-30T00:23:27.070Z with it. There is no need to use a third party library like joda time anymore (maybe in other cases there is, but not in your's)

(Do not use java.util.Date - most of it's methods are deprecated, for good reasons)

As a blogger saied: 「Scala does not have a datetime package, but we can use the ones provided by Java.

Java 8 provides a better data/time API, so 3rd-party libraries like Joda-Time is no longer required.」

Here is the blog: https://www.hackingnote.com/en/scala/datetime

This will be helpful to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM