我们在 Java 中有一个 TimeSpan 类吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6581605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:57:24  来源:igfitidea点击:

Do we have a TimeSpan sort of class in Java

javatimespanjava.util.date

提问by Talha Ahmed Khan

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

我只是想知道是否需要 TimeSpan,java.util以便我可以定义这两个时间之间有多少小时、分钟和秒。

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

由此TimeSpan我们可以在两次之间有一个时间间隔。喜欢

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 TimeSpanwe can use it in SimpleDateFormat.

有了这个,TimeSpan我们可以在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.

我不确定这是否已经在 J​​ava 中实现,但我还没有发现。

采纳答案by Jigar Joshi

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

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

Durationor Period

Duration或者 Period

Intervalfrom JodaTime will do..

Interval从 JodaTime 会做..

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.

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

间隔具有固定的毫秒持续时间。这是开始和结束时刻之间的差异。持续时间由 ReadableDuration 单独表示。因此,间隔不具有可比性。要比较两个间隔的长度,您应该比较它们的持续时间。

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

Interval 是线程安全且不可变的。

回答by ashokgelal

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

如果您正在寻找替代的轻量级版本,请查看我编写的用于在我自己的 Android 应用程序中使用的这个库。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.

抱歉,我没有关于其用法的任何文档,但它与对应的 .net Timespan 类非常相似。此外,还有一些单元测试,其中包含许多有关如何使用它的示例。

回答by Anton Kaiser

If you're on on Java 8 (or higher) or simply don't want to import JodaTime (the Author of JodaTime himself suggest migratingto java.time): Java 8 offers that functionality with Periods, see here for a tutorial: https://docs.oracle.com/javase/tutorial/datetime/iso/period.html

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

Let me quote the Oracle tutorial here:

让我在这里引用 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)");

The code produces output similar to the following:

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

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

回答by Thirler

In Java 8 a proper time library has been added to the standard API (this is based heavily on JodaTime).

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

In it there are two classes that you can use to indicate a period:

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

  1. Durationwhich indicates a seconds or nanoseconds length of a timespan.
  2. Periodwhich indicates a more user-friendly difference, stored as 'x years and y months etc'.
  1. Duration表示时间跨度的秒或纳秒长度。
  2. Period这表明更用户友好的差异,存储为“x 年和 y 个月等”。

A detailed explanation of the difference between them can be found in the Java tutorial

它们之间的区别的详细解释可以在Java教程中找到