在java中对日期的类,我应该在Java 8中使用哪个日期类?

There is a whole set of date's classes in Java 8:

java.time.LocalDateTime;

java.time.ZonedDateTime;

java.time.Instant;

java.time.OffsetDateTime;

java.sql.Timestamp;

java.util.Date.

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:

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).

My questions:

Which aspects should I bear in mind in order to choose the optimal format among these four options from the performance & maintainability points of view?

Is there any reason why I should avoid some of these date classes?

解决方案

Each one of the Date classes are for specific purposes:

If you want to use your Date in an SQL/JDBC context, use the 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.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 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.

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.

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).

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();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值