Commits

Anonymous committed 79d6134

Added support for RFC 3339 date format in the XWorkBasicConvertor.java.
Updated OgnlUtilTest.java to include a test for it.

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@1321e221344d-f017-0410-9bd5-d282ab1896d7

Comments (0)

Files changed (2)

src/java/com/opensymphony/xwork2/util/XWorkBasicConverter.java

 public class XWorkBasicConverter extends DefaultTypeConverter {
 
     private static String MILLISECOND_FORMAT = ".SSS";
-
+    final private static SimpleDateFormat RFC3399_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
     public Object convertValue(Map context, Object o, Member member, String s, Object value, Class toType) {
         Object result = null;
 
             } else if (!"".equals(value)) { // we've already tried the types we know
                 result = super.convertValue(context, value, toType);
             }
-            
+
             if (result == null && value != null && !"".equals(value)) {
                 throw new XWorkException("Cannot create type " + toType + " from value " + value);
             }
                 SimpleDateFormat d1 = (SimpleDateFormat)DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG, locale);
                 SimpleDateFormat d2 = (SimpleDateFormat)DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, locale);
                 SimpleDateFormat d3 = (SimpleDateFormat)DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale);
-                SimpleDateFormat[] dfs = {d1, d2, d3};
+                SimpleDateFormat[] dfs = {d1, d2, d3, RFC3399_FORMAT}; //added RFC 3339 date format (XW-473)
                 for (int i = 0; i < dfs.length; i++) {
                 	try {
                 		check = dfs[i].parse(sa);
                 		if (check != null) {
                 			break;
                 		}
-                	} 
+                	}
                 	catch (ParseException ignore) {
                 	}
                 }
             }
             //final fallback for dates without time
             if (df == null){
-            	df = DateFormat.getDateInstance(DateFormat.SHORT, locale); 
+            	df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
             }
             try {
             	df.setLenient(false); // let's use strict parsing (XW-341)
                 }
                 numFormat.setGroupingUsed(true);
                 Number number = numFormat.parse(stringValue, parsePos);
-                
+
                 if (parsePos.getIndex() != stringValue.length()) {
                     throw new XWorkException("Unparseable number: \"" + stringValue + "\" at position "
                             + parsePos.getIndex());
                 || char.class == type || Character.class == type) {
             return false;
         }
-        
+
         return true;
     }
-    
+
     private String doConvertToString(Map context, Object value) {
         String result = null;
 

src/test/com/opensymphony/xwork2/util/OgnlUtilTest.java

 import ognl.*;
 
 import java.lang.reflect.Method;
+import java.text.*;
 import java.util.*;
 
 
         Locale.setDefault(orig);
          
         Locale.setDefault(orig);
+        
+        //test RFC 3339 date format for JSON
+        props.put("event", "1996-12-19T16:39:57Z");
+        OgnlUtil.setProperties(props, foo, context);
+        
+        cal = Calendar.getInstance();
+        cal.clear();
+        cal.set(Calendar.MONTH, Calendar.DECEMBER);
+        cal.set(Calendar.DAY_OF_MONTH, 19);
+        cal.set(Calendar.YEAR, 1996);
+        cal.set(Calendar.HOUR_OF_DAY, 16);
+        cal.set(Calendar.MINUTE, 39);
+        cal.set(Calendar.SECOND, 57);
+        
+        assertEquals(cal.getTime(), foo.getEvent());
     }
 
     public void testSetPropertiesInt() {
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.