显示“负"字样. Joda-Time PeriodFormatter的时间段 [英] Displaying "negative" time periods with Joda-Time PeriodFormatter

查看:84
本文介绍了显示“负"字样. Joda-Time PeriodFormatter的时间段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我在一个项目上使用了joda-time(1.6.2),而我正在做的一件事是获得预测时间与实际时间之间的差.有时这种差异是积极的,有时是消极的.虽然适当的方法可能是使用Duration而不是Period,但是使用PeriodFormatter来显示结果使我对PeriodFormatterBuilder类产生了疑问.例如:


I'm using joda-time (1.6.2) on a project and one of the things I'm doing is getting the difference between a predicted time and an actual time. Sometimes this difference is positive, sometimes negative. While the appropriate approach may be to use a Duration rather than a Period, using a PeriodFormatter to display the result led me a question about the PeriodFormatterBuilder class. As an example:

DateTime d1 = new DateTime(2011, 6, 17, 13, 13, 5, 0) ;
DateTime d2 = new DateTime(2011, 6, 17, 10, 17, 3, 0) ;

Period negativePeriod = new Period(d1, d2);
Period positivePeriod = new Period(d2, d1);

PeriodFormatter pf = new PeriodFormatterBuilder()
    .minimumPrintedDigits(2)
    .appendHours()
    .appendSuffix(":")
    .rejectSignedValues(true) // Does this do anything?
    .appendMinutes()
    .appendSuffix(":")
    .appendSeconds()
    .toFormatter();

System.out.printf("Negative Period: %s\n", pf.print(negativePeriod));
System.out.printf("Positive Period: %s\n", pf.print(positivePeriod));

此输出为:

Negative Period: -02:-56:-02
Positive Period: 02:56:02

我知道Period分别存储其日期和时间的每个部分,但是对我来说,用于构建Formatter.rejectSignedValues(true)方法的预期行为是仅显示-符号第一个元素如:

I understand that Period stores each component of its date and time separately, but to me, the expected behavior of the .rejectSignedValues(true) method for building a Formatter would be to only show the - sign for only the first element like:

Negative Period: -02:56:02

我是否误解了API,或者这是一个错误?乔达·斯蒂芬(JodaStephen)?有人吗?

Am I misunderstanding the API, or is this a bug? JodaStephen? Anyone?

显示我想要的东西的工作并不难,但是我只是对Builder方法感到好奇.

The work around to display what I want is not hard, but I'm just curious about the Builder approach.

谢谢,
-Manuel

Thanks,
-Manuel

推荐答案

rejectSignedValues的Javadoc表示Reject signed values when parsing the next and following appended fields,即.它只会影响解析,不会影响打印.

The Javadoc for rejectSignedValues says Reject signed values when parsing the next and following appended fields, ie. it only affects parsing, not printing.

您的建议可能会做一些有益的改进,但不能保证所有组成部分都是正面的,也可能是负面的.这也是一个有效期:P-6D8H(正负混合).

Your proposal might make a useful enhancement, but there is no guarantee that all components would be positive, or all negative. This is also a valid period: P-6D8H (a mixture of positive and negative).

这篇关于显示“负"字样. Joda-Time PeriodFormatter的时间段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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