[英]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:优点:
java.time
in Java 8, as they're at least somewhat similarjava.time
先机,因为它们至少有些相似Cons:缺点:
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 的想法,以下是我对为什么这是一个坏主意的看法:
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
Date
和Calendar
类中的 2 个主要(恕我直言)错误是:
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 解决了这些问题,但您会发现为
YearMonthDay
和Instant
滚动您自己的类非常容易,它们都在后台使用 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 库,因为:
Exception in thread "main" org.joda.time.IllegalFieldValueException: Cannot parse "2014-02-31": Value 31 for dayOfMonth must be in the range [1,28].
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.