Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
372 views
in Technique[技术] by (71.8m points)

testng - Incorrect jodatime period - years months days

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Unfortunately the standard period type in Joda Time includes weeks (yes - not intuitive at all, so it is not sufficient just to write new Period(from, to), but to use the alternative 3-args-constructor with an explicit period type argument where you specify only years, months and days.

Solution:

Period period = 
  new Period(new LocalDate(start), new LocalDate(end), PeriodType.yearMonthDay());

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...