我应该使用 Java 日期和时间类还是使用像 Joda Time 这样的 3rd 方库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/589870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:35:38  来源:igfitidea点击:

Should I use Java date and time classes or go with a 3rd party library like Joda Time?

javadatetimejodatime

提问by

I'm creating a web based system which will be used in countries from all over the world. One type of data which must be stored is dates and times.

我正在创建一个基于网络的系统,该系统将在世界各地的国家/地区使用。必须存储的一种数据类型是日期和时间。

What are the pros and cons of using the Java date and time classes compared to 3rd party libraries such as Joda time? I guess these third party libraries exist for a good reason, but I've never really compared them myself.

Joda time等 3rd 方库相比,使用 Java 日期和时间类的优缺点是什么?我想这些第三方库的存在是有充分理由的,但我自己从未真正比较过它们。

采纳答案by Jon Skeet

EDIT: Now that Java 8 has been released, if you can use that, do so! java.timeis even cleaner than Joda Time, in my view. However, if you're stuck pre-Java-8, read on...

编辑:现在 Java 8 已经发布,如果你可以使用它,就这样做吧!java.time在我看来,它甚至比 Joda Time 更干净。但是,如果您在 Java-8 之前遇到困难,请继续阅读...

Max asked for the pros and cons of using Joda...

Max 询问使用 Joda 的利弊...

Pros:

优点:

  • It works, very well. I strongly suspect there are far fewer bugs in Joda than the standard Java libraries. Some of the bugs in the Java libraries are really hard (if not impossible) to fix due to the design.
  • It's designed to encourage you to think about date/time handling in the right way - separating the concept of a "local time" (e.g "wake me at 7am wherever I am") and an instant in time ("I'm calling James at 3pm PST; it may not be 3pm where he is, but it's the same instant")
  • I believe it makes it easier to update the timezone database, which doeschange relatively frequently
  • It has a good immutability story, which makes life a loteasier IME.
  • Leading on from immutability, all the formatters are thread-safe, which is great because you almost alwayswant to reuse a single formatter through the application
  • You'll have a head-start on learning java.timein Java 8, as they're at least somewhat similar
  • 它有效,非常好。我强烈怀疑 Joda 中的错误比标准 Java 库少得多。由于设计的原因,Java 库中的一些错误确实很难(如果不是不可能的话)修复。
  • 它旨在鼓励您以正确的方式思考日期/时间处理 - 将“当地时间”(例如“无论我在哪里,早上 7 点叫醒我”)和即时(“我打电话给 Ja​​mes在太平洋标准时间下午 3 点;他所在的地方可能不是下午 3 点,但它是同一时刻”)
  • 我相信它可以更轻松地更新时区数据库,它确实变化相对频繁
  • 它具有良好的不变性的故事,这让生活很多容易IME。
  • 从不变性开始,所有格式化程序都是线程安全的,这很好,因为您几乎总是希望通过应用程序重用单个格式化程序
  • 您将java.time在 Java 8 中抢占先机,因为它们至少有些相似

Cons:

缺点:

  • It's another API to learn (although the docs are pretty good)
  • It's another library to build against and deploy
  • When you use Java 8, there's still some work to migrate your skills
  • I've failed to use the DateTimeZoneBuildereffectively in the past. This is a veryrare use case though.
  • 这是另一个需要学习的 API(尽管文档非常好)
  • 这是另一个构建和部署的库
  • 当您使用 Java 8 时,仍有一些工作需要迁移您的技能
  • 我过去未能DateTimeZoneBuilder有效地使用。不过,这是一个非常罕见的用例。

To respond to the oxbow_lakes' idea of effectively building your own small API, here are my views of why this is a bad idea:

为了回应 oxbow_lakes 有效构建自己的小型 API 的想法,以下是我对为什么这是一个坏主意的看法:

  • It's work. Why do work when it's already been done for you?
  • A newcomer to your team is much more likely to be familiar with Joda than with your homegrown API
  • You're likely to get it wrong for anything beyond the simplest uses... and even if you initially thinkyou only need simple functionality, these things have a habit of growing more complicated, one tiny bit at a time. Date and time manipulation is hardto do properly. Furthermore, the built-in Java APIs are hard to useproperly - just look at the rules for how the calendar API's date/time arithmetic works. Building anything on top of these is a bad idea rather than using a well-designed library to start with.
  • 这是工作。既然已经为你完成了工作,为什么还要工作?
  • 你团队的新人比你自己开发的 API 更有可能熟悉 Joda
  • 除了最简单的用途之外,您可能会弄错任何东西……即使您最初认为您只需要简单的功能,这些东西也有变得越来越复杂的习惯,一次一点点。日期和时间操作很难正确进行。此外,内置的 Java API 很难正确使用- 只需查看日历 API 的日期/时间算法如何工作的规则。在这些之上构建任何东西都是一个坏主意,而不是使用设计良好的库开始。

