我有一个关于减去Joda DateTimes的问题,我用句号来得到日期、小时、分钟和秒数。
输出示例:2天11小时23分05秒
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
DateTime firstDate = formatter.parseDateTime(firstStringDateValue);
DateTime secondDate = formatter.parseDateTime(secondStringDateValue);
DurationFieldType[] types = {DurationFieldType.days(),
DurationFieldType.hours(),
DurationFieldType.minutes(),
DurationFieldType.seconds()};
Period period = new Period(firstDate, secondDate, PeriodType.forFields(types));
PeriodFormatter periodFormatter = new PeriodFormatterBuilder()
.minimumPrintedDigits(2)
.appendDays().appendSuffix(" Day", " Days").appendSeparator(" ")
.appendHours().appendSuffix("h").appendSeparator(" ")
.appendMinutes().appendSuffix("min").appendSeparator(" ")
.appendSeconds().appendSuffix("sec")
.toFormatter();
String result = periodFormatter.print(period);问题是当开始日期大于结束日期时。
结果是:-2天-11小时23分-05秒。
所以我想知道有没有办法让它看起来像这样:-2天11小时23分05秒,只有一个'-‘在整个事情的前面。但使用与Joda相关的函数。
或者至少有一种方法来告诉我周期是否为负值。
我可以使用字符串操作来完成这个操作,但是它看起来有点过于手动了。
谢谢。
发布于 2020-05-08 12:58:52
你需要的是比较两次约会。
有一些isAfter和isBefore方法可以比较millis的日期(忽略时区)。
firstDate.isBefore(secondDate)
firstDate.isAfter(secondDate)现在,您可以按照正确的顺序设置参数。
然后,如果需要,添加前面的减号。
发布于 2020-05-08 13:35:10
不幸的是,Period与Duration相比没有方法abs()。
但是,我们可以使用Period::negated()方法编写自己的助手方法来实现这一点:
public static Period periodAbs(Period period) {
return period.toStandardSeconds().getSeconds() < 0
? period.negated()
: period;
}此外,如果您不喜欢静态方法,则可以将此逻辑提取为单独的类(在我看来,这无疑是更好的选择):
public static class AbsPeriod {
private final Period period;
public AbsPeriod(Period period) {
this.period = period;
}
public Period value() {
return period.toStandardSeconds().getSeconds() < 0
? period.negated()
: period;
}
}完整的可运行示例:
import org.joda.time.DateTime;
import org.joda.time.DurationFieldType;
import org.joda.time.Period;
import org.joda.time.PeriodType;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.PeriodFormatter;
import org.joda.time.format.PeriodFormatterBuilder;
class Scratch {
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
DateTime firstDate = formatter.parseDateTime("12/11/2001 12:12:12");
DateTime secondDate = formatter.parseDateTime("12/11/2000 12:12:12");
DurationFieldType[] types = {DurationFieldType.days(),
DurationFieldType.hours(),
DurationFieldType.minutes(),
DurationFieldType.seconds()};
Period period = new Period(firstDate, secondDate, PeriodType.forFields(types));
PeriodFormatter periodFormatter = new PeriodFormatterBuilder()
.minimumPrintedDigits(2)
.appendDays().appendSuffix(" Day", " Days").appendSeparator(" ")
.appendHours().appendSuffix("h").appendSeparator(" ")
.appendMinutes().appendSuffix("min").appendSeparator(" ")
.appendSeconds().appendSuffix("sec")
.toFormatter();
System.out.println("original period = " + periodFormatter.print(period));
System.out.println("absolute period = " + periodFormatter.print(new AbsPeriod(period).value()));
}
public static class AbsPeriod {
private final Period period;
public AbsPeriod(Period period) {
this.period = period;
}
public Period value() {
return period.toStandardSeconds().getSeconds() < 0
? period.negated()
: period;
}
}
}以及由此测试片段生成的相应STDOUT:
original period = -365 Days
absolute period = 365 Dayshttps://stackoverflow.com/questions/61679447
复制相似问题