了解joda时间PeriodFormatter
我以为我明白了,但显然我不明白。你能帮我通过这些单元测试吗?
@Test
public void second() {
assertEquals("00:00:01", OurDateTimeFormatter.format(1000));
}
@Test
public void minute() {
assertEquals("00:01:00", OurDateTimeFormatter.format(1000 * 60));
}
@Test
public void hour() {
assertEquals("01:00:00", OurDateTimeFormatter.format(1000 * 60 * 60));
}
@Test
public void almostMidnight() {
final int secondsInDay = 60 * 60 * 24;
assertEquals("23:59:59", OurDateTimeFormatter.format(1000 * (secondsInDay - 1)));
}
@Test
public void twoDaysAndAHalf() {
final int secondsInDay = 60 * 60 * 24;
assertEquals("12:00:00 and 2 days", OurDateTimeFormatter.format(1000 * secondsInDay * 5 / 2));
}
实际代码在这里:
public class OurDateTimeFormatter {
public OurDateTimeFormatter() {
}
private static final PeriodFormatter dateFormat = new PeriodFormatterBuilder()
.appendDays()
.appendSuffix(" day", " days")
.appendSeparator(" ")
.appendHours()
.appendSeparator(":")
.appendMinutes().minimumPrintedDigits(2)
.appendSeparator(":")
.appendSeconds().minimumPrintedDigits(2)
.toFormatter();
public static String format(long millis) {
return dateFormat.print(new Period(millis).normalizedStandard());
}
}
I thought I understand it, but apparently I don't. Can you help me make these unit tests pass?
@Test
public void second() {
assertEquals("00:00:01", OurDateTimeFormatter.format(1000));
}
@Test
public void minute() {
assertEquals("00:01:00", OurDateTimeFormatter.format(1000 * 60));
}
@Test
public void hour() {
assertEquals("01:00:00", OurDateTimeFormatter.format(1000 * 60 * 60));
}
@Test
public void almostMidnight() {
final int secondsInDay = 60 * 60 * 24;
assertEquals("23:59:59", OurDateTimeFormatter.format(1000 * (secondsInDay - 1)));
}
@Test
public void twoDaysAndAHalf() {
final int secondsInDay = 60 * 60 * 24;
assertEquals("12:00:00 and 2 days", OurDateTimeFormatter.format(1000 * secondsInDay * 5 / 2));
}
Where the actual code is here:
public class OurDateTimeFormatter {
public OurDateTimeFormatter() {
}
private static final PeriodFormatter dateFormat = new PeriodFormatterBuilder()
.appendDays()
.appendSuffix(" day", " days")
.appendSeparator(" ")
.appendHours()
.appendSeparator(":")
.appendMinutes().minimumPrintedDigits(2)
.appendSeparator(":")
.appendSeconds().minimumPrintedDigits(2)
.toFormatter();
public static String format(long millis) {
return dateFormat.print(new Period(millis).normalizedStandard());
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这修复了除
twoDaysAndAHalf
之外的所有测试:编辑:
也许您的
twoDaysAndAHalf
测试应该是这样的?然后使用这个(稍作编辑):
它可以工作
This fixes all tests except
twoDaysAndAHalf
:EDIT:
perhaps your
twoDaysAndAHalf
test should be like this?Then use this (slightly edited):
and it works