我们是否在Java中使用TimeSpan类

时间:2021-11-26 22:26:39

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.

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

From this TimeSpan we 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 TimeSpan we 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.

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

4 个解决方案

#1


24  

Interval from JodaTime will do..

来自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.

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

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.

间隔也可以转换为ReadablePeriod。这表示以年和日等字段表示的起点和终点之间的差异。

Interval is thread-safe and immutable.

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

#2


24  

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. Duration which indicates a seconds or nanoseconds length of a timespan.
  2. 持续时间,表示时间跨度的秒或纳秒长度。
  3. Period which indicates a more user-friendly difference, stored as 'x years and y months etc'.
  4. 表示更加用户友好的差异的期间,存储为“x年和y个月等”。

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

可以在Java教程中找到它们之间差异的详细说明

#3


3  

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

如果您使用的是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)

#4


2  

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类非常相似。此外,还有一些单元测试包含许多如何使用它的示例。

#1


24  

Interval from JodaTime will do..

来自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.

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

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.

间隔也可以转换为ReadablePeriod。这表示以年和日等字段表示的起点和终点之间的差异。

Interval is thread-safe and immutable.

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

#2


24  

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. Duration which indicates a seconds or nanoseconds length of a timespan.
  2. 持续时间,表示时间跨度的秒或纳秒长度。
  3. Period which indicates a more user-friendly difference, stored as 'x years and y months etc'.
  4. 表示更加用户友好的差异的期间,存储为“x年和y个月等”。

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

可以在Java教程中找到它们之间差异的详细说明

#3


3  

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

如果您使用的是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)

#4


2  

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类非常相似。此外,还有一些单元测试包含许多如何使用它的示例。