r1909 - in trunk/src: main/java/org/nuiton/util test/java/org/nuiton/util
Author: tchemit Date: 2010-08-29 16:34:29 +0200 (Sun, 29 Aug 2010) New Revision: 1909 Url: http://nuiton.org/repositories/revision/nuiton-utils/1909 Log: Evolution #831: Deprecates DateUtils in favour to DateUtil Added: trunk/src/main/java/org/nuiton/util/DateUtil.java trunk/src/test/java/org/nuiton/util/DateUtilTest.java Modified: trunk/src/main/java/org/nuiton/util/DateUtils.java trunk/src/test/java/org/nuiton/util/DateUtilsTest.java Added: trunk/src/main/java/org/nuiton/util/DateUtil.java =================================================================== --- trunk/src/main/java/org/nuiton/util/DateUtil.java (rev 0) +++ trunk/src/main/java/org/nuiton/util/DateUtil.java 2010-08-29 14:34:29 UTC (rev 1909) @@ -0,0 +1,372 @@ +/* + * #%L + * Nuiton Utils + * + * $Id: DateUtils.java 1907 2010-08-26 11:09:55Z tchemit $ + * $HeadURL: http://svn.nuiton.org/svn/nuiton-utils/trunk/src/main/java/org/nuiton/util/D... $ + * %% + * Copyright (C) 2004 - 2010 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.nuiton.util; + +import java.text.DateFormat; +import java.text.DateFormatSymbols; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.Locale; + +/** + * Library for manipulating dates. + * + * <b>Note: </b> + * @author fdesbois + * @version $Id: DateUtils.java 1907 2010-08-26 11:09:55Z tchemit $ + * @since 1.4.1 + */ +public class DateUtil { + + public static final String DEFAULT_PATTERN = "dd/MM/yyyy"; + + public static final String MONTH_PATTERN = "MM/yyyy"; + + /** + * Format a date using the pattern in argument. The pattern is the same using + * for DateFormat object. + * + * @param date the date to format + * @param pattern the pattern to use + * @return a String corresponding to the date formatted + * @see DateFormat + */ + public static String formatDate(Date date, String pattern) { + SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); + return simpleDateFormat.format(date); + } + + public static String formatDate(Date date, String pattern, Locale locale) { + SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern, locale); + return simpleDateFormat.format(date); + } + + /** + * Parse a date using the pattern in argument. The pattern is the same using + * for DateFormat object. + * + * @param date the String to parse + * @param pattern the pattern to use + * @return a Date corresponding to the String argument parsed + * @throws ParseException for parsing errors + * @see DateFormat + */ + public static Date parseDate(String date, String pattern) throws ParseException { + SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); + Date result = simpleDateFormat.parse(date); + return result; + } + + /** + * 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 m value of the minutes 1-60 + * @param h 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 + * @param yy value of the year 0-9999 + * @return a new date + */ + public static Date createDate(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); + return setMinTimeOfDay(calendar.getTime()); + } + + /** + * Create a new date after the current date (today) with modification on day, month and year. + * You can use negative values on arguments to have a date before today. + * + * @param ddStep nb days you want to increase from the current date + * @param mmStep nb months you want to increase from the current date + * @param yyStep nb years you want to increase from the current date + * @return a new date from the current date increase by days, months and years. + */ + public static Date createDateAfterToday(int ddStep, int mmStep, int yyStep) { + Calendar calendar = getDefaultCalendar(new Date()); + calendar.add(Calendar.DAY_OF_MONTH, ddStep); + calendar.add(Calendar.MONTH,mmStep); + calendar.add(Calendar.YEAR, yyStep); + return calendar.getTime(); + } + + /** + * Set the last day of month to the date in argument. + * The value depends on the month of the date. (30 april, 28 february, ...) + * + * @param date Date to modify + * @return the date with day of month modified + */ + public static Date setLastDayOfMonth(Date date) { + Calendar calendar = getDefaultCalendar(date); + int maximum = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); + calendar.set(Calendar.DAY_OF_MONTH, maximum); + return calendar.getTime(); + } + + /** + * Set the first day of month to the date in argument. + * + * @param date Date to modify + * @return the date with day of month modified + */ + public static Date setFirstDayOfMonth(Date date) { + Calendar calendar = getDefaultCalendar(date); + calendar.set(Calendar.DAY_OF_MONTH, 1); + return calendar.getTime(); + } + + /** + * Set the min time of the day : 00:00:00.000. + * + * @param date to modify + * @return Date with the time set to the minimum in the day + */ + public static Date setMinTimeOfDay(Date date) { + Calendar calendar = getDefaultCalendar(date); + calendar.set(Calendar.AM_PM, Calendar.AM); + calendar.set(Calendar.HOUR, 0); + calendar.set(Calendar.MINUTE, 0); + calendar.set(Calendar.SECOND, 0); + calendar.set(Calendar.MILLISECOND, 0); + return calendar.getTime(); + } + + /** + * Set the max time of the day. EUROPE : 23:59:59.999. + * + * @param date to modify + * @return Date with the time set to the maximum in the day + */ + public static Date setMaxTimeOfDay(Date date) { + Calendar calendar = getDefaultCalendar(date); + calendar.set(Calendar.AM_PM, Calendar.PM); + calendar.set(Calendar.HOUR, 11); + calendar.set(Calendar.MINUTE, 59); + calendar.set(Calendar.SECOND, 59); + calendar.set(Calendar.MILLISECOND, 999); + return calendar.getTime(); + } + + /** + * 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. + * + * @param myDate the date to test + * @param beforeDate the first date of the period to test + * @param afterDate the second date of the period to test + * @return true if myDate is included between beforeDate and afterDate + */ + public static boolean between(Date myDate, Date beforeDate, Date afterDate) { + if (myDate == null) { + return false; + } + boolean result = true; + result &= myDate.after(beforeDate) || myDate.compareTo(beforeDate) == 0; + result &= afterDate == null || myDate.before(afterDate) || + myDate.compareTo(afterDate) == 0; + return result; + } + + /** + * Check if the current date is between the two dates in argument. + * + * @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, + * false otherwise + * @see #between(Date, Date, Date) + */ + public static boolean currentPeriod(Date beforeDate, Date afterDate) { + return between(new Date(), beforeDate, afterDate); + } + + /** + * Get the month value from a date (between 0 and 11). + * + * @param date the date to extract month + * @return the month value of the date + */ + public static int getMonth(Date date) { + Calendar calendar = getDefaultCalendar(date); + return calendar.get(Calendar.MONTH); + } + + /** + * 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. + * + * @param beginDate first date + * @param endDate second date + * @return a number of days between beginDate and endDate + */ + 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); + } + + /** + * Do the difference between the two dates in argument. The result is a number + * of months between the two dates. + * Ex : 01/01/2009 and 28/02/2009 return 2 months. + * Warning, if beginDate is inferior to endDate, the result will be 1 minimum + * + * @param beginDate first date + * @param endDate second date + * @return a number of months between beginDate and endDate + */ + public static int getDifferenceInMonths(Date beginDate, Date endDate) { + int count = 0; + Calendar fromCalendar = getDefaultCalendar(beginDate); + Calendar thruCalendar = getDefaultCalendar(endDate); + + while (fromCalendar.before(thruCalendar)) { + fromCalendar.add(Calendar.MONTH, 1); + count++; + } + return count; + } + + /** + * Get libelle of the month corresponding to the number given in argument. + * + * @param monthNumber between 1-12 + * @param locale Locale for language support + * @return a String corresponding to the libelle of the month + */ + public static String getMonthLibelle(int monthNumber, Locale locale) { + return new DateFormatSymbols(locale).getMonths()[monthNumber-1]; + } + + /** + * Get libelle of the month corresponding to the number given in argument. + * + * @param monthNumber between 1-12 + * @return a String corresponding to the libelle of the month + */ + public static String getMonthLibelle(int monthNumber) { + return getMonthLibelle(monthNumber, Locale.getDefault()); + } + + /** + * Get the date before today + * + * @param date concerned + * @return Date before today + */ + public static Date getYesterday(Date date) { + Calendar cal = getDefaultCalendar(date); + cal.roll(Calendar.DAY_OF_MONTH, false); + return cal.getTime(); + } + + /** + * Get the calendar corresponding to the {@code date}. The default calendar + * will be returned (default time zone and locale). + * + * @param date used to set the calendar time + * @return the default calendar with time corresponding to the {@code date} + */ + public static Calendar getDefaultCalendar(Date date) { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(date); + return calendar; + } +} Modified: trunk/src/main/java/org/nuiton/util/DateUtils.java =================================================================== --- trunk/src/main/java/org/nuiton/util/DateUtils.java 2010-08-29 14:21:26 UTC (rev 1908) +++ trunk/src/main/java/org/nuiton/util/DateUtils.java 2010-08-29 14:34:29 UTC (rev 1909) @@ -34,12 +34,13 @@ import java.util.Locale; /** - * TODO tchemit 2010-08-25 : the name of the class does not fit, it should be called {@code DateUtil}. * Library for manipulating dates. * * @author fdesbois * @version $Id$ + * @deprecated since 1.4.1 (use now {@link DateUtil} */ +@Deprecated public class DateUtils { public static final String DEFAULT_PATTERN = "dd/MM/yyyy"; Added: trunk/src/test/java/org/nuiton/util/DateUtilTest.java =================================================================== --- trunk/src/test/java/org/nuiton/util/DateUtilTest.java (rev 0) +++ trunk/src/test/java/org/nuiton/util/DateUtilTest.java 2010-08-29 14:34:29 UTC (rev 1909) @@ -0,0 +1,291 @@ +/* + * #%L + * Nuiton Utils + * + * $Id: DateUtilsTest.java 1907 2010-08-26 11:09:55Z tchemit $ + * $HeadURL: http://svn.nuiton.org/svn/nuiton-utils/trunk/src/test/java/org/nuiton/util/D... $ + * %% + * Copyright (C) 2004 - 2010 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.nuiton.util; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.sql.Timestamp; +import java.util.Calendar; +import java.util.Date; +import java.util.Locale; + +/** + * + * @author fdesbois + * @since 1.4.1 + */ +public class DateUtilTest { + + /** Logger */ + private static final Log log = LogFactory.getLog(DateUtilTest.class); + + @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() { + log.info("formatDate"); + } + + /** + * Test of parseDate method, of class DateUtils. + */ + //@Test + public void testParseDate() { + log.info("parseDate"); + } + + /** + * Test of createDate method, of class DateUtils. + */ + @Test + public void testCreateDate() { + log.info("createDate"); + + Date newdate = DateUtil.createDate(3, 3, 2009); + Calendar calendar = DateUtil.getDefaultCalendar(newdate); + + Assert.assertEquals(3, calendar.get(Calendar.DAY_OF_MONTH)); + Assert.assertEquals(2, calendar.get(Calendar.MONTH)); + Assert.assertEquals(2009, calendar.get(Calendar.YEAR)); + + Assert.assertEquals(0, calendar.get(Calendar.HOUR)); + Assert.assertEquals(0, calendar.get(Calendar.MINUTE)); + Assert.assertEquals(0, calendar.get(Calendar.SECOND)); + Assert.assertEquals(0, calendar.get(Calendar.MILLISECOND)); + } + + /** + * Test of createDateAfterToday method, of class DateUtil. + */ + //@Test + public void testCreateDateAfterToday() { + log.info("createDateAfterToday"); + } + + /** + * Test of setLastDayOfMonth method, of class DateUtil. + */ + //@Test + public void testSetLastDayOfMonth() { + log.info("setLastDayOfMonth"); + } + + /** + * Test of setFirstDayOfMonth method, of class DateUtil. + */ + //@Test + public void testSetFirstDayOfMonth() { + log.info("setFirstDayOfMonth"); + } + + /** + * Test of between method, of class DateUtil. + */ + @Test + public void testBetween() { + log.info("between"); + + Date middle = DateUtil.createDate(3, 3, 2009); + + // middle = begin, and end = null + Date begin = DateUtil.createDate(3, 3, 2009); + Date end = null; + + boolean result = DateUtil.between(middle, begin, end); + Assert.assertTrue(result); + + // middle between the period : march 2009 + PeriodDates period = new PeriodDates(begin, begin); + period.initDayOfMonthExtremities(); + + log.info("period : " + period); + + result = DateUtil.between(middle, period.getFromDate(), period.getThruDate()); + Assert.assertTrue(result); + + // middle = period begin + middle = DateUtil.setFirstDayOfMonth(middle); + result = DateUtil.between(middle, period.getFromDate(), period.getThruDate()); + Assert.assertTrue(result); + + // test with different implementation of Date + Timestamp middle2 = new Timestamp(middle.getTime()); + result = DateUtil.between(middle2, period.getFromDate(), period.getThruDate()); + Assert.assertTrue(result); + + // middle before begin + middle = DateUtil.createDate(2, 2, 2009); + result = DateUtil.between(middle, period.getFromDate(), period.getThruDate()); + Assert.assertFalse(result); + } + + /** + * Test of currentPeriod method, of class DateUtil. + */ + //@Test + public void testCurrentPeriod() { + log.info("currentPeriod"); + } + + /** + * Test of getMonth method, of class DateUtil. + */ + //@Test + public void testGetMonth() { + log.info("getMonth"); + } + + @Test + public void testGetDifferenceInSeconds() { + log.info("getDifferenceInSecondes"); + + Date beginDate = DateUtil.createDate(30, 10, 0, 3, 2, 2009); + Date endDate = DateUtil.createDate(0, 11, 0, 3, 2, 2009); + + int result = DateUtil.getDifferenceInSeconds(beginDate, endDate); + Assert.assertEquals(30, result); + + beginDate = DateUtil.createDate(9, 28, 0, 28, 1, 2009); + endDate = DateUtil.createDate(50, 30, 0, 28, 1, 2009); + + result = DateUtil.getDifferenceInSeconds(beginDate, endDate); + Assert.assertEquals(161, result); + } + + @Test + public void testGetDifferenceInMinutes() { + log.info("getDifferenceInMinutes"); + + Date beginDate = DateUtil.createDate(30, 10, 0, 3, 2, 2009); + Date endDate = DateUtil.createDate(0, 12, 0, 3, 2, 2009); + + int result = DateUtil.getDifferenceInMinutes(beginDate, endDate); + Assert.assertEquals(1, result); + + beginDate = DateUtil.createDate(9, 28, 0, 28, 1, 2009); + endDate = DateUtil.createDate(50, 30, 0, 28, 1, 2009); + + result = DateUtil.getDifferenceInMinutes(beginDate, endDate); + Assert.assertEquals(2, result); + } + + @Test + public void testGetDifferenceInHours() { + log.info("getDifferenceInHours"); + + Date beginDate = DateUtil.createDate(30, 10, 0, 3, 2, 2009); + Date endDate = DateUtil.createDate(0, 11, 0, 4, 2, 2009); + + int result = DateUtil.getDifferenceInHours(beginDate, endDate); + Assert.assertEquals(24, result); + + beginDate = DateUtil.createDate(9, 28, 0, 28, 1, 2009); + endDate = DateUtil.createDate(50, 30, 8, 28, 1, 2009); + + result = DateUtil.getDifferenceInHours(beginDate, endDate); + Assert.assertEquals(8, result); + } + + @Test + public void testGetDifferenceInDays() { + log.info("getDifferenceInDays"); + + Date beginDate = DateUtil.createDate(3, 2, 2009); + Date endDate = DateUtil.createDate(8, 2, 2009); + + int result = DateUtil.getDifferenceInDays(beginDate, endDate); + Assert.assertEquals(5, result); + + beginDate = DateUtil.createDate(28, 1, 2009); + endDate = DateUtil.createDate(8, 2, 2009); + + result = DateUtil.getDifferenceInDays(beginDate, endDate); + Assert.assertEquals(11, result); + } + + @Test + public void testGetDifferenceInMonths() { + log.info("getDifferenceInMonths"); + + Date beginDate = DateUtil.createDate(3, 2, 2009); + Date endDate = DateUtil.createDate(8, 8, 2010); + + int result = DateUtil.getDifferenceInMonths(beginDate, endDate); + log.info("result1 : " + result); + Assert.assertEquals(19, result); + + beginDate = DateUtil.createDate(1, 1, 2009); + endDate = DateUtil.createDate(28, 2, 2009); + + result = DateUtil.getDifferenceInMonths(beginDate, endDate); + log.info("result2 : " + result); + Assert.assertEquals(2, result); + + beginDate = DateUtil.createDate(31, 1, 2009); + endDate = DateUtil.createDate(1, 2, 2009); + + result = DateUtil.getDifferenceInMonths(beginDate, endDate); + log.info("result3 : " + result); + Assert.assertEquals(1, result); + } + + @Test + public void testGetMonthLibelle() { + log.info("getMonthLibelle"); + + Locale.setDefault(Locale.FRENCH); + String janvier = DateUtil.getMonthLibelle(1); + Assert.assertEquals("janvier", janvier); + + String juli = DateUtil.getMonthLibelle(7, Locale.GERMAN); + Assert.assertEquals("Juli", juli); + } + +} Modified: trunk/src/test/java/org/nuiton/util/DateUtilsTest.java =================================================================== --- trunk/src/test/java/org/nuiton/util/DateUtilsTest.java 2010-08-29 14:21:26 UTC (rev 1908) +++ trunk/src/test/java/org/nuiton/util/DateUtilsTest.java 2010-08-29 14:34:29 UTC (rev 1909) @@ -42,7 +42,9 @@ /** * * @author fdesbois + * @deprecated since 1.4.1, use now {@link DateUtilTest} */ +@Deprecated public class DateUtilsTest { /** Logger */
participants (1)
-
tchemit@users.nuiton.org