commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1516571 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration/convert/DefaultConversionHandler.java test/java/org/apache/commons/configuration/convert/TestDefaultConversionHandler.java
Date Thu, 22 Aug 2013 19:58:05 GMT
Author: oheger
Date: Thu Aug 22 19:58:04 2013
New Revision: 1516571

URL: http://svn.apache.org/r1516571
Log:
Added a property for the date format to DefaultConversionHandler.

This format string specified here will be used for conversions to Date or
Calendar objects.

Modified:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/convert/DefaultConversionHandler.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/convert/TestDefaultConversionHandler.java

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/convert/DefaultConversionHandler.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/convert/DefaultConversionHandler.java?rev=1516571&r1=1516570&r2=1516571&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/convert/DefaultConversionHandler.java
(original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/convert/DefaultConversionHandler.java
Thu Aug 22 19:58:04 2013
@@ -50,6 +50,9 @@ import org.apache.commons.lang3.ClassUti
  */
 public class DefaultConversionHandler implements ConversionHandler
 {
+    /** The default format for dates. */
+    public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
+
     /** A helper object used for extracting values from complex objects. */
     private static final AbstractListDelimiterHandler EXTRACTOR =
             (AbstractListDelimiterHandler) DisabledListDelimiterHandler.INSTANCE;
@@ -68,6 +71,35 @@ public class DefaultConversionHandler im
                 };
             };
 
+    /** The current date format. */
+    private volatile String dateFormat;
+
+    /**
+     * Returns the date format used by this conversion handler.
+     *
+     * @return the date format
+     */
+    public String getDateFormat()
+    {
+        String fmt = dateFormat;
+        return (fmt != null) ? fmt : DEFAULT_DATE_FORMAT;
+    }
+
+    /**
+     * Sets the date format to be used by this conversion handler. This format
+     * is applied by conversions to {@code Date} or {@code Calendar} objects.
+     * The string is passed to the {@code java.text.SimpleDateFormat} class, so
+     * it must be compatible with this class. If no date format has been set, a
+     * default format is used.
+     *
+     * @param dateFormat the date format string
+     * @see #DEFAULT_DATE_FORMAT
+     */
+    public void setDateFormat(String dateFormat)
+    {
+        this.dateFormat = dateFormat;
+    }
+
     public <T> T to(Object src, Class<T> targetCls, ConfigurationInterpolator
ci)
     {
         ConfigurationInterpolator interpolator = fetchInterpolator(ci);

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/convert/TestDefaultConversionHandler.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/convert/TestDefaultConversionHandler.java?rev=1516571&r1=1516570&r2=1516571&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/convert/TestDefaultConversionHandler.java
(original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/convert/TestDefaultConversionHandler.java
Thu Aug 22 19:58:04 2013
@@ -283,4 +283,27 @@ public class TestDefaultConversionHandle
         assertEquals("Wrong element (2)", Integer.parseInt(src.get(1)),
                 array[1]);
     }
+
+    /**
+     * Tests whether the default date format is used if no format has been set.
+     */
+    @Test
+    public void testGetDateFormatNotSet()
+    {
+        assertEquals("Wrong date format",
+                DefaultConversionHandler.DEFAULT_DATE_FORMAT,
+                handler.getDateFormat());
+    }
+
+    /**
+     * Tests whether the date format can be changed.
+     */
+    @Test
+    public void testSetDateFormat()
+    {
+        String dateFormat = "dd.mm.yyyy";
+        handler.setDateFormat(dateFormat);
+        assertEquals("Date format not changed", dateFormat,
+                handler.getDateFormat());
+    }
 }



Mime
View raw message