我们是否在Java中使用TimeSpan类 [英] Do we have a TimeSpan sort of class in Java

查看:393
本文介绍了我们是否在Java中使用TimeSpan类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道在 java.util 中是否需要TimeSpan,以便我可以定义这两次之间的小时数,分钟数和秒数。

I was just wondering if there is a need of TimeSpan in java.util so that I can define how much hours,minutes and seconds are there in between these two times.

从这个 TimeSpan 我们可以有两次之间的时间间隔。喜欢

From this TimeSpan we can have a time interval between two times. like

TimeSpan getTimeSpan( Date before, Date after ){...}

long timeSpan = System.currentTimeMillis();
// ... long job
timeSpan = System.currentTimeMillis() - timeSpan;

TimeSpan ts = new TimeSpan(timeSpan);

使用此 TimeSpan 我们可以使用它在 SimpleDateFormat

and with this TimeSpan we can use it in SimpleDateFormat.

SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
format.format( timsSpan );

我不确定这是否已经在Java中实现但我尚未发现。

I am not sure if this is already been implemented in Java but yet undiscovered by me.

推荐答案

使用SDK中的JDK 8日期时间库进行了丰富,您可以使用

With JDK 8 date-time libraries in SDK has been enriched and you can use

持续时间 期间

Interval 将会...

Interval from JodaTime will do..


时间间隔表示两个时刻之间的
时间段。间隔
包括起始时刻和
不包括结尾。结束时刻
总是大于或等于
开始时刻。

A time interval represents a period of time between two instants. Intervals are inclusive of the start instant and exclusive of the end. The end instant is always greater than or equal to the start instant.

时间间隔有一个固定的毫秒
持续时间。这是开始和结束时刻之间的差额

持续时间由ReadableDuration单独表示
。因此,
的间隔不具有可比性。以
比较两个区间的长度,
你应该比较它们的持续时间。

Intervals have a fixed millisecond duration. This is the difference between the start and end instants. The duration is represented separately by ReadableDuration. As a result, intervals are not comparable. To compare the length of two intervals, you should compare their durations.

区间也可以转换为
ReadablePeriod 。这表示起始和结束
点之间的
差异,如
年和天等字段。

An interval can also be converted to a ReadablePeriod. This represents the difference between the start and end points in terms of fields such as years and days.

间隔是线程-safe和immutable。

Interval is thread-safe and immutable.

这篇关于我们是否在Java中使用TimeSpan类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