sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1495594 - /sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jdk8/JDK8.java
Date Fri, 21 Jun 2013 20:48:51 GMT
Author: desruisseaux
Date: Fri Jun 21 20:48:50 2013
New Revision: 1495594

URL: http://svn.apache.org/r1495594
Log:
Add an internal method (to be removed on the JDK8 branch) to be needed later for some XML
elements.

Modified:
    sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jdk8/JDK8.java

Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jdk8/JDK8.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jdk8/JDK8.java?rev=1495594&r1=1495593&r2=1495594&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jdk8/JDK8.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jdk8/JDK8.java
[UTF-8] Fri Jun 21 20:48:50 2013
@@ -17,6 +17,11 @@
 package org.apache.sis.internal.jdk8;
 
 import java.util.Date;
+import java.util.Calendar;
+import java.util.GregorianCalendar;
+import java.util.Locale;
+import java.util.TimeZone;
+import java.util.concurrent.atomic.AtomicReference;
 import javax.xml.bind.DatatypeConverter;
 import org.apache.sis.util.CharSequences;
 
@@ -32,6 +37,13 @@ import org.apache.sis.util.CharSequences
  */
 public final class JDK8 {
     /**
+     * A shared Gregorian calendar to use for {@link #printDateTime(Date)}.
+     * We share a single instance instead than using {@link ThreadLocal} instances
+     * on the assumption that usages of this calendar will be relatively rare.
+     */
+    private static final AtomicReference<Calendar> CALENDAR = new AtomicReference<>();
+
+    /**
      * Do not allow instantiation of this class.
      */
     private JDK8() {
@@ -135,4 +147,29 @@ public final class JDK8 {
         }
         return null;
     }
+
+    /**
+     * Formats a date value in a string, assuming UTC timezone and US locale.
+     * This method should be used only for occasional formatting.
+     *
+     * <p>This method will be replaced by {@link java.time.format.DateTimeFormatter}
on the JDK8 branch.</p>
+     *
+     * @param  date The date to format, or {@code null}.
+     * @return The formatted date, or {@code null} if the given date was null.
+     *
+     * @see DatatypeConverter#printDateTime(Calendar)
+     */
+    public static String printDateTime(final Date date) {
+        if (date == null) {
+            return null;
+        }
+        Calendar calendar = CALENDAR.getAndSet(null);
+        if (calendar == null) {
+            calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"), Locale.US);
+        }
+        calendar.setTime(date);
+        final String text = DatatypeConverter.printDateTime(calendar);
+        CALENDAR.set(calendar); // Recycle for future usage.
+        return text;
+    }
 }



Mime
View raw message