简体   繁体   English

我应该将 Java 日期和时间类或 go 与 Joda Time 这样的第三方库一起使用吗?

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

I'm creating a web based system which will be used in countries from all over the world.我正在创建一个基于 web 的系统,它将在世界各地的国家/地区使用。 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 ?Joda time等 3rd 方库相比,使用 Java 日期和时间类的优缺点是什么? I guess these third party libraries exist for a good reason, but I've never really compared them myself.我想这些第三方库的存在是有充分理由的,但我自己从来没有真正比较过它们。

EDIT: Now that Java 8 has been released, if you can use that, do so!编辑:现在 Java 8 已经发布,如果你可以使用它,那就这样做吧! java.time is even cleaner than Joda Time, in my view.在我看来, java.time甚至比 Joda Time 更干净。 However, if you're stuck pre-Java-8, read on...但是,如果您在 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.我强烈怀疑 Joda 中的错误比标准的 Java 库要少得多。 Some of the bugs in the Java libraries are really hard (if not impossible) to fix due to the design.由于设计原因,Java 库中的一些错误确实很难(如果不是不可能)修复。
  • It's designed to encourage you to think about date/time handling in the right way - separating the concept of a "local time" (eg "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")它旨在鼓励您以正确的方式思考日期/时间处理 - 将“本地时间”的概念(例如“无论我在哪里,早上 7 点叫醒我”)和即时时间(“我正在打电话给 James太平洋标准时间下午 3 点;他所在的位置可能不是下午 3 点,但它是同一时刻”)
  • I believe it makes it easier to update the timezone database, which does change relatively frequently我相信它可以更容易地更新时区数据库,它确实会相对频繁地更改
  • It has a good immutability story, which makes life a lot easier IME.它有一个很好的不变性故事,这使 IME 的生活变得更加轻松。
  • Leading on from immutability, all the formatters are thread-safe, which is great because you almost always want to reuse a single formatter through the application从不变性开始,所有格式化程序都是线程安全的,这很棒,因为您几乎总是希望通过应用程序重用单个格式化程序
  • You'll have a head-start on learning java.time in Java 8, as they're at least somewhat similar您将在 Java 8 中学习java.time先机,因为它们至少有些相似

Cons:缺点:

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

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您团队的新人更可能熟悉 Joda,而不是您自己的 API
  • You're likely to get it wrong for anything beyond the simplest uses... and even if you initially think you only need simple functionality, these things have a habit of growing more complicated, one tiny bit at a time.除了最简单的用途之外,你很可能会弄错......即使你最初认为你只需要简单的功能,这些东西也会变得越来越复杂,一次一点点。 Date and time manipulation is hard to do properly.日期和时间操作很难正确完成。 Furthermore, the built-in Java APIs are hard to use properly - just look at the rules for how the calendar API's date/time arithmetic works.此外,内置的 Java API 很难正确使用- 只需查看日历 API 的日期/时间算法如何工作的规则即可。 Building anything on top of these is a bad idea rather than using a well-designed library to start with.在这些之上构建任何东西都是一个坏主意,而不是从使用精心设计的库开始。

Well, unless you intend to wait for Java 8, hoping that they will implement a better API for manipulating date and time, yes, please, use Joda-Time .好吧,除非您打算等待 Java 8,否则希望他们会实施更好的 API来操作日期和时间,是的,请使用Joda-Time It's time saving and avoid many headaches.它可以节省时间并避免许多麻烦。

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.就我个人而言,就我所需要的复杂性而言,我发现 JODA 走得太远了。 The 2 principal (IMHO) mistakes in the standard java Date and Calendar classes are:标准 java DateCalendar类中的 2 个主要(恕我直言)错误是:

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

Although these are addressed by JODA, you'll find it quite easy to roll your own classes for YearMonthDay and Instant , which both use the java classes under the hood for actual "calendrical" calculations.尽管 JODA 解决了这些问题,但您会发现为YearMonthDayInstant滚动您自己的类非常容易,它们都在后台使用 java 类进行实际的“日历”计算。 Then you don't have to familiarize yourself with an API of >100 classes, a different formatting/parsing mechanism etc.然后您不必熟悉 >100 个类的 API、不同的格式化/解析机制等。

Of course, if you do need complete representation of different chronologies (eg Hebrew) or wish to be able to define your own imaginary Calendar system (eg for a game you are writing) then perhaps JODA or JRS-310 is for you.当然,如果您确实需要不同年表的完整表示(例如希伯来语)或希望能够定义自己的虚构日历系统(例如,对于您正在编写的游戏),那么也许 JODA 或 JRS-310 适合您。 If not, then I would suggest that rolling your own is possibly the way to go.如果没有,那么我建议您自己滚动可能是通往 go 的方式。

The JSR-310 spec lead is Stephen Colebourne who wrote JODA in the 1st place, so will logically replace JODA. JSR-310 规范负责人是第一位编写 JODA 的 Stephen Colebourne,因此在逻辑上将取代JODA。

You should use a Joda-Time library, because:您应该使用 Joda-Time 库,因为:

  1. Joda-Time supports the ISO 8601 standard , which is a standard way of Joda-Time 支持ISO 8601 标准,这是一种标准的方式
    date representation.日期表示。
  2. Adding and subtracting a day/month/year is easier in Joda-Time than java.util.date.在 Joda-Time 中添加和减去日/月/年比 java.util.date 更容易。
  3. An initialization by a give date is so much easier in Joda-Time.在 Joda-Time 中,按给定日期进行初始化要容易得多。
  4. Joda-Time supports timezone as well. Joda-Time 也支持时区。
  5. Joda-Time has a better built-in parsing. Joda-Time 有更好的内置解析。 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].像“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/

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.如果您只是简单地保留它们,Java 内置的 Dates 可能会满足您的所有需求。 However if you are doing extensive time date manipulation, you're probably better off with Joda.但是,如果您正在进行大量的时间日期操作,那么使用 Joda 可能会更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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