Set time zone on date formats.

Also fix Locale to use en-US for storing dates

Bug:3502565

Change-Id: Id3728de2d2f2c77ad1f27e6acbd71a91d840b3a6
diff --git a/src/com/android/contacts/editor/EventFieldEditorView.java b/src/com/android/contacts/editor/EventFieldEditorView.java
index 7345dbb..906dbad 100644
--- a/src/com/android/contacts/editor/EventFieldEditorView.java
+++ b/src/com/android/contacts/editor/EventFieldEditorView.java
@@ -40,6 +40,7 @@
 import java.text.ParsePosition;
 import java.util.Calendar;
 import java.util.Date;
+import java.util.Locale;
 
 /**
  * Editor that allows editing Events using a {@link DatePickerDialog}
@@ -175,7 +176,7 @@
         final String oldValue = getEntry().getAsString(column);
         final DataKind kind = getKind();
 
-        final Calendar calendar = Calendar.getInstance();
+        final Calendar calendar = Calendar.getInstance(DateUtils.UTC_TIMEZONE, Locale.US);
         final int defaultYear = calendar.get(Calendar.YEAR);
 
         // Check whether the year is optional
@@ -206,7 +207,7 @@
         final String oldValue = getEntry().getAsString(column);
         final DataKind kind = getKind();
 
-        final Calendar calendar = Calendar.getInstance();
+        final Calendar calendar = Calendar.getInstance(DateUtils.UTC_TIMEZONE, Locale.US);
         final int defaultYear = calendar.get(Calendar.YEAR);
 
         // Check whether the year is optional
@@ -245,7 +246,8 @@
             @Override
             public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                 if (year == 0 && !isYearOptional) throw new IllegalStateException();
-                final Calendar outCalendar = Calendar.getInstance();
+                final Calendar outCalendar =
+                        Calendar.getInstance(DateUtils.UTC_TIMEZONE, Locale.US);
 
                 // If no year specified, set it to 1900. The format string will ignore that year
                 // For formats other than Exchange, the time of the day is ignored
diff --git a/src/com/android/contacts/util/DateUtils.java b/src/com/android/contacts/util/DateUtils.java
index 40570f0..ed9eb94 100644
--- a/src/com/android/contacts/util/DateUtils.java
+++ b/src/com/android/contacts/util/DateUtils.java
@@ -22,40 +22,51 @@
 import java.text.ParsePosition;
 import java.text.SimpleDateFormat;
 import java.util.Date;
+import java.util.Locale;
+import java.util.TimeZone;
 
 /**
  * Utility methods for processing dates.
  */
 public class DateUtils {
-    public static final SimpleDateFormat NO_YEAR_DATE_FORMAT = new SimpleDateFormat("--MM-dd");
-    public static final SimpleDateFormat FULL_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
+    public static final TimeZone UTC_TIMEZONE = TimeZone.getTimeZone("UTC");
+
+    // All the SimpleDateFormats in this class use the UTC timezone
+    public static final SimpleDateFormat NO_YEAR_DATE_FORMAT =
+            new SimpleDateFormat("--MM-dd", Locale.US);
+    public static final SimpleDateFormat FULL_DATE_FORMAT =
+            new SimpleDateFormat("yyyy-MM-dd", Locale.US);
     public static final SimpleDateFormat DATE_AND_TIME_FORMAT =
-            new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
+            new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
 
     // Variations of ISO 8601 date format.  Do not change the order - it does affect the
     // result in ambiguous cases.
     private static final SimpleDateFormat[] DATE_FORMATS = {
         FULL_DATE_FORMAT,
         DATE_AND_TIME_FORMAT,
-        new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'"),
-        new SimpleDateFormat("yyyyMMdd"),
-        new SimpleDateFormat("yyyyMMdd'T'HHmmssSSS'Z'"),
-        new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'"),
-        new SimpleDateFormat("yyyyMMdd'T'HHmm'Z'"),
+        new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'", Locale.US),
+        new SimpleDateFormat("yyyyMMdd", Locale.US),
+        new SimpleDateFormat("yyyyMMdd'T'HHmmssSSS'Z'", Locale.US),
+        new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'", Locale.US),
+        new SimpleDateFormat("yyyyMMdd'T'HHmm'Z'", Locale.US),
     };
 
-    static {
-        for (SimpleDateFormat format : DATE_FORMATS) {
-            format.setLenient(true);
-        }
-    }
-
     private static final java.text.DateFormat FORMAT_WITHOUT_YEAR_MONTH_FIRST =
             new SimpleDateFormat("MMMM dd");
 
     private static final java.text.DateFormat FORMAT_WITHOUT_YEAR_DATE_FIRST =
             new SimpleDateFormat("dd MMMM");
 
+    static {
+        for (SimpleDateFormat format : DATE_FORMATS) {
+            format.setLenient(true);
+            format.setTimeZone(UTC_TIMEZONE);
+        }
+        NO_YEAR_DATE_FORMAT.setTimeZone(UTC_TIMEZONE);
+        FORMAT_WITHOUT_YEAR_MONTH_FIRST.setTimeZone(UTC_TIMEZONE);
+        FORMAT_WITHOUT_YEAR_DATE_FIRST.setTimeZone(UTC_TIMEZONE);
+    }
+
     /**
      * Parses the supplied string to see if it looks like a date. If so,
      * returns the date.  Otherwise, returns null.
@@ -77,7 +88,7 @@
 
     /**
      * Parses the supplied string to see if it looks like a date. If so,
-     * returns the same date in a cleaned-up format.  Otherwise, returns
+     * returns the same date in a cleaned-up format for the user.  Otherwise, returns
      * the supplied string unchanged.
      */
     public static String formatDate(Context context, String string) {
@@ -114,9 +125,8 @@
                 date = f.parse(string, parsePosition);
                 if (parsePosition.getIndex() == string.length()) {
                     java.text.DateFormat outFormat = DateFormat.getDateFormat(context);
-                    synchronized (outFormat) {
-                        return outFormat.format(date);
-                    }
+                    outFormat.setTimeZone(UTC_TIMEZONE);
+                    return outFormat.format(date);
                 }
             }
         }