Understanding the Jedi Time PeriodFormatter

I thought I understood, but apparently I do not. Can you help me pass these unit tests?

@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 is the real code:

 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()); } } 
+4
source share
1 answer

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 he works

+7
source

Source: https://habr.com/ru/post/897856/


All Articles