了解joda时间PeriodFormatter

发布于 2024-10-10 00:15:54 字数 1386 浏览 3 评论 0原文

我以为我明白了,但显然我不明白。你能帮我通过这些单元测试吗?

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千秋岁 2024-10-17 00:15:54

这修复了除 twoDaysAndAHalf 之外的所有测试:

private static final PeriodFormatter dateFormat =
    new PeriodFormatterBuilder()
        .appendDays()
        .appendSuffix(" day", " days")
        .appendSeparator(" ")
        .printZeroIfSupported()
        .minimumPrintedDigits(2)
        .appendHours()
        .appendSeparator(":")
        .appendMinutes()
        .printZeroIfSupported()
        .minimumPrintedDigits(2)
        .appendSeparator(":")
        .appendSeconds()
        .minimumPrintedDigits(2)
        .toFormatter();

编辑:

也许您的 twoDaysAndAHalf 测试应该是这样的?

@Test
public void twoDaysAndAHalf(){
    final int secondsInDay = 60 * 60 * 24;
    assertEquals("2 days and 12:00:00",
        OurDateTimeFormatter.format(1000 * secondsInDay * 5 / 2));
}

然后使用这个(稍作编辑):

private static final PeriodFormatter dateFormat =
        .appendDays()
        .appendSuffix(" day", " days")
        .appendSeparator(" and ") // thx ILMTitan
        // etc. as above

它可以工作

This fixes all tests except twoDaysAndAHalf:

private static final PeriodFormatter dateFormat =
    new PeriodFormatterBuilder()
        .appendDays()
        .appendSuffix(" day", " days")
        .appendSeparator(" ")
        .printZeroIfSupported()
        .minimumPrintedDigits(2)
        .appendHours()
        .appendSeparator(":")
        .appendMinutes()
        .printZeroIfSupported()
        .minimumPrintedDigits(2)
        .appendSeparator(":")
        .appendSeconds()
        .minimumPrintedDigits(2)
        .toFormatter();

EDIT:

perhaps your twoDaysAndAHalf test should be like this?

@Test
public void twoDaysAndAHalf(){
    final int secondsInDay = 60 * 60 * 24;
    assertEquals("2 days and 12:00:00",
        OurDateTimeFormatter.format(1000 * secondsInDay * 5 / 2));
}

Then use this (slightly edited):

private static final PeriodFormatter dateFormat =
        .appendDays()
        .appendSuffix(" day", " days")
        .appendSeparator(" and ") // thx ILMTitan
        // etc. as above

and it works

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文