周期、间隔和持续时间的区别是什么?

时间:2021-09-06 21:30:49

In Joda-Time 2, what is the difference between the three kinds of time spans:

在Joda-Time 2中,三种时间跨度的区别是什么?

  • Period
  • Interval
  • 时间间隔
  • Duration

    持续时间

    1. Why do we need three classes?

      为什么我们需要三节课?

    2. Which one performs better?

      执行哪一个更好?

    3. Why is dividing a Period or Duration or Interval instance not implemented? E.g. p = p.divideBy(2);

      为什么没有实现分割周期、持续时间或间隔实例?如p = p.divideBy(2);

2 个解决方案

#1


229  

3 classes are needed because they represent different concepts so it is a matter of picking the appropriate one for the job rather than of relative performance. From the documentation with comments added by me in italics:

3个类是需要的,因为它们代表不同的概念,所以需要为工作选择合适的类,而不是相对的性能。从我用斜体字加上评论的文件中:


An interval in Joda-Time represents an interval of time from one millisecond instant to another instant. Both instants are fully specified instants in the datetime continuum, complete with time zone. Specific times are defined e.g. this might be the interval between 20:00:00GMT yesterday and 09:00:00GMT this morning.

在Joda-Time中的时间间隔表示从1毫秒到另一个瞬间的间隔时间。两个instants都是datetime continuum中的完全指定的instants,带有时区。具体时间是确定的,例如:这可能是昨天20:00:00GMT到今天上午09:00:00GMT之间的间隔。

A duration in Joda-Time represents a duration of time measured in milliseconds. The duration is often obtained from an interval. i.e. we can subtract start from end of an interval to derive a duration

在Joda-Time中的持续时间表示以毫秒为单位的时间。持续时间通常是从间隔中获得的。也就是说,我们可以从区间的末尾减去开始时间。

A period in Joda-Time represents a period of time defined in terms of fields, for example, 3 years 5 months 2 days and 7 hours. This differs from a duration in that it is inexact in terms of milliseconds. A period can only be resolved to an exact number of milliseconds by specifying the instant (including chronology and time zone) it is relative to. e.g. consider the period of 1 year, if we add this to January 1st we will always arrive at the next January 1st but the duration will depend on whether the intervening year is a leap year or not. Similarly if we add 1 month to the 1st of a month then we will arrive at the 1st of the next month but the duration (in milliseconds) will vary based on the month in question

在Joda-Time中的一段时间表示一段时间,以字段定义,例如,3年5个月2天7小时。这与持续时间的不同之处在于,它在毫秒数上并不精确。一个周期只能通过指定它相对的瞬间(包括时间和时区)来解析为精确的毫秒数。例如,考虑1年的时间段,如果我们把这个时间加到1月1日,我们总是会在下一个1月1日到达,但是时间的长短将取决于间隔年是否是闰年。同样的,如果我们把1个月加到1个月的第1个月,那么我们将在下个月的第1个月到达,但是持续时间(以毫秒为单位)将根据所讨论的月份而变化


For question 3, A specific method to divide a duration is not really needed because we can always get the number of milliseconds from the duration as a long (using getMillis()), divide it and construct a new duration (using new Duration(long duration)).

对于问题3,不需要使用特定的方法来划分持续时间,因为我们总是可以从持续时间中获取毫秒数(使用getMillis()),分割它并构造一个新的持续时间(使用新的持续时间(长持续时间))。

Dividing a period doesn't really have a real meaning based on the definition of a period above. e.g. what is half a month? (its length would depend on which month).

根据上面一段时期的定义,划分一段时间并没有真正意义上的意义。什么是半个月?(它的长度取决于哪个月)。

#2


84  

To add to mikej's answer:

补充一下mikej的回答:

A Joda-Time duration is a "physical" time interval; eg:

Joda-Time持续时间是“物理”时间间隔;例如:

