]> source.dussan.org Git - vaadin-framework.git/commitdiff
Use correct day names when formatting dates (#6207)
authorArtur Signell <artur@vaadin.com>
Mon, 3 Jun 2013 09:18:57 +0000 (12:18 +0300)
committerArtur Signell <artur@vaadin.com>
Tue, 4 Jun 2013 13:52:03 +0000 (16:52 +0300)
Change-Id: I2010f87ef4f9359cdc95104cc02c83355a8630ed

client/src/com/vaadin/client/DateTimeService.java
uitest/src/com/vaadin/tests/components/datefield/CustomDateFormats.java

index 53e8f81a38727cbd33f6d1e9ee6a674216f89d85..5fd0574d3e724a9af131c578ba443227b951782c 100644 (file)
@@ -308,10 +308,11 @@ public class DateTimeService {
      */
     public String formatDate(Date date, String formatStr) {
         /*
-         * Format month names separately when locale for the DateTimeService is
-         * not the same as the browser locale
+         * Format month and day names separately when locale for the
+         * DateTimeService is not the same as the browser locale
          */
         formatStr = formatMonthNames(date, formatStr);
+        formatStr = formatDayNames(date, formatStr);
 
         // Format uses the browser locale
         DateTimeFormat format = DateTimeFormat.getFormat(formatStr);
@@ -321,6 +322,49 @@ public class DateTimeService {
         return result;
     }
 
+    private String formatDayNames(Date date, String formatStr) {
+        if (formatStr.contains("EEEE")) {
+            String dayName = getDay(date.getDay());
+
+            if (dayName != null) {
+                /*
+                 * Replace 4 or more E:s with the quoted day name. Also
+                 * concatenate generated string with any other string prepending
+                 * or following the EEEE pattern, i.e. 'EEEE'ta ' becomes 'DAYta
+                 * ' and not 'DAY''ta ', 'ab'EEEE becomes 'abDAY', 'x'EEEE'y'
+                 * becomes 'xDAYy'.
+                 */
+                formatStr = formatStr.replaceAll("'([E]{4,})'", dayName);
+                formatStr = formatStr.replaceAll("([E]{4,})'", "'" + dayName);
+                formatStr = formatStr.replaceAll("'([E]{4,})", dayName + "'");
+                formatStr = formatStr
+                        .replaceAll("[E]{4,}", "'" + dayName + "'");
+            }
+        }
+
+        if (formatStr.contains("EEE")) {
+
+            String dayName = getShortDay(date.getMonth());
+
+            if (dayName != null) {
+                /*
+                 * Replace 3 or more E:s with the quoted month name. Also
+                 * concatenate generated string with any other string prepending
+                 * or following the EEE pattern, i.e. 'EEE'ta ' becomes 'DAYta '
+                 * and not 'DAY''ta ', 'ab'EEE becomes 'abDAY', 'x'EEE'y'
+                 * becomes 'xDAYy'.
+                 */
+                formatStr = formatStr.replaceAll("'([E]{3,})'", dayName);
+                formatStr = formatStr.replaceAll("([E]{3,})'", "'" + dayName);
+                formatStr = formatStr.replaceAll("'([E]{3,})", dayName + "'");
+                formatStr = formatStr
+                        .replaceAll("[E]{3,}", "'" + dayName + "'");
+            }
+        }
+
+        return formatStr;
+    }
+
     private String formatMonthNames(Date date, String formatStr) {
         if (formatStr.contains("MMMM")) {
             String monthName = getMonth(date.getMonth());
index 247478256f55310d1ddd40e532ace4afa70d2d25..015a974b3e7a107869d2183d90174da99a48f79d 100644 (file)
@@ -104,6 +104,7 @@ public class CustomDateFormats extends TestBase {
                 locale);
         addDateField(gridLayout, getDatePattern(locale, DateFormat.SHORT),
                 locale);
+        addDateField(gridLayout, "EEE d MMMM yyyy", locale);
 
     }