Java日期与日历

Java Date vs Calendar

有人能为目前关于Date和Calendar类型的"最佳实践"提供建议吗?

在编写新代码时,是否最好始终支持Calendar而不是Date,或者是否存在Date是更合适的数据类型的情况?


日期是一个简单的类,主要是因为向后兼容的原因。如果需要设置特定日期或进行日期算术,请使用日历。日历还处理本地化。日期的前一个日期操作函数已被弃用。

就我个人而言,当有选择的时候,我倾向于使用以毫秒为单位的时间作为长(或长,视情况而定)或日历。

日期和日历都是可变的,在API中使用任何一个都会出现问题。


对于新代码(如果您的策略允许第三方代码),最好的方法是使用Joda时间库。

日期和日历都有很多设计问题,这两个问题都不是新代码的好解决方案。


  • Date和Calendar实际上是相同的基本概念(两者都代表一个时间瞬间,并且围绕着一个基础long值包装)。

  • 有人可能会说,Calendar实际上比Date更破碎,因为它似乎提供了关于一周中的某一天和某一天的某一时间等具体事实,而如果你改变了它的timeZone属性,混凝土就会变成平淡的变化!由于这个原因,这两个对象都不能真正用作一年一个月一天或一天中的某个时间的存储。

  • 仅将Calendar用作计算器,当给定Date和timeZone对象时,它将为您进行计算。避免在应用程序中使用属性类型。

  • 使用SimpleDateFormat与timeZone和Date一起生成显示字符串。

  • 如果你觉得冒险,可以使用joda-time,尽管它不必要地复杂了imho,而且很快就会被JSR-310日期API所取代。

  • 我之前已经回答过,你不难用你自己的YearMonthDay类,它在引擎盖下使用Calendar来计算日期。我对这个建议投了反对票,但我仍然认为它是一个有效的建议,因为对于大多数用例来说,Joda Time(和JSR-310)实在是太复杂了。


日期最适合存储日期对象。它是持久的,序列化的…

日历最适合操纵日期。

Note: we also sometimes favor java.lang.Long over Date, because Date is mutable and therefore not thread-safe. On a Date object, use setTime() and getTime() to switch between the two. For example, a constant Date in the application (examples: the zero 1970/01/01, or an applicative END_OF_TIME that you set to 2099/12/31 ; those are very useful to replace null values as start time and end time, especially when you persist them in the database, as SQL is so peculiar with nulls).

号


(P)I generally use date if possible.虽然它是变种的,变种人实际上是被抑制的。In the end it basically wraps a long that would represent the date/time.Conversely,I would use calenders if I have to handle the values.(p)(P)You can think of it this way:you only use stringbuffer only when you need to have strings that you can easily manipulate and then turn them into strings using tosting(……)method.在同样的方式,我只是使用日历,如果我需要处理临时数据。(p)(P)就最佳做法而言,我倾向于尽可能在领域模型之外使用不可改变的目标。这意味着减少了任何外部效应的机会,而它是由竞争对手给你的,Rather than a junit test.你用这个技术在你的班级里建立私人的最后一个领域。(p)(P)还有回到脱衣舞男的肛门这是一些代码你如何转换时间和日期之间(p)字母名称


Dates应作为不可变的时间点使用;Calendars是可变的,如果您需要与其他类协作来制定最终日期,可以传递和修改。把它们与String和StringBuilder相类似,你就会明白我认为应该如何使用它们。

(是的,我知道日期实际上在技术上是不可变的,但目的是它不应该是可变的,如果没有任何东西调用不推荐使用的方法,那么它是可变的。)


DR

advise the current"best practice" around Date and Calendar

Ok.

is it best to always favour Calendar over Date

Ok.

号

完全避免这些遗留类。改用java.time类。好的。

  • 在UTC中,暂时使用Instant
    (现代等效于Date)。
  • 在某个特定时区的某一时刻,使用ZonedDateTime
    (现代等效于GregorianCalendar)。
  • 在与UTC的特定偏移量中,暂时使用OffsetDateTime
    (在遗留类中没有等效项)
  • 对于具有未知时区或偏移量的日期时间(不是片刻),请使用LocalDateTime
    (在遗留类中没有等效值)

细节

Ortomala Lokni的答案是正确的,建议使用现代java.time类,而不是麻烦的旧遗留日期时间类(Date、Calendar等)。但这个答案表明错误的班级是等效的(见我对这个答案的评论)。好的。使用java.time

与传统的日期时间类、夜间和日间差异相比,java.time类有了很大的改进。旧的类设计不好,容易混淆,而且很麻烦。你应该尽可能避免上旧课。但是,当您需要转换为/从旧的/新的,您可以通过调用新方法添加到旧的类来实现。好的。

有关转换的更多信息,请参阅我的答案和NIFTY图到另一个问题,将JavaUTL日期转换为"Java.Time'"类型。.好的。

搜索堆栈溢出提供了数百个关于使用java.time的示例问题和答案。但这里有一个简短的概要。好的。Instant。

用Instant得到当前时刻。Instant类表示UTC时间轴上的一个时刻,分辨率为纳秒(小数点后九(9)位)。好的。

1
Instant instant = Instant.now();

