Joda period can't print Year Date?

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Joda period can't print Year Date?

casttree
This post has NOT been accepted by the mailing list yet.
I added the PeriodType.yearDayTime()  in the period constructor,  but still get 26 hours, instead of 1 day 2 hours for output  in Junit.

org.junit.ComparisonFailure: expected:<'[1 day and 2] hours'> but was:<'[26] hours'>


Do I miss anything ? Any suggestions for this issue?

thank you!



import org.joda.time.Period;
import org.joda.time.PeriodType;
import org.joda.time.format.PeriodFormatter;
import org.joda.time.format.PeriodFormatterBuilder;

/**
 * Utils class for the date and time formatting
 */
public class TimeDurationFormatUtils {

    private TimeDurationFormatUtils() {

    }

    /**
     * Formats the duration as time format
     * @param duration
     * @return String
     */
    public static String convertLongToTimeFormat(long duration) {

        Period period = new Period(duration * 1000, PeriodType.yearDayTime());
         /*** duration = 3600 * 26 ;
          *  and get days = 0;  and hours = 26
         ****/
         int days =  period.getDays();
         int hours = period.getHours();

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

      /**** here result = 26 hours, but not 1 day 2 hous ***/

        String result = formatter.print(period);
     
        return formatter.print(period);
    }

}


add completed junit test code

import static org.fest.assertions.Assertions.*;

import org.junit.Before;
import org.junit.Test;

public class TimeDurationFormatUtilsTest {

    private long durationHourMinute;
    private long durationMinuteSecond;
    private long durationHour;
    private long durationMinute;
    private long durationSecond;
    private long durationHourMinuteSecond;
    private long durationHourSecond;
    private long durationDayHour;

    @Before
    public void setup() {
        durationSecond = 30L;
        durationMinute = 60 * 3;
        durationHour = 3600L;
        durationMinuteSecond = 80L;
        durationHourMinute = 60 * 2 + 3600;
        durationHourSecond = 15 + 3600;
        durationHourMinuteSecond = 15 + 60 * 2 + 3600;
        durationDayHour = 3600 * 26;
    }

    @Test
    public void testMatch() {

        assertThat(TimeDurationFormatUtils.convertLongToTimeFormat(durationSecond)).isEqualTo("30 seconds");
        assertThat(TimeDurationFormatUtils.convertLongToTimeFormat(durationMinute)).isEqualTo("3 minutes");
        assertThat(TimeDurationFormatUtils.convertLongToTimeFormat(durationHour)).isEqualTo("1 hour");
        assertThat(TimeDurationFormatUtils.convertLongToTimeFormat(durationMinuteSecond)).isEqualTo("1 minute 20 seconds");
        assertThat(TimeDurationFormatUtils.convertLongToTimeFormat(durationHourMinute)).isEqualTo("1 hour 2 minutes");
        assertThat(TimeDurationFormatUtils.convertLongToTimeFormat(durationHourSecond)).isEqualTo("1 hour 15 seconds");
        assertThat(TimeDurationFormatUtils.convertLongToTimeFormat(durationHourMinuteSecond)).isEqualTo("1 hour 2 minutes 15 seconds");

        assertThat(TimeDurationFormatUtils.convertLongToTimeFormat(durationDayHour)).isEqualTo("1 day and 2 hours");

    }

}
Loading...