我应该在Java 8中使用哪个日期类? [英] Which date class should I use in Java 8?
问题描述
Java 8
中有一整套日期类:
-
java.time.LocalDateTime
; -
java.time.ZonedDateTime
; -
java.time.Instant
; -
java.time。 OffsetDateTime
; -
java.sql.Timestamp
; -
java.util.Date
。
java.time.LocalDateTime
;java.time.ZonedDateTime
;java.time.Instant
;java.time.OffsetDateTime
;java.sql.Timestamp
;java.util.Date
.
我已经通过了他们的JavaDocs并付了钱注意所有这些类都包含我需要的所有方法。因此,目前,我可以随机选择它们。但我想有一些理由说明为什么有6个单独的课程,每个课程都专门用于特定目的。
I already passed over their JavaDocs and paid attention that all these classes contain all the methods I need. Thus, for the moment, I can select them randomly. But I guess that there is some reason why there are 6 separate classes and each of them is dedicated to the specific purpose.
技术信息&要求:
Technical information & requirements:
- 输入在
String
中,转换为其中一个日期格式。 - 我不需要显示时区,但是当我比较两个日期时,能够正确比较纽约和巴黎的时间非常重要。
- 精确级别是秒,不需要使用毫秒。
- 所需操作:
- 查找最大/最小日期;
- 按日期排序对象;
- 计算日期和时间;时间段(两个日期之间的差异);
- 将对象插入
MongoDB
并按日期从数据库中检索它们(例如,之后的所有对象)具体日期)。
- The input is in
String
, which is converted to one of these date formats. - I don't need to display the time zones but when I compare two dates it's important to be capable to compare correctly the time in New York and in Paris.
- The precise level is seconds, there is no need to use milliseconds.
- The required operations:
- find max/min date;
- sort objects by date;
- calculate date & time period (difference between two dates);
- insert objects to
MongoDB
and retrieve them from a db by date (e.g. all objects after specific date).
我的问题:
- 我应该记住哪些方面,以便从性能和表现中选择这四个选项中的最佳格式。可维护性的观点?
- 我有什么理由可以避免这些日期类别吗?
推荐答案
每个 Date
类都用于特定目的:
Each one of the Date
classes are for specific purposes:
-
如果你想在
SQL中使用你的日期
/JDBC
上下文,使用java.sql.Timestamp
。
If you want to use your Date in an
SQL
/JDBC
context, use thejava.sql.Timestamp
.
java.util .Date
是旧的Java API,它不是线程安全的,你可以很难处理时间分区,最重要的是,它的设计很差:一个简单的统一是几个月从1开始天从0开始。
java.util.Date
is the old Java API, it is not thread safe, you can difficultly handle time zoning, and on the top of all, it is poorly designed: one simple uniformity is that months start from 1 while days start from 0.
java.time.LocalDateTime
是一个不可变的日期时间对象,表示日期时间,通常被视为年 - 月 - 日 - 时 - 分 - 秒,您需要完全正确。
java.time.LocalDateTime
is an immutable date-time object that represents a date-time, often viewed as year-month-day-hour-minute-second, which you need exactly.
java .time.ZonedDateTime
类存储所有日期和时间字段,因此您可以使用它来处理以下值:
1990年1月27日15:40.30.123123123 +02:00
在欧洲/巴黎时区。
java.time.ZonedDateTime
class stores all date and time fields, so you can use it to deal with values like:
27th January 1990 at 15:40.30.123123123 +02:00
in the Europe/Paris time-zone.
为完成您的任务, ZonedDateTime
类处理来自本地时间线 LocalDateTime
的转换到 Instant
的即时时间线(它模拟时间线上的一个瞬时点)。由 ZoneOffset
表示的两个时间线之间的差异是与UTC / Greenwich的偏移。
To do your task, the ZonedDateTime
class handles conversion from the local time-line of LocalDateTime
to the instant time-line of Instant
(which models a single instantaneous point on the time-line). The difference between the two time-lines, represented by a ZoneOffset
, is the offset from UTC/Greenwich.
要计算持续时间和周期:有 java.time.Duration
这是一个基于时间的时间量,例如'20 .5秒'和 java.time.Period
,这是一个基于日期的时间量(如:26年,2个月和2天)。
To calculate duration and period: there is the java.time.Duration
which is a time-based amount of time, such as '20.5 seconds', and java.time.Period
, which is a date-based amount of time (like: 26 years, 2 months and 2 days).
要获得最大和最小日期,您可以使用Java 8 lambdas,例如:
To get max and min dates, you can use the Java 8 lambdas in something like:
Date maxDate = list.stream().map(yourInstance -> yourInstance.date).max(Date::compareTo).get();
Date minDate = list.stream().map(yourInstance -> yourInstance.date).min(Date::compareTo).get();
这篇关于我应该在Java 8中使用哪个日期类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!