Index: test/src/org/ametys/runtime/test/util/DateConversionTestCase.java =================================================================== --- test/src/org/ametys/runtime/test/util/DateConversionTestCase.java (revision 20552) +++ test/src/org/ametys/runtime/test/util/DateConversionTestCase.java (working copy) @@ -15,6 +15,7 @@ */ package org.ametys.runtime.test.util; +import java.util.Calendar; import java.util.Date; import junit.framework.TestCase; @@ -65,4 +66,41 @@ // Date formatting using current time zone assertEquals(ISODateTimeFormat.dateTime().print(new DateTime(dateValue)), ParameterHelper.valueToString(date)); } + + /** + * Tests value to string conversion for a recent date + * @throws Exception if an error occurs. + */ + public void testRecentDate() throws Exception + { + String expected = ISODateTimeFormat.dateTime().print(new DateTime(2012, 3, 2, 18, 30, 22, 666)); + + Calendar cal = Calendar.getInstance(); + cal.clear(); + cal.set(2012, 2, 2, 18, 30, 22); + cal.set(Calendar.MILLISECOND, 666); + Date date = cal.getTime(); + + // Date parsing + String actual = ParameterHelper.valueToString(date); + assertEquals(expected, actual); + } + + /** + * Tests value to string conversion for date older than 1900 + * @throws Exception if an error occurs. + */ + public void testOldDate() throws Exception + { + String expected = ISODateTimeFormat.dateTime().print(new DateTime(1899, 6, 18, 0, 0, 0, 0)); + + Calendar cal = Calendar.getInstance(); + cal.clear(); + cal.set(1899, 5, 18, 0, 0, 0); + Date date = cal.getTime(); + + // Date parsing + String actual = ParameterHelper.valueToString(date); + assertEquals(expected, actual); + } } Index: main/kernel/src/org/ametys/runtime/util/parameter/ParameterHelper.java =================================================================== --- main/kernel/src/org/ametys/runtime/util/parameter/ParameterHelper.java (revision 20552) +++ main/kernel/src/org/ametys/runtime/util/parameter/ParameterHelper.java (working copy) @@ -16,7 +16,9 @@ package org.ametys.runtime.util.parameter; import java.io.InputStream; +import java.util.Calendar; import java.util.Date; +import java.util.GregorianCalendar; import java.util.Map; import org.apache.avalon.framework.logger.Logger; @@ -24,6 +26,7 @@ import org.apache.cocoon.xml.AttributesImpl; import org.apache.cocoon.xml.XMLUtils; import org.joda.time.DateTime; +import org.joda.time.DateTimeZone; import org.joda.time.format.ISODateTimeFormat; import org.xml.sax.ContentHandler; import org.xml.sax.SAXException; @@ -38,7 +41,7 @@ public final class ParameterHelper { /** Enumeration of supported types */ - public static enum ParameterType + public static enum ParameterType { /** boolean values */ BOOLEAN, @@ -80,7 +83,7 @@ * Convert a string containing a type to its value * * @param type Name of the type - * @return Type + * @return Type * @throws IllegalArgumentException if the type is unknown */ public static ParameterType stringToType(String type) @@ -163,7 +166,7 @@ * @return String readable by the config bean * @throws IllegalArgumentException if the object is a InputStream */ - public static String valueToString(Object value) + public static String valueToString(Object value) { if (value == null) { @@ -172,7 +175,19 @@ if (value instanceof Date) { - return ISODateTimeFormat.dateTime().print(new DateTime(value)); + GregorianCalendar calendar = new GregorianCalendar(); + calendar.setTime((Date) value); + calendar.setTimeZone(DateTimeZone.getDefault().toTimeZone()); + + DateTime date = new DateTime(calendar.get(Calendar.YEAR), + calendar.get(Calendar.MONTH) + 1, + calendar.get(Calendar.DAY_OF_MONTH), + calendar.get(Calendar.HOUR_OF_DAY), + calendar.get(Calendar.MINUTE), + calendar.get(Calendar.SECOND), + calendar.get(Calendar.MILLISECOND)); + + return ISODateTimeFormat.dateTime().print(date); } if (value instanceof InputStream) @@ -290,7 +305,7 @@ catch (Exception e) { throw new ProcessingException("Unable to enumerate entries with enumerator: " + enumerator, e); - } + } XMLUtils.endElement(handler, "enumeration"); }