我有一个方法可以确定两个dateTime变量之间的时间段。
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);
我希望00:01:00看1分钟,23:00:00看23小时,30:00:00看30小时,120:00:00看120小时(5天)。 我尝试使用
Period daystoHours = period.normalizedStandard(PeriodType.time());
但是eclipse显示normalizedStandard()方法未定义类型句点。
答案 0 :(得分:1)
请确保您使用org.joda.time
包中的Period类,而不是java.time
。以下示例可以帮助您。
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()))
);
}
}