Java 如何创建具有可变PeriodType的jodatime时段
我有两个变量:Java 如何创建具有可变PeriodType的jodatime时段,java,jodatime,Java,Jodatime,我有两个变量: int duration; PeriodType periodType; 根据一些逻辑,我发现了2个日期时间的差异,只想在最后的时段中存储天、分钟或小时 如何创建一个period对象,在运行时可以找到该类型和相应的持续时间 new Period(0, duration, periodType); 返回一个包含所有字段0的句点在此,您使用的方法签名为 Period(long startInstant, long endInstant, PeriodType type) 我想,
int duration;
PeriodType periodType;
根据一些逻辑,我发现了2个日期时间的差异,只想在最后的时段中存储天、分钟或小时
如何创建一个period对象,在运行时可以找到该类型和相应的持续时间
new Period(0, duration, periodType);
返回一个包含所有字段0的句点在此,您使用的方法签名为
Period(long startInstant, long endInstant, PeriodType type)
我想,duration
的单位是毫秒(长)
你得用这个
Period(long duration, PeriodType type)
所以试试看
new Period(duration, periodType);
如果只想存储天、分钟或小时,它应该可以工作
PeriodType.days();
PeriodType.minutes();
PeriodType.hours();
然后
final Period per = new Period().withField(periodType.getFieldType(0), duration);
是您需要的。我最初尝试过,但预计持续时间为毫秒。因此,如果持续时间为10天,periodType为天,它将再次失败(link:,org.joda.time.periodType)您是否尝试过新期间(持续时间,null,periodType)
使用签名期间(ReadableDuration持续时间,ReadableInstant endInstant,periodType)
。它迫使你把“现在”作为结束时刻,但这是你的担忧吗?