[英]Which date class should I use in Java 8?
There is a whole set of date's classes in Java 8: Java 8中有一整套日期类:
java.time.LocalDateTime
; java.time.LocalDateTime
; java.time.ZonedDateTime
; java.time.ZonedDateTime
; java.time.Instant
; java.time.Instant
; java.time.OffsetDateTime
; java.time.OffsetDateTime
; java.sql.Timestamp
; java.sql.Timestamp
; java.util.Date
. java.util.Date
。 I already passed over their JavaDocs and paid attention that all these classes contain all the methods I need. 我已经传递了他们的JavaDocs并注意到所有这些类都包含我需要的所有方法。 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.
但我想有一些理由说明为什么有6个单独的类,每个类都专门用于特定目的。
Technical information & requirements: 技术信息和要求:
String
, which is converted to one of these date formats. String
,它被转换为这些日期格式之一。 My questions: 我的问题:
Each one of the Date
classes are for specific purposes: 每个
Date
类都用于特定目的:
If you want to use your Date in an SQL
/ JDBC
context, use the java.sql.Timestamp
. 如果要在
SQL
/ JDBC
上下文中使用Date,请使用java.sql.Timestamp
。
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.util.Date
是旧的Java API,它不是线程安全的,你可以很难处理时间分区,最重要的是,它的设计很糟糕:一个简单的统一是几个月从1开始,而几天从0开始。
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.LocalDateTime
是一个不可变的日期时间对象,它表示一个日期时间,通常被视为年 - 月 - 日 - 时 - 分 - 秒,您需要完全正确。
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. java.time.ZonedDateTime
类存储所有日期和时间字段,因此您可以使用它来处理以下值: 27th January 1990 at 15:40.30.123123123 +02:00
在欧洲/巴黎时区。
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). 为了完成您的任务,
ZonedDateTime
类处理从LocalDateTime
的本地时间线到Instant
的即时时间线的转换(该时间线模拟时间线上的单个瞬时点)。 The difference between the two time-lines, represented by a ZoneOffset
, is the offset from UTC/Greenwich. 由
ZoneOffset
表示的两个时间线之间的差异是与UTC / Greenwich的偏移。
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.time.Duration
是一个基于时间的时间量,例如'20 .5秒',和java.time.Period
,这是一个基于日期的时间量(如: 26年,2个月和2天)。
To get max and min dates, you can use the Java 8 lambdas in something like: 要获取最大和最小日期,您可以使用Java 8 lambdas,例如:
Date maxDate = list.stream().map(yourInstance -> yourInstance.date).max(Date::compareTo).get();
Date minDate = list.stream().map(yourInstance -> yourInstance.date).min(Date::compareTo).get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.