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