Author: sletellier Date: 2011-03-30 17:00:07 +0200 (Wed, 30 Mar 2011) New Revision: 2096 Url: http://nuiton.org/repositories/revision/nuiton-utils/2096 Log: - Create method setLastDayOfYear and setFirstDayOfYear - Debug setLastDayOfMonth, dont set max of day Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/DateUtil.java trunk/nuiton-utils/src/test/java/org/nuiton/util/DateUtilTest.java Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/DateUtil.java =================================================================== --- trunk/nuiton-utils/src/main/java/org/nuiton/util/DateUtil.java 2011-03-25 12:13:54 UTC (rev 2095) +++ trunk/nuiton-utils/src/main/java/org/nuiton/util/DateUtil.java 2011-03-30 15:00:07 UTC (rev 2096) @@ -152,7 +152,6 @@ int maximum = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); calendar.set(Calendar.DAY_OF_MONTH, maximum); Date lastDay = calendar.getTime(); - lastDay = setMaxTimeOfDay(lastDay); return lastDay; } @@ -165,10 +164,38 @@ public static Date setFirstDayOfMonth(Date date) { Calendar calendar = getDefaultCalendar(date); calendar.set(Calendar.DAY_OF_MONTH, 1); - return calendar.getTime(); + Date firstDay = calendar.getTime(); + return firstDay; } /** + * Set the last day of year to the date in argument. + * + * @param date Date to modify + * @return the date with day of year modified + */ + public static Date setLastDayOfYear(Date date) { + Calendar calendar = getDefaultCalendar(date); + int maximum = calendar.getActualMaximum(Calendar.DAY_OF_YEAR); + calendar.set(Calendar.DAY_OF_YEAR, maximum); + Date lastDay = calendar.getTime(); + return lastDay; + } + + /** + * Set the first day of year to the date in argument. + * + * @param date Date to modify + * @return the date with day of year modified + */ + public static Date setFirstDayOfYear(Date date) { + Calendar calendar = getDefaultCalendar(date); + calendar.set(Calendar.DAY_OF_YEAR, 1); + Date firstDay = calendar.getTime(); + return firstDay; + } + + /** * Set the min time of the day : 00:00:00.000. * * @param date to modify @@ -201,7 +228,7 @@ } /** - * Check if the first date in argument is included between the two other + * Check if the first date in argument is included between the two other * dates. The argument myDate can be equals to beforeDate or afterDate to * validate the includes. * @@ -216,7 +243,7 @@ } boolean result = true; result &= myDate.after(beforeDate) || myDate.compareTo(beforeDate) == 0; - result &= afterDate == null || myDate.before(afterDate) || + result &= afterDate == null || myDate.before(afterDate) || myDate.compareTo(afterDate) == 0; return result; } @@ -226,7 +253,7 @@ * * @param beforeDate the first date of the period * @param afterDate the second date of the period - * @return true if the current date is included between the two dates, + * @return true if the current date is included between the two dates, * false otherwise * @see #between(Date, Date, Date) */ Modified: trunk/nuiton-utils/src/test/java/org/nuiton/util/DateUtilTest.java =================================================================== --- trunk/nuiton-utils/src/test/java/org/nuiton/util/DateUtilTest.java 2011-03-25 12:13:54 UTC (rev 2095) +++ trunk/nuiton-utils/src/test/java/org/nuiton/util/DateUtilTest.java 2011-03-30 15:00:07 UTC (rev 2096) @@ -96,9 +96,31 @@ } Assert.assertTrue(DateUtil.between(day1, firstDayOfFebruary, lastDayOfFebruary)); - Assert.assertTrue(DateUtil.between(day2, firstDayOfFebruary, lastDayOfFebruary)); + + // In this case, day2'hour is after lastDayOfMonth + Assert.assertFalse(DateUtil.between(day2, firstDayOfFebruary, lastDayOfFebruary)); } + /** + * Test of setLastDayOfMonth method, of class DateUtil. + */ + @Test + public void testSetFistLastDayOfYear() { + Date february = DateUtil.createDate(22, 2, 2011); + Date firstDayOf2011 = DateUtil.setFirstDayOfYear(february); + Date lastDayOf2011 = DateUtil.setLastDayOfYear(february); + Date day1 = DateUtil.createDate(0, 0, 2, 1, 2, 2011); + Date day2 = DateUtil.createDate(67, 45, 23, 28, 2, 2012); + + if (log.isDebugEnabled()) { + log.debug("first day of february = " + firstDayOf2011 + ", last day of february = " + + lastDayOf2011 + ", day1 = " + day1 + ", day 2 = " + day2); + } + + Assert.assertTrue(DateUtil.between(day1, firstDayOf2011, lastDayOf2011)); + Assert.assertFalse(DateUtil.between(day2, firstDayOf2011, lastDayOf2011)); + } + /** * Test of between method, of class DateUtil. */