Java 8 中的新日期和时间 API
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17527468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
New Date & Time API in Java 8
提问by Chris311
On thispage i read the following:
在此页面上,我阅读了以下内容:
To do calculations with dates, it is also very easy. Probably the best improvement compared to the current situation with Java < 1.8:
用日期进行计算也很容易。与 Java < 1.8 的当前情况相比,可能是最好的改进:
Period p = Period.of(2, HOURS);
LocalTime time = LocalTime.now();
LocalTime newTime = time.plus(p); // or time.plus(5, HOURS); or time.plusHours(5);
I don't clearly see the advantage prior to Versions < 1.8.
我没有清楚地看到版本 < 1.8 之前的优势。
Maybe someone can give me an example? Atm i am asking myself, where the improvement of the new date & time API comes from.
也许有人可以给我举个例子?Atm 我在问自己,新日期和时间 API 的改进来自哪里。
回答by assylias
With Java < 8, you would need to write something like:
使用 Java < 8,您需要编写如下内容:
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR, cal.get(Calendar.HOUR) + 2);
vs. with Java 8:
与 Java 8 对比:
LocalTime now = LocalTime.now();
LocalTime later = now.plus(2, HOURS);
The improvements are essentially on
改进主要在于
- readability:
Calendar.getInstance()
is not very well named: it is hard to tell whichinstance you are getting without reading the Javadoc.LocalTime.now()
is quite self-describing: you get a time and it is now.- To offset a date, you call an offsetting method (
plus
) whereas with the Calendar API, you have to manually change the fields of the object (in this example, the hour) which is error prone.
- ease of use (see for example the table towards the bottom of this pagefor a comparison):
- the Calendar API is complicated to use because it mixes concepts, such as a simple date (June 26th 2015) and an instant in time (June 26th 2015 at 10am UTC) - there isn't a class for the former concept
- The new Time API has a clear separation between the various date/time concepts
- safety:
- The Calendar API is not safe: nothing prevents you from writing
cal.set(123, 2)
which would throw a not-so-helpfulArrayOutOfBoundsException
. The new API uses enums which solves that problem. - The new API uses immutable objects, which makes it thread safe.
- The Calendar API is not safe: nothing prevents you from writing
- 可读性:
Calendar.getInstance()
不是很好命名:如果不阅读 Javadoc,很难判断您获得的是哪个实例。LocalTime.now()
是很自我描述:你得到一个时间,它是现在。- 要偏移日期,您可以调用偏移方法 (
plus
),而使用 Calendar API,您必须手动更改对象的字段(在本例中为小时),这很容易出错。
- 易用性(例如,请参见本页底部的表格进行比较):
- Calendar API 使用起来很复杂,因为它混合了一些概念,例如简单的日期(2015 年 6 月 26 日)和时间瞬间(UTC 时间 2015 年 6 月 26 日上午 10 点)- 前一个概念没有类
- 新的 Time API 明确区分了各种日期/时间概念
- 安全:
- 日历 API 并不安全:没有什么可以阻止您编写
cal.set(123, 2)
会抛出不太有用的ArrayOutOfBoundsException
. 新的 API 使用枚举来解决这个问题。 - 新 API 使用不可变对象,这使其线程安全。
- 日历 API 并不安全:没有什么可以阻止您编写
Overall, the new API is significantly inspired from jodatime which has been the preferred Java Date API for quite some time now. You can also read this detailed comparison of Java (<1.8) date vs. JodaTime(most of it should apply to the Java 8 Date API).
总体而言,新 API 的灵感来自 jodatime,它在很长一段时间内一直是首选的 Java Date API。您还可以阅读Java (<1.8) 日期与 JodaTime 的详细比较(其中大部分应该适用于 Java 8 Date API)。
回答by Hubert Schumacher
Advantages of the new date/time API
新日期/时间 API 的优点
- The notions of date (LocalDate), time (LocalTime), date&time (LocalDateTime), instant (Instant) are much clearer and correspond to their meaning in common language.
- Intializing date/time to a specific value got easier (method 'of', e.g. LocalDate.of(2016,1,1))
- Dealing with leap years (method isLeapYear) got trivial
- Adding/subtracting dates got very easy. E.g. to reference today in 10 years: LocalDate.now().plusYears(10)
- Month numbering offset adapted to intuition. 'January' is '1' now and not '0' as before
- 日期(LocalDate)、时间(LocalTime)、日期和时间(LocalDateTime)、即时(Instant)的概念更加清晰,对应于它们在通用语言中的含义。
- 将日期/时间初始化为特定值变得更容易(方法“of”,例如 LocalDate.of(2016,1,1))
- 处理闰年(方法是LeapYear)变得微不足道
- 添加/减去日期变得非常容易。例如参考 10 年后的今天:LocalDate.now().plusYears(10)
- 适应直觉的月份编号偏移量。'January' 现在是 '1' 而不是像以前一样的 '0'
Disadvantages
缺点
- Not yet supported by JPA2
- Not yet supported by JSF2
- JPA2 尚不支持
- JSF2 尚不支持