我应该使用 Java 日期和时间类还是使用像 Joda Time 这样的 3rd 方库? [英] Should I use Java date and time classes or go with a 3rd party library like Joda Time?

查看:22
本文介绍了我应该使用 Java 日期和时间类还是使用像 Joda Time 这样的 3rd 方库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

推荐答案

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

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 询问使用 Joda 的利弊...

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

优点:

  • 效果很好,非常好.我强烈怀疑 Joda 中的错误比标准 Java 库少得多.由于设计原因,Java 库中的一些错误确实很难(如果不是不可能的话)修复.
  • 它旨在鼓励您以正确的方式思考日期/时间处理 - 将当地时间"(例如无论我在哪里,早上 7 点叫醒我")和即时(我"我在太平洋标准时间下午 3 点给詹姆斯打电话;他所在的地方可能不是下午 3 点,但它是同一时刻")
  • 我相信更新时区数据库会更容易,它确实变化相对频繁
  • 它有一个很好的不变性故事,这使 IME 的生活很多更容易.
  • 从不变性开始,所有格式化程序都是线程安全的,这很棒,因为您几乎总是希望通过应用程序重用单个格式化程序
  • 您将在学习 Java 8 中的 java.time 方面领先一步,因为它们至少有些相似
  • 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

缺点:

  • 这是另一个需要学习的 API(尽管文档非常好)
  • 这是另一个用于构建和部署的库
  • 当您使用 Java 8 时,仍有一些工作需要迁移您的技能
  • 我过去未能有效地使用 DateTimeZoneBuilder.这是一个非常的用例.
  • 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.

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

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:

  • 这是工作.既然已经为你完成了工作,为什么还要工作?
  • 与您自己开发的 API 相比,您团队的新成员更有可能熟悉 Joda
  • 除了最简单的用途之外,您很可能会犯错……即使您最初认为您只需要简单的功能,但这些东西有一个变得越来越复杂的习惯,一个一点点.日期和时间操作很难正确执行.此外,内置的 Java API 很难正确使用 - 只需查看日历 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 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 这样的 3rd 方库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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