12000 milliseconds <-- this is a duration

12000毫秒<,这是一个持续时间。

A Joda-Time interval is actually a pair of instants (start instant - end instant). An instant is, again, a "physical" concept, a point in the timeline. Eg (just a possible notation):

一个Joda-Time间隔实际上是一对instants (start instant - end instant)。瞬间又是一个“物理”概念,时间轴中的一个点。(可能的记号):

(2010/3/3 19:00:00.000 UTC ; 2010/3/3 20:00:00.000 UTC) <-- this is an interval

(2010/3/3 19:00:00.000 UTC;<-- -这是一个间隔

An interval, then, can be converted to a duration, but not the reverse.

因此,可以将间隔转换为持续时间,但不能转换为相反的时间。

Consider these two intervals:

考虑这两个时间间隔:

I1=(2010/3/3 19:00:00.000 UTC ; 2010/3/3 20:00:00.000 UTC)

I1 =(2010/3/3 19:00:00.000 UTC;2010/3/3 20:00:00.000 UTC)

I2=(2010/3/3 21:00:00.000 UTC ; 2010/3/3 22:00:00.000 UTC)

I2 =(2010/3/3 21:00:00.000 UTC;2010/3/3 22:00:00.000 UTC)

As intervals, I1 and I2 are different, because the end-points are different; but if I convert them to durations, I get the same thing: 3600000 milliseconds.

作为区间,I1和I2是不同的,因为端点不同;但是如果我把它们转换成持续时间,得到的结果是一样的:3600000毫秒。

(Math analogy: the intervals [10,12] and [95,97] are different intervals, but they have the same length: "interval length" maps to duration).

(数学类比:间隔[10,12]和[95,97]是不同的间隔,但它们的长度相同:“间隔长度”映射到持续时间)。

Finally, a period is a lapse of "civil time", expressed as a number of months, days, hours, etc. It does not -by itself- represent a "physical" interval, hence it can't be directly converted to a duration (months have variable lengths...).

最后,一个周期是“民用时间”的间隔,表示为几个月、天数、小时等。它本身并不代表“物理”的间隔,因此不能直接转换为持续时间(月有可变长度……)。

This answers question 3: you can only divide by two a physical time (a duration).

这就回答了问题3:物理时间(持续时间)只能除以2。

#1


229  

3 classes are needed because they represent different concepts so it is a matter of picking the appropriate one for the job rather than of relative performance. From the documentation with comments added by me in italics:

3个类是需要的,因为它们代表不同的概念,所以需要为工作选择合适的类,而不是相对的性能。从我用斜体字加上评论的文件中:


An interval in Joda-Time represents an interval of time from one millisecond instant to another instant. Both instants are fully specified instants in the datetime continuum, complete with time zone. Specific times are defined e.g. this might be the interval between 20:00:00GMT yesterday and 09:00:00GMT this morning.

在Joda-Time中的时间间隔表示从1毫秒到另一个瞬间的间隔时间。两个instants都是datetime continuum中的完全指定的instants,带有时区。具体时间是确定的,例如:这可能是昨天20:00:00GMT到今天上午09:00:00GMT之间的间隔。

A duration in Joda-Time represents a duration of time measured in milliseconds. The duration is often obtained from an interval. i.e. we can subtract start from end of an interval to derive a duration

在Joda-Time中的持续时间表示以毫秒为单位的时间。持续时间通常是从间隔中获得的。也就是说,我们可以从区间的末尾减去开始时间。

A period in Joda-Time represents a period of time defined in terms of fields, for example, 3 years 5 months 2 days and 7 hours. This differs from a duration in that it is inexact in terms of milliseconds. A period can only be resolved to an exact number of milliseconds by specifying the instant (including chronology and time zone) it is relative to. e.g. consider the period of 1 year, if we add this to January 1st we will always arrive at the next January 1st but the duration will depend on whether the intervening year is a leap year or not. Similarly if we add 1 month to the 1st of a month then we will arrive at the 1st of the next month but the duration (in milliseconds) will vary based on the month in question

在Joda-Time中的一段时间表示一段时间,以字段定义,例如,3年5个月2天7小时。这与持续时间的不同之处在于,它在毫秒数上并不精确。一个周期只能通过指定它相对的瞬间(包括时间和时区)来解析为精确的毫秒数。例如,考虑1年的时间段,如果我们把这个时间加到1月1日,我们总是会在下一个1月1日到达,但是时间的长短将取决于间隔年是否是闰年。同样的,如果我们把1个月加到1个月的第1个月,那么我们将在下个月的第1个月到达,但是持续时间(以毫秒为单位)将根据所讨论的月份而变化


For question 3, A specific method to divide a duration is not really needed because we can always get the number of milliseconds from the duration as a long (using getMillis()), divide it and construct a new duration (using new Duration(long duration)).

对于问题3,不需要使用特定的方法来划分持续时间,因为我们总是可以从持续时间中获取毫秒数(使用getMillis()),分割它并构造一个新的持续时间(使用新的持续时间(长持续时间))。

Dividing a period doesn't really have a real meaning based on the definition of a period above. e.g. what is half a month? (its length would depend on which month).

根据上面一段时期的定义,划分一段时间并没有真正意义上的意义。什么是半个月?(它的长度取决于哪个月)。

#2


84  

To add to mikej's answer:

补充一下mikej的回答:

A Joda-Time duration is a "physical" time interval; eg:

Joda-Time持续时间是“物理”时间间隔;例如:

12000 milliseconds <-- this is a duration

12000毫秒<,这是一个持续时间。

A Joda-Time interval is actually a pair of instants (start instant - end instant). An instant is, again, a "physical" concept, a point in the timeline. Eg (just a possible notation):

一个Joda-Time间隔实际上是一对instants (start instant - end instant)。瞬间又是一个“物理”概念,时间轴中的一个点。(可能的记号):

(2010/3/3 19:00:00.000 UTC ; 2010/3/3 20:00:00.000 UTC) <-- this is an interval

(2010/3/3 19:00:00.000 UTC;<-- -这是一个间隔

An interval, then, can be converted to a duration, but not the reverse.

因此,可以将间隔转换为持续时间,但不能转换为相反的时间。

Consider these two intervals:

考虑这两个时间间隔:

I1=(2010/3/3 19:00:00.000 UTC ; 2010/3/3 20:00:00.000 UTC)

I1 =(2010/3/3 19:00:00.000 UTC;2010/3/3 20:00:00.000 UTC)

I2=(2010/3/3 21:00:00.000 UTC ; 2010/3/3 22:00:00.000 UTC)

I2 =(2010/3/3 21:00:00.000 UTC;2010/3/3 22:00:00.000 UTC)

As intervals, I1 and I2 are different, because the end-points are different; but if I convert them to durations, I get the same thing: 3600000 milliseconds.

作为区间,I1和I2是不同的,因为端点不同;但是如果我把它们转换成持续时间,得到的结果是一样的:3600000毫秒。

(Math analogy: the intervals [10,12] and [95,97] are different intervals, but they have the same length: "interval length" maps to duration).

(数学类比:间隔[10,12]和[95,97]是不同的间隔,但它们的长度相同:“间隔长度”映射到持续时间)。

Finally, a period is a lapse of "civil time", expressed as a number of months, days, hours, etc. It does not -by itself- represent a "physical" interval, hence it can't be directly converted to a duration (months have variable lengths...).

最后,一个周期是“民用时间”的间隔,表示为几个月、天数、小时等。它本身并不代表“物理”的间隔,因此不能直接转换为持续时间(月有可变长度……)。

This answers question 3: you can only divide by two a physical time (a duration).

这就回答了问题3:物理时间(持续时间)只能除以2。