Joda时间,期间到总millis [英] Joda time, Period to total millis
问题描述
我正在尝试从Period对象实例中获取总毫秒数(不 millis字段)。我尝试了多次转换,因为我找不到任何方法可以轻松提供。
有没有人需要它并设法检索它?
(我需要这个补丁,找出一个负期;负毫秒=负期。)
你无法直接从期间
获得毫秒数,因为像月份和年份这样的字段在毫秒数方面是可变的。
为了使这项工作,您需要提供一个基线瞬间, Period
可以计算实际的毫秒持续时间。例如, Period.toDurationFrom
和 Period.toDurationTo
方法采用这样的基线瞬间,并计算持续时间
对象,然后您可以获得毫秒数。
toDurationFrom 的ableInstant%29> Javadoc 说:
获取相对于开始时刻,此时段的总毫秒持续时间。
此方法将时间段添加到指定的时刻以计算持续时间。
必须提供瞬间,因为时间段的持续时间会有所不同。例如,由于长度不同的月份,1个月的时间段可以在28到31天之间变化,以毫秒为单位。同样,一天的夏令时可能会有所不同,通常在23到25小时之间。
所以你需要选择一个合适的基线瞬间为您的申请。
I'm trying to get the total amount of Milliseconds (not the millis field) from the Period object instance. I've tried multiple conversions, as I couldn't find any method easily giving it.
Has anyone ever needed that and managed to retrieve it ?
(I need this for my patch, to figure out a negative period; negative millis = negative period.)
You can't get the millis directly from a Period
, since fields like months and years are variable in terms of milliseconds.
In order to make this work, you need to supply a "baseline" instant from which Period
can calculate that actual millisecond duration.
For example, the Period.toDurationFrom
and Period.toDurationTo
methods take such a baseline instant, and calculate a Duration
object, which you can then obtain the millis.
The Javadoc for toDurationFrom
says:
Gets the total millisecond duration of this period relative to a start instant. This method adds the period to the specified instant in order to calculate the duration.
An instant must be supplied as the duration of a period varies. For example, a period of 1 month could vary between the equivalent of 28 and 31 days in milliseconds due to different length months. Similarly, a day can vary at Daylight Savings cutover, typically between 23 and 25 hours.
So you need to pick an appropriate baseline instant for your application.
这篇关于Joda时间,期间到总millis的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!