Author: fdesbois Date: 2009-12-15 17:00:48 +0100 (Tue, 15 Dec 2009) New Revision: 1701 Added: trunk/src/test/java/org/nuiton/util/DateUtilsTest.java Modified: trunk/src/main/java/org/nuiton/util/DateUtils.java Log: Add method to give difference in days between two dates Modified: trunk/src/main/java/org/nuiton/util/DateUtils.java =================================================================== --- trunk/src/main/java/org/nuiton/util/DateUtils.java 2009-12-14 16:02:55 UTC (rev 1700) +++ trunk/src/main/java/org/nuiton/util/DateUtils.java 2009-12-15 16:00:48 UTC (rev 1701) @@ -108,4 +108,11 @@ calendar.setTime(date); return calendar.get(Calendar.MONTH); } + + public static int getDifferenceInDays(Date beginDate, Date endDate) { + long begin = beginDate.getTime(); + long end = endDate.getTime(); + // 86400000 = 24 * 60 * 60 * 1000 + return (int) Math.ceil(((end - begin) / 86400000)); + } } Added: trunk/src/test/java/org/nuiton/util/DateUtilsTest.java =================================================================== --- trunk/src/test/java/org/nuiton/util/DateUtilsTest.java (rev 0) +++ trunk/src/test/java/org/nuiton/util/DateUtilsTest.java 2009-12-15 16:00:48 UTC (rev 1701) @@ -0,0 +1,126 @@ + +package org.nuiton.util; + +import java.util.Date; +import junit.framework.Assert; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * + * @author fdesbois + */ +public class DateUtilsTest { + + public DateUtilsTest() { + } + + @BeforeClass + public static void setUpClass() throws Exception { + } + + @AfterClass + public static void tearDownClass() throws Exception { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + /** + * Test of formatDate method, of class DateUtils. + */ + //@Test + public void testFormatDate() { + System.out.println("formatDate"); + } + + /** + * Test of parseDate method, of class DateUtils. + */ + //@Test + public void testParseDate() { + System.out.println("parseDate"); + } + + /** + * Test of createDate method, of class DateUtils. + */ + //@Test + public void testCreateDate() { + System.out.println("createDate"); + } + + /** + * Test of createDateAfterToday method, of class DateUtils. + */ + //@Test + public void testCreateDateAfterToday() { + System.out.println("createDateAfterToday"); + } + + /** + * Test of setLastDayOfMonth method, of class DateUtils. + */ + //@Test + public void testSetLastDayOfMonth() { + System.out.println("setLastDayOfMonth"); + } + + /** + * Test of setFirstDayOfMonth method, of class DateUtils. + */ + //@Test + public void testSetFirstDayOfMonth() { + System.out.println("setFirstDayOfMonth"); + } + + /** + * Test of between method, of class DateUtils. + */ + //@Test + public void testBetween() { + System.out.println("between"); + } + + /** + * Test of currentPeriod method, of class DateUtils. + */ + //@Test + public void testCurrentPeriod() { + System.out.println("currentPeriod"); + } + + /** + * Test of getMonth method, of class DateUtils. + */ + //@Test + public void testGetMonth() { + System.out.println("getMonth"); + } + + @Test + public void testGetDifferenceInDays() { + System.out.println("getDifferenceInDays"); + + Date beginDate = DateUtils.createDate(3, 2, 2009); + Date endDate = DateUtils.createDate(8, 2, 2009); + + int result = DateUtils.getDifferenceInDays(beginDate, endDate); + Assert.assertEquals(5, result); + + beginDate = DateUtils.createDate(28, 1, 2009); + endDate = DateUtils.createDate(8, 2, 2009); + + result = DateUtils.getDifferenceInDays(beginDate, endDate); + Assert.assertEquals(11, result); + } + +} \ No newline at end of file Property changes on: trunk/src/test/java/org/nuiton/util/DateUtilsTest.java ___________________________________________________________________ Added: svn:keywords + "Author Date Id Revision HeadURL"