I created a Utility method using jodatime to calculate date difference and display it such as 3y 2m 4d.
Here is my method:
public static String dateBetween(Object start, Object end) {
Period period = new Period(new LocalDate(start), new LocalDate(end));
PeriodFormatter formatter = new PeriodFormatterBuilder()
.printZeroAlways()
.appendYears().appendSuffix("y ")
.appendMonths().appendSuffix("m ")
.appendDays().appendSuffix("d")
.toFormatter();
return formatter.print(period);
}
The test scripts are created using testng as follow:
@DataProvider(name = "dateBetweenData")
public static Object[][] dateBetweenData() {
return new Object[][] {
{ "01/08/2010", "02/07/2012", "1y 11m 1d" },
{ "01/08/2010", "03/07/2012", "1y 11m 2d" },
{ "01/08/2010", "11/07/2012", "1y 11m 10d" },
{ "01/08/2010", "21/07/2012", "1y 11m 20d" },
{ "01/08/2010", "31/07/2012", "1y 11m 30d" },
};
}
@Test(dataProvider="dateBetweenData")
public void dateBetween(String startDate, String endDate, String expected) throws ParseException {
DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Assert.assertEquals(
DateUtil.dateBetween(format.parse(startDate), format.parse(endDate)),
expected);
}
I noticed that some of my tests are failed:
PASSED: dateBetween("01/08/2010", "02/07/2012", "1y 11m 1d")
PASSED: dateBetween("01/08/2010", "03/07/2012", "1y 11m 2d")
FAILED: dateBetween("01/08/2010", "11/07/2012", "1y 11m 10d")
java.lang.AssertionError: expected [1y 11m 10d] but found [1y 11m 3d]
...
FAILED: dateBetween("01/08/2010", "21/07/2012", "1y 11m 20d")
java.lang.AssertionError: expected [1y 11m 20d] but found [1y 11m 6d]
...
FAILED: dateBetween("01/08/2010", "31/07/2012", "1y 11m 30d")
java.lang.AssertionError: expected [1y 11m 30d] but found [1y 11m 2d]
...
===============================================
Default test
Tests run: 5, Failures: 3, Skips: 0
===============================================
Do I use the wrong method or is it bug in jodatime?
NOTE: I'm using jodatime 2.3 and testng 6.8.7.
See Question&Answers more detail:
os