diff --git a/src/java/org/apache/commons/lang/time/DateUtils.java b/src/java/org/apache/commons/lang/time/DateUtils.java index e5138b58d..ac3addbe9 100644 --- a/src/java/org/apache/commons/lang/time/DateUtils.java +++ b/src/java/org/apache/commons/lang/time/DateUtils.java @@ -403,7 +403,129 @@ public class DateUtils { c.add(calendarField, amount); return c.getTime(); } + + //----------------------------------------------------------------------- + /** + * Sets the years field to a date returning a new object. + * The original date object is unchanged. + * + * @param date the date, not null + * @param amount the amount to set + * @return a new Date object set with the specified value + * @throws IllegalArgumentException if the date is null + */ + public static Date setYears(Date date, int amount) { + return set(date, Calendar.YEAR, amount); + } + //----------------------------------------------------------------------- + /** + * Sets the months field to a date returning a new object. + * The original date object is unchanged. + * + * @param date the date, not null + * @param amount the amount to set + * @return a new Date object set with the specified value + * @throws IllegalArgumentException if the date is null + */ + public static Date setMonths(Date date, int amount) { + return set(date, Calendar.MONTH, amount); + } + + //----------------------------------------------------------------------- + /** + * Sets the day of month field to a date returning a new object. + * The original date object is unchanged. + * + * @param date the date, not null + * @param amount the amount to set + * @return a new Date object set with the specified value + * @throws IllegalArgumentException if the date is null + */ + public static Date setDays(Date date, int amount) { + return set(date, Calendar.DAY_OF_MONTH, amount); + } + + //----------------------------------------------------------------------- + /** + * Sets the hours field to a date returning a new object. Hours range + * from 0-23. + * The original date object is unchanged. + * + * @param date the date, not null + * @param amount the amount to set + * @return a new Date object set with the specified value + * @throws IllegalArgumentException if the date is null + */ + public static Date setHours(Date date, int amount) { + return set(date, Calendar.HOUR_OF_DAY, amount); + } + + //----------------------------------------------------------------------- + /** + * Sets the minute field to a date returning a new object. + * The original date object is unchanged. + * + * @param date the date, not null + * @param amount the amount to set + * @return a new Date object set with the specified value + * @throws IllegalArgumentException if the date is null + */ + public static Date setMinutes(Date date, int amount) { + return set(date, Calendar.MINUTE, amount); + } + + //----------------------------------------------------------------------- + /** + * Sets the seconds field to a date returning a new object. + * The original date object is unchanged. + * + * @param date the date, not null + * @param amount the amount to set + * @return a new Date object set with the specified value + * @throws IllegalArgumentException if the date is null + */ + public static Date setSeconds(Date date, int amount) { + return set(date, Calendar.SECOND, amount); + } + + //----------------------------------------------------------------------- + /** + * Sets the miliseconds field to a date returning a new object. + * The original date object is unchanged. + * + * @param date the date, not null + * @param amount the amount to set + * @return a new Date object set with the specified value + * @throws IllegalArgumentException if the date is null + */ + public static Date setMilliseconds(Date date, int amount) { + return set(date, Calendar.MILLISECOND, amount); + } + + //----------------------------------------------------------------------- + /** + * Sets the specified field to a date returning a new object. + * This does not use a lenient calendar. + * The original date object is unchanged. + * + * @param date the date, not null + * @param amount the amount to set + * @return a new Date object set with the specified value + * @throws IllegalArgumentException if the date is null + */ + private static Date set(Date date, int calendarField, int amount) { + if (date == null) { + throw new IllegalArgumentException("The date must not be null"); + } + // getInstance() returns a new object, so this method is thread safe. + Calendar c = Calendar.getInstance(); + c.setLenient(false); + c.setTime(date); + c.set(calendarField, amount); + return c.getTime(); + } + //----------------------------------------------------------------------- /** *

