我有两个变量:

int duration;
PeriodType periodType;


根据一些逻辑,我发现2个dateTimes的差,并且只想在最终的period中存储天,分钟或小时。

如何在运行时找到类型和相应持续时间的期间对象?

new Period(0, duration, periodType);


返回所有字段为0的期间

最佳答案

如果只想存储几天,几分钟或几小时

PeriodType.days();
PeriodType.minutes();
PeriodType.hours();


然后

final Period per = new Period().withField(periodType.getFieldType(0), duration);


是您所需要的。

10-06 06:13