回答by gizmo

Well, unless you intend to wait for Java 8, hoping that they will implement a better APIfor manipulating date and time, yes, please, use Joda-Time. It's time saving and avoid many headaches.

好吧,除非你打算等待 Java 8,希望他们能实现更好的 API来操作日期和时间,是的,请使用Joda-Time。它可以节省时间并避免许多麻烦。

回答by oxbow_lakes

The answer is: it depends

答案是:视情况而定

JODA (and JSR-310) is a fully-functional date/time library, including support for use with multiple calendar systems.

JODA(和 JSR-310)是一个功能齐全的日期/时间库,包括支持与多个日历系统一起使用。

Personally I found JODA to be a step too far in terms of complexity for what I need. The 2 principal (IMHO) mistakes in the standard java Dateand Calendarclasses are:

就我个人而言,我发现 JODA 就我需要的复杂性而言太过分了。标准 JavaDateCalendar类中的 2 个主要(恕我直言)错误是:

  1. They are mutable
  2. They mix up the concept of a Year-Month-Day from an Instant-In-Time
  1. 它们是可变的
  2. 他们将“年-月-日”的概念与“即时”混为一谈

Although these are addressed by JODA, you'll find it quite easy to roll your own classes for YearMonthDayand Instant, which both use the java classes under the hood for actual "calendrical" calculations. Then you don't have to familiarize yourself with an API of >100 classes, a different formatting/parsing mechanism etc.

尽管 JODA 解决了这些问题,但您会发现为YearMonthDayand滚动您自己的类非常容易,它们都在幕后Instant使用 java 类进行实际的“日历”计算。那么您就不必熟悉超过 100 个类的 API、不同的格式/解析机制等。

Of course, if you do need complete representation of different chronologies (e.g. Hebrew) or wish to be able to define your own imaginary Calendar system (e.g. for a game you are writing) then perhaps JODA or JRS-310 is for you. If not, then I would suggest that rolling your own is possibly the way to go.

当然,如果您确实需要完整表示不同的年表(例如希伯来语)或希望能够定义您自己想象的日历系统(例如您正在编写的游戏),那么 JODA 或 JRS-310 可能适合您。如果没有,那么我建议您自己滚动可能是可行的方法。

The JSR-310 spec lead is Stephen Colebourne who wrote JODA in the 1st place, so will logically replaceJODA.

JSR-310 规范的领导者是 Stephen Colebourne,他在第一名编写了 JODA,因此在逻辑上将取代JODA。

回答by Anthony Roy

It all depends on what you are doing with the dates. If you are simply persisting them, them Java's built in Dates will probably do all you want them to. However if you are doing extensive time date manipulation, you're probably better off with Joda.

这完全取决于您对日期的处理方式。如果您只是将它们持久化,那么 Java 内置的 Dates 可能会满足您的所有要求。但是,如果您正在进行大量的时间日期操作,那么使用 Joda 可能会更好。

回答by Guest

You should use a Joda-Time library, because:

您应该使用 Joda-Time 库,因为:

  1. Joda-Time supports the ISO 8601 standard, which is a standard way of
    date representation.
  2. Adding and subtracting a day/month/year is easier in Joda-Time than java.util.date.
  3. An initialization by a give date is so much easier in Joda-Time.
  4. Joda-Time supports timezone as well.
  5. Joda-Time has a better built-in parsing. A wrong date like "2014-02-31" is thrown as an error: Exception in thread "main" org.joda.time.IllegalFieldValueException: Cannot parse "2014-02-31": Value 31 for dayOfMonth must be in the range [1,28].
  1. Joda-Time 支持ISO 8601 标准,这是一种标准的
    日期表示方式。
  2. 在 Joda-Time 中添加和减去日/月/年比 java.util.date 更容易。
  3. 在 Joda-Time 中按给定日期进行初始化要容易得多。
  4. Joda-Time 也支持时区。
  5. Joda-Time 有更好的内置解析。像“2014-02-31”这样的错误日期会作为错误抛出:Exception in thread "main" org.joda.time.IllegalFieldValueException: Cannot parse "2014-02-31": Value 31 for dayOfMonth must be in the range [1,28].

You may like this page for more details: http://swcodes.blogspot.com/

您可能会喜欢此页面以获取更多详细信息:http: //swcodes.blogspot.com/