Round this date, leaving the field specified as the most diff --git a/src/test/org/apache/commons/lang/time/DateUtilsTest.java b/src/test/org/apache/commons/lang/time/DateUtilsTest.java index 6ded24d60..1a2d008d1 100644 --- a/src/test/org/apache/commons/lang/time/DateUtilsTest.java +++ b/src/test/org/apache/commons/lang/time/DateUtilsTest.java @@ -455,6 +455,151 @@ public class DateUtilsTest extends TestCase { assertDate(base, 2000, 6, 5, 4, 3, 2, 1); assertDate(result, 1999, 6, 5, 4, 3, 2, 1); } + + // ----------------------------------------------------------------------- + public void testSetYears() throws Exception { + Date base = new Date(MILLIS_TEST); + Date result = DateUtils.setYears(base, 2000); + assertNotSame(base, result); + assertDate(base, 2000, 6, 5, 4, 3, 2, 1); + assertDate(result, 2000, 6, 5, 4, 3, 2, 1); + + result = DateUtils.setYears(base, 2008); + assertNotSame(base, result); + assertDate(base, 2000, 6, 5, 4, 3, 2, 1); + assertDate(result, 2008, 6, 5, 4, 3, 2, 1); + + result = DateUtils.setYears(base, 2005); + assertNotSame(base, result); + assertDate(base, 2000, 6, 5, 4, 3, 2, 1); + assertDate(result, 2005, 6, 5, 4, 3, 2, 1); + } + + // ----------------------------------------------------------------------- + public void testSetMonths() throws Exception { + Date base = new Date(MILLIS_TEST); + Date result = DateUtils.setMonths(base, 5); + assertNotSame(base, result); + assertDate(base, 2000, 6, 5, 4, 3, 2, 1); + assertDate(result, 2000, 5, 5, 4, 3, 2, 1); + + result = DateUtils.setMonths(base, 1); + assertNotSame(base, result); + assertDate(base, 2000, 6, 5, 4, 3, 2, 1); + assertDate(result, 2000, 1, 5, 4, 3, 2, 1); + + try { + result = DateUtils.setMonths(base, 12); + fail("DateUtils.setMonths did not throw an expected IllegalArguementException."); + } catch (IllegalArgumentException e) { + + } + } + + // ----------------------------------------------------------------------- + public void testSetDays() throws Exception { + Date base = new Date(MILLIS_TEST); + Date result = DateUtils.setDays(base, 1); + assertNotSame(base, result); + assertDate(base, 2000, 6, 5, 4, 3, 2, 1); + assertDate(result, 2000, 6, 1, 4, 3, 2, 1); + + result = DateUtils.setDays(base, 29); + assertNotSame(base, result); + assertDate(base, 2000, 6, 5, 4, 3, 2, 1); + assertDate(result, 2000, 6, 29, 4, 3, 2, 1); + + try { + result = DateUtils.setDays(base, 32); + fail("DateUtils.setDays did not throw an expected IllegalArguementException."); + } catch (IllegalArgumentException e) { + + } + } + + // ----------------------------------------------------------------------- + public void testSetHours() throws Exception { + Date base = new Date(MILLIS_TEST); + Date result = DateUtils.setHours(base, 0); + assertNotSame(base, result); + assertDate(base, 2000, 6, 5, 4, 3, 2, 1); + assertDate(result, 2000, 6, 5, 0, 3, 2, 1); + + result = DateUtils.setHours(base, 23); + assertNotSame(base, result); + assertDate(base, 2000, 6, 5, 4, 3, 2, 1); + assertDate(result, 2000, 6, 5, 23, 3, 2, 1); + + try { + result = DateUtils.setHours(base, 24); + fail("DateUtils.setHours did not throw an expected IllegalArguementException."); + } catch (IllegalArgumentException e) { + + } + } + + // ----------------------------------------------------------------------- + public void testSetMinutes() throws Exception { + Date base = new Date(MILLIS_TEST); + Date result = DateUtils.setMinutes(base, 0); + assertNotSame(base, result); + assertDate(base, 2000, 6, 5, 4, 3, 2, 1); + assertDate(result, 2000, 6, 5, 4, 0, 2, 1); + + result = DateUtils.setMinutes(base, 59); + assertNotSame(base, result); + assertDate(base, 2000, 6, 5, 4, 3, 2, 1); + assertDate(result, 2000, 6, 5, 4, 59, 2, 1); + + try { + result = DateUtils.setMinutes(base, 60); + fail("DateUtils.setMinutes did not throw an expected IllegalArguementException."); + } catch (IllegalArgumentException e) { + + } + } + + // ----------------------------------------------------------------------- + public void testSetSeconds() throws Exception { + Date base = new Date(MILLIS_TEST); + Date result = DateUtils.setSeconds(base, 0); + assertNotSame(base, result); + assertDate(base, 2000, 6, 5, 4, 3, 2, 1); + assertDate(result, 2000, 6, 5, 4, 3, 0, 1); + + result = DateUtils.setSeconds(base, 59); + assertNotSame(base, result); + assertDate(base, 2000, 6, 5, 4, 3, 2, 1); + assertDate(result, 2000, 6, 5, 4, 3, 59, 1); + + try { + result = DateUtils.setSeconds(base, 60); + fail("DateUtils.setSeconds did not throw an expected IllegalArguementException."); + } catch (IllegalArgumentException e) { + + } + } + + // ----------------------------------------------------------------------- + public void testSetMilliseconds() throws Exception { + Date base = new Date(MILLIS_TEST); + Date result = DateUtils.setMilliseconds(base, 0); + assertNotSame(base, result); + assertDate(base, 2000, 6, 5, 4, 3, 2, 1); + assertDate(result, 2000, 6, 5, 4, 3, 2, 0); + + result = DateUtils.setMilliseconds(base, 999); + assertNotSame(base, result); + assertDate(base, 2000, 6, 5, 4, 3, 2, 1); + assertDate(result, 2000, 6, 5, 4, 3, 2, 999); + + try { + result = DateUtils.setMilliseconds(base, 1000); + fail("DateUtils.setMilliseconds did not throw an expected IllegalArguementException."); + } catch (IllegalArgumentException e) { + + } + } //----------------------------------------------------------------------- private void assertDate(Date date, int year, int month, int day, int hour, int min, int sec, int mil) throws Exception {