本文介绍了Joda时间,期间到总millis的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Period对象实例中获取毫秒数( millis字段)。我尝试了多次转换,因为我找不到任何方法可以轻松提供。



有没有人需要它并设法检索它?



(我需要这个补丁,找出一个负期;负毫秒=负期。)

解决方案

你无法直接从期间获得毫秒数,因为像月份和年份这样的字段在毫秒数方面是可变的。



为了使这项工作,您需要提供一个基线瞬间, Period 可以计算实际的毫秒持续时间。例如, Period.toDurationFrom Period.toDurationTo 方法采用这样的基线瞬间,并计算持续时间对象,然后您可以获得毫秒数。



说:

所以你需要选择一个合适的基线瞬间为您的申请。


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:

So you need to pick an appropriate baseline instant for your application.

这篇关于Joda时间,期间到总millis的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 03:05