Use locale-appropriate date format for mTitleNoYearDateFormat

Bug: 6383997
Change-Id: I8630123e3c40f4860a0a5039639b6b5a255defec
diff --git a/src/com/android/contacts/datepicker/DatePickerDialog.java b/src/com/android/contacts/datepicker/DatePickerDialog.java
index 14ebd3b..7681898 100644
--- a/src/com/android/contacts/datepicker/DatePickerDialog.java
+++ b/src/com/android/contacts/datepicker/DatePickerDialog.java
@@ -35,7 +35,6 @@
 import com.android.contacts.util.DateUtils;
 
 import java.text.DateFormat;
-import java.text.SimpleDateFormat;
 import java.util.Calendar;
 
 /**
@@ -158,8 +157,7 @@
         mInitialDay = dayOfMonth;
 
         mTitleDateFormat = DateFormat.getDateInstance(DateFormat.FULL);
-        mTitleNoYearDateFormat = new SimpleDateFormat(
-                DateUtils.isMonthBeforeDay(getContext()) ? "MMMM dd" : "dd MMMM");
+        mTitleNoYearDateFormat = DateUtils.getLocalizedDateFormatWithoutYear(getContext());
         updateTitle(mInitialYear, mInitialMonth, mInitialDay);
 
         setButton(BUTTON_POSITIVE, context.getText(com.android.internal.R.string.date_time_set),
diff --git a/src/com/android/contacts/util/DateUtils.java b/src/com/android/contacts/util/DateUtils.java
index 56fcead..72a53a0 100644
--- a/src/com/android/contacts/util/DateUtils.java
+++ b/src/com/android/contacts/util/DateUtils.java
@@ -162,4 +162,30 @@
         }
         return false;
     }
+
+    /**
+     * Returns a SimpleDateFormat object without the year fields by using a regular expression
+     * to eliminate the year in the string pattern. In the rare occurence that the resulting
+     * pattern cannot be reconverted into a SimpleDateFormat, it uses the provided context to
+     * determine whether the month field should be displayed before the day field, and returns
+     * either "MMMM dd" or "dd MMMM" converted into a SimpleDateFormat.
+     */
+    public static SimpleDateFormat getLocalizedDateFormatWithoutYear(Context context) {
+        final String pattern = ((SimpleDateFormat) SimpleDateFormat.getDateInstance(
+                java.text.DateFormat.LONG)).toPattern();
+        // Determine the correct regex pattern for year.
+        // Special case handling for Spanish locale by checking for "de"
+        final String yearPattern = pattern.contains(
+                "de") ? "[^Mm]*[Yy]+[^Mm]*" : "[^DdMm]*[Yy]+[^DdMm]*";
+        try {
+         // Eliminate the substring in pattern that matches the format for that of year
+            return new SimpleDateFormat(pattern.replaceAll(yearPattern, ""));
+        } catch (IllegalArgumentException e) {
+            // In case the new pattern isn't handled by SimpleDateFormat, fall back to the original
+            // method of constructing the SimpleDateFormat, which may not be appropriate for all
+            // locales (i.e. Germany)
+            return new SimpleDateFormat(
+                    DateUtils.isMonthBeforeDay(context) ? "MMMM dd" : "dd MMMM");
+        }
+    }
 }