埃多克斯1〔2〕

为了通过某个特定区域的壁钟时间透镜看到相同的同时时刻,应用时区(ZoneId)得到ZonedDateTime。好的。时区

以continent/region格式指定适当的时区名称,如America/Montreal、Africa/Casablanca或Pacific/Auckland。切勿使用3-4字母缩写,如EST或IST,因为它们不是真正的时区,不标准,甚至不是唯一的(!).好的。

1
2
ZoneId z = ZoneId.of("America/Montreal" );
ZonedDateTime zdt = instant.atZone();

号抵消

时区是区域相对于UTC的偏移量变化的历史记录。但有时你只得到一个没有完整区域的偏移。在这种情况下,使用OffsetDateTime类。好的。

1
2
ZoneOffset offset = ZoneOffset.parse("+05:30" );
OffsetDateTime odt = instant.atOffset( offset );

使用时区比使用单纯的偏移更可取。好的。埃多克斯1〔5〕

Local…类中的"地方"是指任何地方,而不是特定的地方。所以这个名字可能是反直觉的。好的。

关于胶印或时间带的任何信息都是紫色的。因此,他们不代表实际的时刻,他们不在时间线上。当怀疑或困惑时,使用ZonedDateTime比LocalDateTime更糟糕。搜索堆积过多,供更多的讨论。

okay.弦乐

Do not conflate date-time objects with strings that represent their value.你可以将一条弦放在一个日期-时间对象上,并且从一个日期-时间对象产生一条弦。但弦乐从来不是自我的日期。

okay.

学习ISO 8601格式,在Java.time类别中使用。

okay.关于爪哇

Java.Time Framework is built into Java 8 and later.这些等级补充了旧遗产的日期和时间等级,如java.util.Date,Calendar,SimpleDateFormat。

okay.

工作时间项目,现在在维护模式,建议移民到爪哇.时间类。

okay.

要学会更多,见《神谕教程》。并为许多例子和解释寻找堆栈。规格IS JSR 310。

okay.

使用JDBC驱动程序符合JDBC 4.2或以后,您可以交换Java.Time objects directly with your database.No need for strings nor Java.sql.*classes.

okay.

哪儿能听见爪哇.时间阶级?

okay.

  • Java se 8,Java se 9,and later
    • 建造。
    • 标准Java API的一部分有一个组合实现。
    • Java 9 address some minor features and fixes.
  • Java se 6 and Java se 7
    • Java.Time functionality is back-ported to Java 6&Amp 7 in threeten-backport.
  • 仙女座
    • 后版本的Android Bundle Implementations of the Java.Time Classes.
    • 对于早期的安卓,三个项目的适应三个后端(在上面提到)。See how to use threetenabp…

三个额外项目扩展Java.time with additional classes.这是一个为可能的未来添加Java.time提供的基础。你可以在这里找到一些用户名,如Interval,YearWeek,YearQuarter,和更多。

okay.好吧


(P)With Java 8,the New Java.time package should be used.(p)(P)Objects are immutable,time zones and day light saving are taken into account.(p)(P)你可以创造一个EDOCX1从一个老的EDOCX1(p)字母名称


我一直提倡"佐达时间"。原因如下。

  • API是一致和直观的。与java.util.date/calendar API不同
  • 它不受线程问题的影响,不像java.text.simpledateformat等(我看到过许多与不认识到标准日期/时间格式不是线程安全相关的客户端问题)
  • 它是Java Java 8的日期/时间API(JSR310)的基础。因此,您将使用API,它将成为核心Java API。
  • 编辑:如果你可以迁移到Java 8,现在用Java 8引入的Java日期/时间类现在是首选的解决方案


    (P)a little bit late at party,but Java has a new date api in JDK 8.You may want to upgrade your JDK version and embrace the standard.不再是梅西的日期/日历,不再是第三方的贾尔斯。(p)


    (P)Date should be re-developed.Instead of being a long interger,it should hold year,month,date,hour,minute,second,as separate fields.这一日期可能是一个好时机和时间区的故事。(p)(P)In our natural conversation,if setup an appointment at nov.1,2013 1PM NY time,this is a date.这不是一个日历。So we should be able to conversate like this in Java as well.(p)(P)当日期被认为是一个很长的时间(自Jan 1970年以来,或是一些时间),计算其目前的日期取决于时间安排。不同的日历会给不同的日期。This is from the prospective of giveing an absolute time(EG 1 trillion seconds after big bang).但是,我们也需要一种方便的交流方式,如一个目标封装年份、月等。(p)(P)I wonder if there are new advances in Java to reconcile these 2 objectives.也许我爪哇的知识太老了。(p)


    (P)I use calendar when I need some specific operations over the dates like moving in time,but date I find it helpful when you need to format the date to adapt your needs,recently I discovered that locale has a lot of useful operations and methods.So I'm using locale right now!(p)


    (P)BTW"date"is usually tagged as"过时/抑郁"(I don know exactly why)-Something about it is wrote thereJava:Why is the date constructor depracted,and what do I use instead?(p)(P)It looks like it's a problem of the constructor only-way through new date(Int year,int month,int day),recommended way is through calendar and set params separately.(Calder Cal=Calder.Getistance();附件(p)