Author: davidb
Date: Wed Aug  9 14:34:31 2017
New Revision: 1804532

URL: http://svn.apache.org/viewvc?rev=1804532&view=rev
Log:
Implement ISO8601 date conversions using Java 7 compatible code.

Modified:
    
felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/ConverterImpl.java
    
felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java

Modified: 
felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/ConverterImpl.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/ConverterImpl.java?rev=1804532&r1=1804531&r2=1804532&view=diff
==============================================================================
--- 
felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/ConverterImpl.java
 (original)
+++ 
felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/ConverterImpl.java
 Wed Aug  9 14:34:31 2017
@@ -17,6 +17,8 @@
 package org.apache.felix.converter.impl;
 
 import java.lang.reflect.Method;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.UUID;
@@ -30,6 +32,8 @@ import org.osgi.util.converter.TypeRule;
 import org.osgi.util.function.Function;
 
 public class ConverterImpl implements InternalConverter {
+    private final SimpleDateFormat ISO8601_DATE_FORMAT = new 
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
+
     @Override
     public InternalConverting convert(Object obj) {
         return new ConvertingImpl(this, obj);
@@ -45,7 +49,7 @@ public class ConverterImpl implements In
         cb.rule(new Rule<Calendar, String>(new Function<Calendar, String>() {
             @Override
             public String apply(Calendar f) {
-                return f.getTime()./* toInstant(). */toString();
+                return ISO8601_DATE_FORMAT.format(f.getTime());
             }
         }) {});
 
@@ -57,9 +61,13 @@ public class ConverterImpl implements In
         cb.rule(new Rule<String, Calendar>(new Function<String, Calendar>() {
             @Override
             public Calendar apply(String f) {
-                Calendar cc = Calendar.getInstance();
-                //cc.setTime(Date.from(Instant.parse(f)));
-                return cc;
+                try {
+                    Calendar cc = Calendar.getInstance();
+                    cc.setTime(ISO8601_DATE_FORMAT.parse(f));
+                    return cc;
+                } catch (ParseException e) {
+                    throw new ConversionException("Cannot convert " + f + " to 
Date", e);
+                }
             }
         }) {});
 
@@ -146,8 +154,8 @@ public class ConverterImpl implements In
 //        cb.rule(new Rule<Date,String>(f -> f.toInstant().toString()) {});
         cb.rule(new Rule<Date, String>(new Function<Date, String>() {
             @Override
-            public String apply(Date f) {
-                return null; // f.toInstant().toString()
+            public String apply(Date d) {
+                return ISO8601_DATE_FORMAT.format(d);
             }
         }) {});
 
@@ -155,7 +163,11 @@ public class ConverterImpl implements In
         cb.rule(new Rule<String, Date>(new Function<String, Date>() {
             @Override
             public Date apply(String f) {
-                return null; // Date.from(Instant.parse(f))
+                try {
+                    return ISO8601_DATE_FORMAT.parse(f);
+                } catch (ParseException e) {
+                    throw new ConversionException("Cannot convert " + f + " to 
Date", e);
+                }
             }
         }) {});
 

Modified: 
felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java?rev=1804532&r1=1804531&r2=1804532&view=diff
==============================================================================
--- 
felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java
 (original)
+++ 
felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java
 Wed Aug  9 14:34:31 2017
@@ -44,6 +44,7 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.TimeZone;
 import java.util.UUID;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -54,7 +55,6 @@ import org.apache.felix.converter.impl.M
 import org.apache.felix.converter.impl.MyEmbeddedDTO.Alpha;
 import org.junit.After;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
 import org.osgi.util.converter.ConversionException;
 import org.osgi.util.converter.Converter;
@@ -503,18 +503,22 @@ public class ConverterTest {
     }
 
     @Test
-    @Ignore("Code needs to be converted to Java 7")
     public void testCalendarDate() {
-        Calendar cal = new GregorianCalendar(2017, 1, 13);
+        Calendar cal = new GregorianCalendar(1971, 1, 13, 12, 37, 41);
+        TimeZone tz =TimeZone.getTimeZone("CET");
+        cal.setTimeZone(tz);
         Date d = cal.getTime();
 
         Converter c = converter;
 
         String s = c.convert(d).toString();
+        assertEquals("1971-02-13T12:37:41+01:00", s);
         assertEquals(d, c.convert(s).to(Date.class));
 
         String s2 = c.convert(cal).toString();
-        assertEquals(cal, c.convert(s2).to(Calendar.class));
+        assertEquals("1971-02-13T12:37:41+01:00", s2);
+        Calendar cal2 = c.convert(s2).to(Calendar.class);
+        assertEquals(cal.getTime(), cal2.getTime());
     }
 
     @Test


Reply via email to