Java 中有 TimeSpan 类吗
我只是想知道 java.util 中是否需要 TimeSpan ,以便我可以定义这两个时间之间有多少小时、分钟和秒。
从这个TimeSpan
我们可以得到两个时间之间的时间间隔。像
TimeSpan getTimeSpan( Date before, Date after ){...}
or
long timeSpan = System.currentTimeMillis();
// ... long job
timeSpan = System.currentTimeMillis() - timeSpan;
TimeSpan ts = new TimeSpan(timeSpan);
和这样的 TimeSpan
我们可以在 SimpleDateFormat
中使用它。
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
format.format( timsSpan );
我不确定这是否已经在 Java 中实现但尚未被我发现。
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.
From this TimeSpan
we can have a time interval between two times. like
TimeSpan getTimeSpan( Date before, Date after ){...}
or
long timeSpan = System.currentTimeMillis();
// ... long job
timeSpan = System.currentTimeMillis() - timeSpan;
TimeSpan ts = new TimeSpan(timeSpan);
and with this TimeSpan
we can use it in SimpleDateFormat
.
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
format.format( timsSpan );
I am not sure if this is already been implemented in Java but yet undiscovered by me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
SDK 中的 JDK 8 日期时间库得到了丰富,您可以使用
持续时间
或周期
Interval
就可以了..With JDK 8 date-time libraries in SDK has been enriched and you can use
Duration
orPeriod
Interval
from JodaTime will do..在 Java 8 中,标准 API 中添加了一个适当的时间库(这在很大程度上基于 JodaTime)。
其中有两个类可用于指示句点:
Duration
表示时间跨度的秒或纳秒长度。期间
其中表示更加用户友好的差异,存储为“x 年和 y 月等”。它们之间差异的详细说明可以在 Java 教程
In Java 8 a proper time library has been added to the standard API (this is based heavily on JodaTime).
In it there are two classes that you can use to indicate a period:
Duration
which indicates a seconds or nanoseconds length of a timespan.Period
which indicates a more user-friendly difference, stored as 'x years and y months etc'.A detailed explanation of the difference between them can be found in the Java tutorial
如果您使用的是 Java 8(或更高版本)或者只是不想导入 JodaTime(JodaTime 的作者本人 建议迁移 到 java.time):Java 8 通过 句点,请参阅此处的教程:https://docs.oracle.com/javase/tutorial/datetime/iso/period.html
让我在这里引用 Oracle 教程:
代码生成类似于以下内容的输出:
If you're on on Java 8 (or higher) or simply don't want to import JodaTime (the Author of JodaTime himself suggest migrating to java.time): Java 8 offers that functionality with Periods, see here for a tutorial: https://docs.oracle.com/javase/tutorial/datetime/iso/period.html
Let me quote the Oracle tutorial here:
The code produces output similar to the following:
如果您正在寻找替代的更轻版本,请查看我编写并在我自己的 Android 应用程序中使用的这个库。 https://github.com/ashokgelal/samaya
抱歉,我没有任何有关其用法的文档,但它与对应的 .net Timespan 类非常相似。此外,还有一些单元测试,其中包含许多有关如何使用它的示例。
If you are looking for an alternative lighter version, have a look at this library that I wrote to use in my own Android app. https://github.com/ashokgelal/samaya
Sorry, I don't have any documentation on its usage, but it is very similar to the counterpart .net Timespan class. Also, there are some unit tests which contains many examples on how to use it.