r1868 - in trunk/src: main/java/org/nuiton/util test/java/org/nuiton/util
Author: sletellier Date: 2010-05-25 15:36:14 +0200 (Tue, 25 May 2010) New Revision: 1868 Url: http://nuiton.org/repositories/revision/nuiton-utils/1868 Log: Add 3 method to get the difference between two dates in second, minutes and hours Modified: trunk/src/main/java/org/nuiton/util/DateUtils.java trunk/src/test/java/org/nuiton/util/DateUtilsTest.java Modified: trunk/src/main/java/org/nuiton/util/DateUtils.java =================================================================== --- trunk/src/main/java/org/nuiton/util/DateUtils.java 2010-05-20 14:34:45 UTC (rev 1867) +++ trunk/src/main/java/org/nuiton/util/DateUtils.java 2010-05-25 13:36:14 UTC (rev 1868) @@ -85,6 +85,29 @@ /** * Create a new date from day, month and year (French version). * The month is the real month of the year and not the one which is stored + * in Calendar object. + * @param s value of the seconds 1-60 + * @param dd value of the minutes 1-60 + * @param dd value of the hours 1-24 + * @param dd value of the day 1-31 + * @param mm value of the month 1-12 + * @param yy value of the year 0-9999 + * @return a new date + */ + public static Date createDate(int s, int m, int h, int dd, int mm, int yy) { + Calendar calendar = Calendar.getInstance(); + calendar.set(Calendar.YEAR, yy); + calendar.set(Calendar.MONTH, mm-1); + calendar.set(Calendar.DAY_OF_MONTH, dd); + calendar.set(Calendar.HOUR_OF_DAY, h); + calendar.set(Calendar.MINUTE, m); + calendar.set(Calendar.SECOND, s); + return calendar.getTime(); + } + + /** + * Create a new date from day, month and year (French version). + * The month is the real month of the year and not the one which is stored * in Calendar object. Time is set to 00:00:00.000 * @param dd value of the day 1-31 * @param mm value of the month 1-12 @@ -221,6 +244,50 @@ /** * Do the difference between the two dates in argument. The result is a number + * of seconds between the two dates. + * + * @param beginDate first date + * @param endDate second date + * @return a number of seconds between beginDate and endDate + */ + public static int getDifferenceInSeconds(Date beginDate, Date endDate) { + long begin = beginDate.getTime(); + long end = endDate.getTime(); + return (int) Math.ceil((end - begin) / 1000); + } + + /** + * Do the difference between the two dates in argument. The result is a number + * of minutes between the two dates. + * + * @param beginDate first date + * @param endDate second date + * @return a number of minutes between beginDate and endDate + */ + public static int getDifferenceInMinutes(Date beginDate, Date endDate) { + long begin = beginDate.getTime(); + long end = endDate.getTime(); + // 60000 = 60 * 1000 + return (int) Math.ceil((end - begin) / 60000); + } + + /** + * Do the difference between the two dates in argument. The result is a number + * of hours between the two dates. + * + * @param beginDate first date + * @param endDate second date + * @return a number of hours between beginDate and endDate + */ + public static int getDifferenceInHours(Date beginDate, Date endDate) { + long begin = beginDate.getTime(); + long end = endDate.getTime(); + // 3600000 = 60 * 60 * 1000 + return (int) Math.ceil((end - begin) / 3600000); + } + + /** + * Do the difference between the two dates in argument. The result is a number * of days between the two dates. * Ex : 28/01/2009 and 08/02/2009 return 11. * @@ -249,7 +316,7 @@ int count = 0; Calendar fromCalendar = getDefaultCalendar(beginDate); Calendar thruCalendar = getDefaultCalendar(endDate); - + while (fromCalendar.before(thruCalendar)) { fromCalendar.add(Calendar.MONTH, 1); count++; @@ -279,7 +346,7 @@ } /** - * Get the date before today (so yesterday no ?) + * Get the date before today * * @param date concerned * @return Date before today Modified: trunk/src/test/java/org/nuiton/util/DateUtilsTest.java =================================================================== --- trunk/src/test/java/org/nuiton/util/DateUtilsTest.java 2010-05-20 14:34:45 UTC (rev 1867) +++ trunk/src/test/java/org/nuiton/util/DateUtilsTest.java 2010-05-25 13:36:14 UTC (rev 1868) @@ -178,6 +178,57 @@ } @Test + public void testGetDifferenceInSeconds() { + System.out.println("getDifferenceInSecondes"); + + Date beginDate = DateUtils.createDate(30, 10, 0, 3, 2, 2009); + Date endDate = DateUtils.createDate(00, 11, 0, 3, 2, 2009); + + int result = DateUtils.getDifferenceInSeconds(beginDate, endDate); + Assert.assertEquals(30, result); + + beginDate = DateUtils.createDate(9, 28, 0, 28, 1, 2009); + endDate = DateUtils.createDate(50, 30, 0, 28, 1, 2009); + + result = DateUtils.getDifferenceInSeconds(beginDate, endDate); + Assert.assertEquals(161, result); + } + + @Test + public void testGetDifferenceInMinutes() { + System.out.println("getDifferenceInMinutes"); + + Date beginDate = DateUtils.createDate(30, 10, 0, 3, 2, 2009); + Date endDate = DateUtils.createDate(00, 12, 0, 3, 2, 2009); + + int result = DateUtils.getDifferenceInMinutes(beginDate, endDate); + Assert.assertEquals(1, result); + + beginDate = DateUtils.createDate(9, 28, 0, 28, 1, 2009); + endDate = DateUtils.createDate(50, 30, 0, 28, 1, 2009); + + result = DateUtils.getDifferenceInMinutes(beginDate, endDate); + Assert.assertEquals(2, result); + } + + @Test + public void testGetDifferenceInHours() { + System.out.println("getDifferenceInHours"); + + Date beginDate = DateUtils.createDate(30, 10, 0, 3, 2, 2009); + Date endDate = DateUtils.createDate(00, 11, 0, 4, 2, 2009); + + int result = DateUtils.getDifferenceInHours(beginDate, endDate); + Assert.assertEquals(24, result); + + beginDate = DateUtils.createDate(9, 28, 0, 28, 1, 2009); + endDate = DateUtils.createDate(50, 30, 8, 28, 1, 2009); + + result = DateUtils.getDifferenceInHours(beginDate, endDate); + Assert.assertEquals(8, result); + } + + @Test public void testGetDifferenceInDays() { System.out.println("getDifferenceInDays");
participants (1)
-
sletellier@users.nuiton.org