JodaTime 显示周数
JodaTime show weeks in duration
我正在尝试以下列格式显示两个日期时间之间的时间差:
? years, ? months, ? weeks, ? days, ? hours, ? minutes, ? seconds and ? milliseconds
我已经得到它显示几乎像这样,但它不显示星期。我试过使用 PeriodType.standard(),但省略了月份和年份。可以这样做吗?
这是我用来实现当前结果的代码:
private String getPeriodBetween(DateTime from, DateTime to, boolean showMilliseconds) {
Period period;
if (from.isAfter(to)) {
period = new Period(to, from);
} else {
period = new Period(from, to);
}
PeriodFormatterBuilder builder = new PeriodFormatterBuilder().appendYears()
.appendSuffix(" year, ", " years, ")
.appendMonths()
.appendSuffix(" month, ", " months, ")
.appendWeeks()
.appendSuffix(" week, ", " weeks, ")
.appendDays()
.appendSuffix(" day, ", " days, ")
.appendHours()
.appendSuffix(" hour, ", " hours, ")
.appendMinutes()
.appendSuffix(" minute, ", " minutes, ")
.appendSeconds()
.appendSuffix(" second, ", " seconds, ");
if (showMilliseconds) {
builder = builder.appendMillis().appendSuffix(" millisecond", " milliseconds");
}
return builder.toFormatter()
.print(period.normalizedStandard(PeriodType.yearMonthDayTime()))
.replaceAll("[,]\s*$", "")
.replaceAll("(?:,\s*)([^,]+)$", " and ")
.replaceAll("^(?![\s\S])", "No difference");
}
如果我使用 PeriodType.forFields();
并指定包含所有必填字段的 DurationFieldType
数组,它会省略年和月。
想通了。问题是我最初使用 Duration
并将其转换为 Period
。我曾尝试使用以下代码来修复它,但它并没有给我预期的结果。在我将其更改为 Period
之后,我可以使用 Period.forFields();
指定我需要的所有字段,接受以下内容:
private static final DurationFieldType[] DURATION_FIELD_TYPES = {
DurationFieldType.years(),
DurationFieldType.months(),
DurationFieldType.weeks(),
DurationFieldType.days(),
DurationFieldType.hours(),
DurationFieldType.minutes(),
DurationFieldType.seconds(),
DurationFieldType.millis(),
};
没关系,您甚至不需要规范化标准,因为它已经默认包含所有字段。现在想想,也有道理。这是我的完整工作代码供参考:
private String getDifference(DateTime from, DateTime to, boolean showMilliseconds) {
Period period;
if (from.isAfter(to)) {
period = new Period(to, from);
} else {
period = new Period(from, to);
}
PeriodFormatterBuilder builder = new PeriodFormatterBuilder()
.appendYears()
.appendSuffix(" year, ", " years, ")
.appendMonths()
.appendSuffix(" month, ", " months, ")
.appendWeeks()
.appendSuffix(" week, ", " weeks, ")
.appendDays()
.appendSuffix(" day, ", " days, ")
.appendHours()
.appendSuffix(" hour, ", " hours, ")
.appendMinutes()
.appendSuffix(" minute, ", " minutes, ")
.appendSeconds()
.appendSuffix(" second, ", " seconds, ");
if (showMilliseconds) {
builder = builder.appendMillis().appendSuffix(" millisecond", " milliseconds");
}
return builder
.toFormatter()
.print(period)
.replaceAll("[,]\s*$", "")
.replaceAll("(?:,\s*)([^,]+)$", " and ")
.replaceAll("^(?![\s\S])", "No difference");
}
我正在尝试以下列格式显示两个日期时间之间的时间差:
? years, ? months, ? weeks, ? days, ? hours, ? minutes, ? seconds and ? milliseconds
我已经得到它显示几乎像这样,但它不显示星期。我试过使用 PeriodType.standard(),但省略了月份和年份。可以这样做吗?
这是我用来实现当前结果的代码:
private String getPeriodBetween(DateTime from, DateTime to, boolean showMilliseconds) {
Period period;
if (from.isAfter(to)) {
period = new Period(to, from);
} else {
period = new Period(from, to);
}
PeriodFormatterBuilder builder = new PeriodFormatterBuilder().appendYears()
.appendSuffix(" year, ", " years, ")
.appendMonths()
.appendSuffix(" month, ", " months, ")
.appendWeeks()
.appendSuffix(" week, ", " weeks, ")
.appendDays()
.appendSuffix(" day, ", " days, ")
.appendHours()
.appendSuffix(" hour, ", " hours, ")
.appendMinutes()
.appendSuffix(" minute, ", " minutes, ")
.appendSeconds()
.appendSuffix(" second, ", " seconds, ");
if (showMilliseconds) {
builder = builder.appendMillis().appendSuffix(" millisecond", " milliseconds");
}
return builder.toFormatter()
.print(period.normalizedStandard(PeriodType.yearMonthDayTime()))
.replaceAll("[,]\s*$", "")
.replaceAll("(?:,\s*)([^,]+)$", " and ")
.replaceAll("^(?![\s\S])", "No difference");
}
如果我使用 PeriodType.forFields();
并指定包含所有必填字段的 DurationFieldType
数组,它会省略年和月。
想通了。问题是我最初使用 Duration
并将其转换为 Period
。我曾尝试使用以下代码来修复它,但它并没有给我预期的结果。在我将其更改为 Period
之后,我可以使用 Period.forFields();
指定我需要的所有字段,接受以下内容:
private static final DurationFieldType[] DURATION_FIELD_TYPES = {
DurationFieldType.years(),
DurationFieldType.months(),
DurationFieldType.weeks(),
DurationFieldType.days(),
DurationFieldType.hours(),
DurationFieldType.minutes(),
DurationFieldType.seconds(),
DurationFieldType.millis(),
};
没关系,您甚至不需要规范化标准,因为它已经默认包含所有字段。现在想想,也有道理。这是我的完整工作代码供参考:
private String getDifference(DateTime from, DateTime to, boolean showMilliseconds) {
Period period;
if (from.isAfter(to)) {
period = new Period(to, from);
} else {
period = new Period(from, to);
}
PeriodFormatterBuilder builder = new PeriodFormatterBuilder()
.appendYears()
.appendSuffix(" year, ", " years, ")
.appendMonths()
.appendSuffix(" month, ", " months, ")
.appendWeeks()
.appendSuffix(" week, ", " weeks, ")
.appendDays()
.appendSuffix(" day, ", " days, ")
.appendHours()
.appendSuffix(" hour, ", " hours, ")
.appendMinutes()
.appendSuffix(" minute, ", " minutes, ")
.appendSeconds()
.appendSuffix(" second, ", " seconds, ");
if (showMilliseconds) {
builder = builder.appendMillis().appendSuffix(" millisecond", " milliseconds");
}
return builder
.toFormatter()
.print(period)
.replaceAll("[,]\s*$", "")
.replaceAll("(?:,\s*)([^,]+)$", " and ")
.replaceAll("^(?![\s\S])", "No difference");
}