Java 中有 TimeSpan 类吗

发布于 2024-11-18 10:02:53 字数 610 浏览 3 评论 0原文

我只是想知道 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 );

我不确定这是否已经在 J​​ava 中实现但尚未被我发现。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

深白境迁sunset 2024-11-25 10:02:53

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

持续时间周期

Interval 就可以了..

时间间隔代表一段时间
两个瞬间之间的时间。间隔
包括开始时刻和
排除结尾。结束瞬间
总是大于或等于
立即开始。

间隔有固定的毫秒数
期间。这就是区别
开始时刻和结束时刻之间。
持续时间单独表示
通过 ReadableDuration。因此,
间隔不具有可比性。到
比较两个间隔的长度,
您应该比较它们的持续时间。

间隔也可以转换为
可读期。这代表了
开始和结束之间的差异
点在领域方面,例如
年复一年。

间隔是线程安全且不可变的。

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

Duration or Period

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.

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.

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.

Interval is thread-safe and immutable.

ぶ宁プ宁ぶ 2024-11-25 10:02:53

在 Java 8 中,标准 API 中添加了一个适当的时间库(这在很大程度上基于 JodaTime)。

其中有两个类可用于指示句点:

  1. Duration 表示时间跨度的秒或纳秒长度。
  2. 期间 其中表示更加用户友好的差异,存储为“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:

  1. Duration which indicates a seconds or nanoseconds length of a timespan.
  2. 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

我们的影子 2024-11-25 10:02:53

如果您使用的是 Java 8(或更高版本)或者只是不想导入 JodaTime(JodaTime 的作者本人 建议迁移 到 java.time):Java 8 通过 句点,请参阅此处的教程:https://docs.oracle.com/javase/tutorial/datetime/iso/period.html

让我在这里引用 Oracle 教程:

LocalDate today = LocalDate.now();
LocalDate birthday = LocalDate.of(1960, Month.JANUARY, 1);

Period p = Period.between(birthday, today);
long p2 = ChronoUnit.DAYS.between(birthday, today);
System.out.println("You are " + p.getYears() + " years, " + p.getMonths() +
                   " months, and " + p.getDays() +
                   " days old. (" + p2 + " days total)");

代码生成类似于以下内容的输出:

You are 53 years, 4 months, and 29 days old. (19508 days total)

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:

LocalDate today = LocalDate.now();
LocalDate birthday = LocalDate.of(1960, Month.JANUARY, 1);

Period p = Period.between(birthday, today);
long p2 = ChronoUnit.DAYS.between(birthday, today);
System.out.println("You are " + p.getYears() + " years, " + p.getMonths() +
                   " months, and " + p.getDays() +
                   " days old. (" + p2 + " days total)");

The code produces output similar to the following:

You are 53 years, 4 months, and 29 days old. (19508 days total)
错々过的事 2024-11-25 10:02:53

如果您正在寻找替代的更轻版本,请查看我编写并在我自己的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文