我应该使用Java日期和时间课程,还可以和Joda Time等第三方库一起去吗? [英] Should I use Java date and time classes or go with a 3rd party library like Joda Time?

查看:91
本文介绍了我应该使用Java日期和时间课程,还可以和Joda Time等第三方库一起去吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



与第三方库相比,使用Java日期和时间类的优缺点是什么? a href =http://www.joda.org/joda-time/ =noreferrer> Joda时间?我猜这些第三方图书馆存在很好的理由,但我从来没有真正比较过他们。

解决方案

编辑:现在Java 8已经发布,如果可以使用,那么这样做!在我看来, java.time 比Joda Time更干净。但是,如果你被卡在Java-8之前,请阅读...



Max要求使用Joda的优缺点...



优点:




  • 它的工作非常好。我强烈怀疑Joda中的错误比标准Java库少得多。

  • 它旨在鼓励您以正确的方式思考日期/时间处理(如果不是不可能),那么Java库中的一些错误是非常困难的(如果不是不可能) - 分离本地时间的概念(例如我在任何地方上午7点醒来)和时间瞬间(我在太平洋标准时间下午3点打电话给詹姆斯,可能不是下午3点,但是这是相同的时刻)

  • 我相信它可以更轻松地更新时区数据库,哪些相对频繁地更改

  • 它有一个很好的不变性故事,这使得生活更容易IME。

  • 从不变性领先,所有格式化器都是线程安全的,这是非常好的,因为您几乎总是希望通过应用程序重新使用单个格式化程序

  • 您将学习开始学习 java.time ,因为它们至少在某种程度上相似。



缺点:




  • 这是一个注意API学习(虽然文档很不错)

  • 这是另一个构建反对和部署的库

  • 当您使用Java 8时,仍然存在有些工作要迁移你的技能

  • 我以前没有有效地使用 DateTimeZoneBuilder 。这是一个非常的罕见用例。



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




  • 这是工作。为什么在为您工作时,您的工作已经完成?

  • 您的团队的新手更有可能熟悉Joda而不是本地生成的API

  • <你可能会因为最简单的用法而错误了...甚至你最初认为只需要简单的功能,这些东西就有一个日益复杂的习惯,一个很小的一点一点日期和时间操纵是难以正常执行的。此外,内置的Java API很难正确使用,只需查看日历API日期/时间算术如何工作的规则。构建任何东西都是一个坏主意,而不是使用精心设计的库来开始。

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.

解决方案

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

Max asked for the pros and cons of using 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 does change relatively frequently
  • It has a good immutability story, which makes life a lot easier 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

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 DateTimeZoneBuilder effectively in the past. 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:

  • 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 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. Building anything on top of these is a bad idea rather than using a well-designed library to start with.

这篇关于我应该使用Java日期和时间课程,还可以和Joda Time等第三方库一起去吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