Java – my periodformatter didn’t behave as I expected – what did I do wrong?

I'm having trouble using joda time's periodformatter I want someone to report days, hours, minutes and seconds, but my attempt seems to be in a few weeks What should I do?

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

Edit: I think I can see why this doesn't work - that is, the formatter only prints various values of period, and since periods stores a value for several weeks, the number of days during my question period is really 0 But I still need a good way to do this

Solution

In your case, the problem is that you do not require your periodformatter to display weeks

Two possibilities:

Solution 1: Show weeks:

PeriodFormatter formatter = new PeriodFormatterBuilder()
        .printZeroNever()
        .appendWeeks()
        .appendSuffix(" week"," weeks")
        .appendSeparator(",")
        .appendDays()
        .appendSuffix(" day"," days")
        .appendSeparator(",")
        .appendHours()
        .appendSuffix(" hour"," hours")
        .appendSeparator(",")
        .appendMinutes()
        .appendSuffix(" minute"," minutes")
        .appendSeparator(",")
        .appendSeconds()
        .appendSuffix(" second"," seconds")
        .toFormatter();

The second output in your example would be:

1 week,1 minute

Solution 2: only the date is displayed, so you must use periodtype yearMonthDayTime():

new Period(justUndeAWeekAgo,Now,PeriodType.yearMonthDayTime());

With the second solution, you can keep the periodformatter in its current state The second output in your example would be:

7 days,1 minute
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>