乔达时间段打印 [英] Joda Time Period print
问题描述
我想以"2天,3小时,5分钟和8秒"之类的格式打印两个日期之间的时差.
I'd like to print the difference between two dates in a form like "2 days, 3 hours, 5 minutes and 8 seconds".
我正在使用此代码调用 Joda-Time 库,但是我有一些麻烦.
I'm using this code calling the Joda-Time library but I've some troubles.
Calendar cal = Calendar.getInstance();
DateTime firstSeen = new DateTime(cal.getTime());
cal.add(Calendar.HOUR, 24*8);
cal.add(Calendar.MINUTE, 10);
cal.add(Calendar.SECOND, 45);
DateTime lastSeen = new DateTime(cal.getTime());
Period period = new Period(firstSeen, lastSeen);
PeriodFormatter daysHoursMinutesSeconds = new PeriodFormatterBuilder()
.appendDays()
.appendSuffix(" day", " days")
.appendSeparator(", ")
.appendMinutes()
.appendSuffix(" minute", " minutes")
.appendSeparator(" and ")
.appendSeconds()
.appendSuffix(" second", " seconds")
.toFormatter();
String periodFormatted;
periodFormatted = daysHoursMinutesSeconds.print(period);
System.out.println(periodFormatted);
应打印的内容是"8天10分45秒".相反,我得到"1天10分45秒";日子似乎一天都溢出了.我该如何告诉格式化程序忽略溢出,并将溢出内容累加到第一个单元中?
What should be printed is "8 days, 10 minutes and 45 seconds". Instead I get "1 day, 10 minutes and 45 seconds"; It seems the days overflow into the week. How can I tell the formatter to ignore the overflows and sum what overflows into the first unit?
推荐答案
这是正确的输出,因为时间段是"1周1天45分钟",并且未打印出周"字段:
This is the correct output, because the period is "1 week 1 day, 45 minutes" and week field is not printed:
package com.stackoverflow.so21002436;
import org.joda.time.DateTime;
import org.joda.time.Period;
import org.joda.time.format.PeriodFormatter;
import org.joda.time.format.PeriodFormatterBuilder;
public class App {
public static void main(final String[] args)
{
final DateTime firstSeen = DateTime.now();
final DateTime lastSeen = firstSeen.plusDays(8).plusMinutes(10).plusSeconds(45);
final Period period = new Period(firstSeen, lastSeen);
System.err.println(period); // P1W1DT10M45S, ie. 1 day, 10 minutes and 45 seconds
final PeriodFormatter daysHoursMinutesSeconds = new PeriodFormatterBuilder().appendDays()
.appendSuffix(" day", " days")
.appendSeparator(", ")
.appendMinutes()
.appendSuffix(" minute", " minutes")
.appendSeparator(" and ")
.appendSeconds()
.appendSuffix(" second", " seconds")
.toFormatter();
final String periodFormatted = daysHoursMinutesSeconds.print(period);
System.out.println(periodFormatted);
}
}
或使用特定的 PeriodType
:
or use a specific PeriodType
:
package com.stackoverflow.so21002436;
import org.joda.time.DateTime;
import org.joda.time.Period;
import org.joda.time.PeriodType;
import org.joda.time.format.PeriodFormatter;
import org.joda.time.format.PeriodFormatterBuilder;
public class App {
public static void main(final String[] args)
{
final DateTime firstSeen = DateTime.now();
final DateTime lastSeen = firstSeen.plusDays(8).plusMinutes(10).plusSeconds(45);
final Period period = new Period(firstSeen, lastSeen, PeriodType.dayTime());
System.err.println(period); // P8DT10M45S, ie. 8 days, 10 minutes and 45 seconds
final PeriodFormatter daysHoursMinutesSeconds = new PeriodFormatterBuilder().appendDays()
.appendSuffix(" day", " days")
.appendSeparator(", ")
.appendMinutes()
.appendSuffix(" minute", " minutes")
.appendSeparator(" and ")
.appendSeconds()
.appendSuffix(" second", " seconds")
.toFormatter();
final String periodFormatted = daysHoursMinutesSeconds.print(period);
System.out.println(periodFormatted);
}
}
请参见 查看全文