Joda时间,期间到总millis [英] Joda time, Period to total millis

查看:145
本文介绍了Joda时间,期间到总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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