Java Joda-Time:Period、Interval 和 Duration 之间有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2653567/
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
Joda-Time: what's the difference between Period, Interval and Duration?
提问by Gerard
采纳答案by mikej
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 documentationwith comments added by me in italics:
需要 3 个类,因为它们代表不同的概念,因此选择适合工作的类而不是相对性能的问题。从我用斜体添加的注释的文档中:
An intervalin 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.
的间隔在约达时间表示时间从一个毫秒时刻到另一时刻的间隔。两个时刻都是日期时间连续体中完全指定的时刻,并带有时区。定义了特定时间,例如这可能是昨天 20:00:00GMT 和今天早上 09:00:00GMT 之间的时间间隔。
A durationin 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
甲持续时间在约达时间表示以毫秒计的时间的持续时间。持续时间通常是从一个间隔中获得的。即我们可以从间隔的末尾减去开始以得出持续时间
A periodin 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
甲期间在约达时间表示一段时间中的字段来定义,例如,3年5月2天及7小时。这与持续时间的不同之处在于它在毫秒方面是不精确的。一个周期只能通过指定它相对的时刻(包括时间顺序和时区)来解析为精确的毫秒数。例如,考虑 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,实际上并不需要使用特定的方法来划分持续时间,因为我们始终可以从持续时间中获取毫秒数作为 a long
(使用getMillis()
),将其划分并构造新的持续时间(使用new Duration(long duration)
)。
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).
根据上述期间的定义,划分期间并没有真正的意义。例如什么是半个月?(其长度取决于哪个月份)。
回答by leonbloy
To add to mikej's answer:
添加到mikej 的答案:
A Joda-Timedurationis a "physical" time interval; eg:
甲约达时间的持续时间是“物理”的时间间隔; 例如:
12000 milliseconds
<-- this is a duration
12000 milliseconds
<-- 这是一个持续时间
A Joda-Time intervalis actually a pair of instants(start instant - end instant). An instantis, again, a "physical" concept, a point in the timeline. Eg (just a possible notation):
Joda-Time间隔实际上是一对瞬间(开始瞬间 - 结束瞬间)。一个瞬间是,再次,“物理”的概念,在时间线中的一个点。例如(只是一个可能的符号):
(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 ; 2010/3/3 20:00:00.000 UTC)
<-- 这是一个区间
An interval, then, can be converted to a duration, but not the reverse.
那么,一个interval可以转换为一个duration,但反过来不行。
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 milliseconds
.
(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 periodis 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。