Displaying “negative” time periods with Joda-Time PeriodFormatter

对着背影说爱祢 提交于 2020-01-14 17:16:51

问题



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));

The output of this is:

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

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

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

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

Thanks,
-Manuel


回答1:


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

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).




回答2:


I solve negative like this:

 Duration dToFormat = duration;
    if (duration.getStandardMinutes() < 0) {
        dToFormat = duration.minus(duration).minus(duration);
    }

    ...

    if (duration.getStandardMinutes() < 0) {
                return "-"+pf.print(p); // leading minus
            }

and all code here in my case:

public static String durationToHHHmmFormat(Duration duration, String separator){
        if ( duration == null ) {
            return null;
        }

        Duration dToFormat = duration;
        if (duration.getStandardMinutes() < 0) {
            dToFormat = duration.minus(duration).minus(duration);
        }

        Period p = dToFormat.toPeriod();
        PeriodFormatter pf = new PeriodFormatterBuilder()
            .printZeroAlways()
            .minimumPrintedDigits(2) // gives the '01'
            .appendHours()
            .appendSeparator(separator)
            .appendMinutes()
            .toFormatter();

        if (duration.getStandardMinutes() < 0) {
            return "-"+pf.print(p); // leading minus
        }
        return pf.print(p);
    }


来源:https://stackoverflow.com/questions/6387723/displaying-negative-time-periods-with-joda-time-periodformatter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!