Note that there are some explanatory texts on larger screens.

plurals
  1. POMy PeriodFormatter is not behaving as I expect - what have I done wrong?
    primarykey
    data
    text
    <p>I'm having trouble using Joda Time's PeriodFormatter. I want one to report days, hours, minutes and seconds but my attempt seems to be wrapping around weeks. What should I be doing differently?</p> <pre><code>import org.joda.time.DateTime; import org.joda.time.Period; import org.joda.time.format.PeriodFormatter; import org.joda.time.format.PeriodFormatterBuilder; public class Problems { public static void main(String[] args) { PeriodFormatter formatter = new PeriodFormatterBuilder() .printZeroNever() .appendDays() .appendSuffix(" day", " days") .appendSeparator(", ") .appendHours() .appendSuffix(" hour", " hours") .appendSeparator(", ") .appendMinutes() .appendSuffix(" minute", " minutes") .appendSeparator(", ") .appendSeconds() .appendSuffix(" second", " seconds") .toFormatter(); DateTime now = new DateTime(); DateTime justUnderAWeekAgo = now.minusDays(7).plusMinutes(1); DateTime justOverAWeekAgo = now.minusDays(7).minusMinutes(1); System.out.println(now); System.out.println(justUnderAWeekAgo); System.out.println(justOverAWeekAgo); // I am happy with the following: System.out.println( formatter.print(new Period(justUnderAWeekAgo, now))); // But not with this (outputs "1 minute" but I want "7 days, 1 minute"): System.out.println( formatter.print(new Period(justOverAWeekAgo, now))); } } </code></pre> <p><strong>EDIT:</strong> I guess I can see why this isn't working - i.e. that the formatter just prints the Period's various values, and since Periods store a value for weeks, the value for days on my problem period is indeed 0. But I still need a good way of doing this...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload