Author: reto
Date: Mon Feb 14 19:01:24 2011
New Revision: 1070607

URL: http://svn.apache.org/viewvc?rev=1070607&view=rev
Log:
CLEREZZA-419: serializing date objects indepemdemtly of local time zone in UTC 
Z-notation

Modified:
    
incubator/clerezza/trunk/parent/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/util/W3CDateFormat.java
    
incubator/clerezza/trunk/parent/rdf.core/src/test/java/org/apache/clerezza/rdf/core/impl/util/W3CDateFormatTest.java

Modified: 
incubator/clerezza/trunk/parent/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/util/W3CDateFormat.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/parent/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/util/W3CDateFormat.java?rev=1070607&r1=1070606&r2=1070607&view=diff
==============================================================================
--- 
incubator/clerezza/trunk/parent/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/util/W3CDateFormat.java
 (original)
+++ 
incubator/clerezza/trunk/parent/rdf.core/src/main/java/org/apache/clerezza/rdf/core/impl/util/W3CDateFormat.java
 Mon Feb 14 19:01:24 2011
@@ -38,6 +38,17 @@ public class W3CDateFormat extends DateF
 
        private static final TimeZone utcTZ = new SimpleTimeZone(0, "UTC");
 
+       static {
+               dateFormatWithMillis.setTimeZone(utcTZ);
+               dateFormatNoMillis.setTimeZone(utcTZ);
+       }
+
+       @Override
+       public void setTimeZone(TimeZone zone) {
+               super.setTimeZone(zone);
+       }
+
+
        /**
         * @see java.text.DateFormat#format(java.util.Date, 
java.lang.StringBuffer,
         *      java.text.FieldPosition)
@@ -49,9 +60,15 @@ public class W3CDateFormat extends DateF
                final DateFormat dateFormat = (date.getTime() % 1000) == 0 ?
                        dateFormatNoMillis : dateFormatWithMillis;
                String string = dateFormat.format(date);
-               StringBuffer result = new StringBuffer(string);
-               result.insert(string.length() - 2, ':');
-               return result;
+               if (string.endsWith("0000")) {
+                       StringBuffer result = new 
StringBuffer(string.substring(0, string.length()-5));
+                       result.append('Z');
+                       return result;
+               } else {
+                       StringBuffer result = new StringBuffer(string);
+                       result.insert(string.length() - 2, ':');
+                       return result;
+               }
        }
 
        /**

Modified: 
incubator/clerezza/trunk/parent/rdf.core/src/test/java/org/apache/clerezza/rdf/core/impl/util/W3CDateFormatTest.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/parent/rdf.core/src/test/java/org/apache/clerezza/rdf/core/impl/util/W3CDateFormatTest.java?rev=1070607&r1=1070606&r2=1070607&view=diff
==============================================================================
--- 
incubator/clerezza/trunk/parent/rdf.core/src/test/java/org/apache/clerezza/rdf/core/impl/util/W3CDateFormatTest.java
 (original)
+++ 
incubator/clerezza/trunk/parent/rdf.core/src/test/java/org/apache/clerezza/rdf/core/impl/util/W3CDateFormatTest.java
 Mon Feb 14 19:01:24 2011
@@ -50,6 +50,15 @@ public class W3CDateFormatTest {
        }
 
        @Test
+       public void dateObjectSerializedWithoutTimeZone() throws Exception {
+               Calendar calendar = new GregorianCalendar(2009, 0, 1, 1, 33, 
58);
+               calendar.setTimeZone(TimeZone.getTimeZone("GMT+7:00"));
+               Date date = calendar.getTime();
+               String serializedDate = new W3CDateFormat().format(date);
+               assertEquals("2008-12-31T18:33:58Z", serializedDate);
+       }
+
+       @Test
        public void roundTrip() throws Exception {
                Calendar calendar = new GregorianCalendar(2009, 0, 1,
                                1, 33, 58);


Reply via email to