Java 8 Date API vs Calendar / Date / DateFormat

时间:2015-07-28 12:41:46

标签: date calendar java-8 jodatime java-time

Java 8中有一个新的n-cool Date API,java.time package。据我所知,它比旧类强设计更好,模糊度更低,线程更安全。不过,我不知道如何使用它:

  • 我应该专门使用它而不是旧类吗?
  • 我应该更换旧课程的现有用法,因为新的东西要好得多吗?
  • 我是否应该完全不使用java.util.Calendar, java.util.Datejava.sql.Datejava.text.DateFormat来支持新的API,还是使用的用例,其中旧类仍然是优选的
  • 由于新的Date API类似于使用Java 8流API替换Joda Time APIGuava FluentIterable是否已过时了?

我知道这些问题很多,但他们觉得彼此有些相关。如果有人可以回答整个问题,那将是非常棒的,但也可以赞赏好的部分答案。

1 个答案:

答案 0 :(得分:18)

Should I use it exclusively instead of the old classes?

No, no need to exclude old classes. The old java.util.Date/.Calendar work the same, unchanged. The have not been deprecated.

You can mix and match all three frameworks (old classes, Joda-Time, and java.time). Just be careful of some identical or similar class names -- watch your import statements!

See The Tutorial, Legacy Date-Time Code.

Also, the ThreeTen-Extra project extends java.time with additional features. The "ThreeTen" refers to JSR 310 that defines java.time.

Should I replace existing usages of old classes whereever I spot them because the new stuff is so much better?

If the old code is working as intended, then no need to change now.

If you were concerned about that old code possibly not handling time zones properly or have been wanting to do more localization, the you might want to rework them using java.time. Even then, remember that you can easily convert between j.u.Date and Instant. So you could leave your business logic as-is with j.u.Date/.Calendar and change only the user-presentation code to use java.time.

Should I refrain from using java.util.Calendar, java.util.Date, java.sql.Date and java.text.DateFormat in favor of the new API all together OR are there use cases where the old classes are still preferable?

You will need the old classes for interoperation with old code and libraries that expect the old types. Again, the old classes are not deprecated and are not going away. They will probably never go away given their extensive usage and the Java team’s top priority being preservation of backward-compatibility.

The new java.time classes are far superior, that's why they were added. And they are more logical and easier to use. So yes, it would be beneficial and pleasant to learn how to use them. But not urgent. In a crunch, write code the old way you are used to. When you can afford the time to learn (Tutorial) and to perform extra testing, start to use the new classes.

A newbie programmer should certainly focus their learning on java.time.

Has the Joda Time API become obsolete thanks to the new Date API similarly to the substitution of Guava FluentIterable by Java 8 stream API?

Joda-Time was the inspiration for java.time. The same folks invented both. They intend for java.time to be their "2.0" re-invention of Joda-Time, what they would have done then if they knew what they know now. They have said Joda-Time users should transition over to java.time.

That said, Joda-Time is not going away. It is still worked on, still updated with bug fixes and fresh tz time zone data. And it is very important to the large Android community who have no other decent date-time library (that I know of). So you can continue to rely on Joda-Time, no urgent need to rip out old code, but expect no new features or enhancements.

Joda-Time is well-worn and proven while java.time has had a few minor bugs and kinks to work out. Joda-Time and java.time each have features the other lacks. So personally, I mix-and-match to best fit. I rely on Joda-Time while dabbling with java.time.

Plan an eventual transition to java.time but no rush, no panic.