This is an automated email from the ASF dual-hosted git repository.

radu pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-scripting-sightly.git


The following commit(s) were added to refs/heads/master by this push:
     new c6c68f4  SLING-9983 - Add support for predefined date format styles
c6c68f4 is described below

commit c6c68f447eb407d90443382a643b60245bc66e08
Author: Konrad Windszus <[email protected]>
AuthorDate: Fri Jan 22 09:48:35 2021 +0100

    SLING-9983 - Add support for predefined date format styles
---
 .../engine/extension/FormatFilterExtension.java    | 35 +++++++++++++++++++---
 1 file changed, 31 insertions(+), 4 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/FormatFilterExtension.java
 
b/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/FormatFilterExtension.java
index f98df20..06527cf 100644
--- 
a/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/FormatFilterExtension.java
+++ 
b/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/FormatFilterExtension.java
@@ -18,6 +18,7 @@
  
******************************************************************************/
 package org.apache.sling.scripting.sightly.impl.engine.extension;
 
+import java.text.DateFormat;
 import java.text.DecimalFormat;
 import java.text.DecimalFormatSymbols;
 import java.text.NumberFormat;
@@ -175,16 +176,42 @@ public class FormatFilterExtension implements 
RuntimeExtension {
         return "";
     }
 
+    private int getPredefinedFormattingStyleFromValue(String value) {
+        switch (value.toLowerCase(Locale.ROOT)) {
+            case "default":
+                return DateFormat.DEFAULT;
+            case "short":
+                return DateFormat.SHORT;
+            case "medium":
+                return DateFormat.MEDIUM;
+            case "long":
+                return DateFormat.LONG;
+            case "full":
+                return DateFormat.FULL;
+            default:
+                return -1;
+        }
+    }
+
     private String formatDate(String format, Date date, Locale locale, 
TimeZone timezone) {
         if (date == null) {
             return null;
         }
         try {
-            SimpleDateFormat formatter;
-            if (locale != null) {
-                formatter = new SimpleDateFormat(format, locale);
+            final DateFormat formatter;
+            int formattingStyle = 
getPredefinedFormattingStyleFromValue(format);
+            if (formattingStyle != -1) {
+                if (locale != null) {
+                    formatter = DateFormat.getDateInstance(formattingStyle, 
locale);
+                } else {
+                    formatter = DateFormat.getDateInstance(formattingStyle);
+                }
             } else {
-                formatter = new SimpleDateFormat(format);
+                if (locale != null) {
+                    formatter = new SimpleDateFormat(format, locale);
+                } else {
+                    formatter = new SimpleDateFormat(format);
+                }
             }
             if (timezone != null) {
                 formatter.setTimeZone(timezone);

Reply via email to