2

I have a method which determines the period between two dateTime variables.

Period period = new Period(startTime, endTime);
PeriodFormatter runDurationFormatter = new PeriodFormatterBuilder().printZeroAlways().minimumPrintedDigits(2).appendDays().appendSeparator(":").appendHours().appendSeparator(":").appendMinutes().appendSeparator(":").appendSeconds().toFormatter();
return runDurationFormatter.print(period);

I would expect to see 00:01:00 for 1 minute, 23:00:00 for 23 hours, 30:00:00 for 30 hours, and 120:00:00 for 120 hours (5 days). I tried using

Period daystoHours = period.normalizedStandard(PeriodType.time());

But eclipse shows that normalizedStandard() method is undefined for type period.

1
  • The method Period.normalizedStandard() exists since Joda-Time-version v1.5. Make sure that a) you don't use a stone-old version and b) use the right imports (see answer of @Gordeev) May 17, 2016 at 21:09

1 Answer 1

2

Be sure you are using the Period class from org.joda.time package, not from java.time. Following example may help you.

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

import java.util.Calendar;
import java.util.GregorianCalendar;

public class Launcher
{
    public static void main(String[] args)
    {
        Calendar start = new GregorianCalendar(2016, 4, 12, 0, 0, 0);
        Calendar end = new GregorianCalendar(2016, 4, 17, 0, 0, 0);

        Period period = new Period(start.getTimeInMillis(), end.getTimeInMillis());

        PeriodFormatter runDurationFormatter = new PeriodFormatterBuilder().printZeroAlways()
            .minimumPrintedDigits(2)
            .appendHours().appendSeparator(":")    // <-- say formatter to emit hours
            .appendMinutes().appendSeparator(":")  // <-- say formatter to emit minutes
            .appendSeconds()                       // <-- say formatter to emit seconds
            .toFormatter();

        // here we are expecting the following result string 120:00:00
        System.out.println(
            runDurationFormatter.print(period.normalizedStandard(PeriodType.time()))
        );
    }
}
1
  • Thanks for your response but i am not able to use period.normalizedStandard(PeriodType.time()), it says normalizedStandard is undefined for type period.
    – Shubham
    May 17, 2016 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.