From 2fc13087e700ee044684adb99e7114c9f477c67f Mon Sep 17 00:00:00 2001 From: Jay Shrauner Date: Fri, 2 May 2014 16:17:17 -0700 Subject: [PATCH] Parse abbreviated date fields Add support for abbreviated dates with no timestamp (eg, "2009-01-02") to parseEmailDateTimeToMillis. Bug:14496986 Change-Id: Ifc77cb75fd9e23536b48c8f6ecefc0e2e8f1cc2c --- .../src/com/android/emailcommon/utility/Utility.java | 7 ++++++- tests/src/com/android/emailcommon/utility/UtilityTest.java | 4 +++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/emailcommon/src/com/android/emailcommon/utility/Utility.java b/emailcommon/src/com/android/emailcommon/utility/Utility.java index 6ef1d8421..592d03c15 100644 --- a/emailcommon/src/com/android/emailcommon/utility/Utility.java +++ b/emailcommon/src/com/android/emailcommon/utility/Utility.java @@ -263,6 +263,9 @@ public class Utility { return cal; } + private static final ThreadLocalDateFormat mAbbrevEmailDateTimeFormat = + new ThreadLocalDateFormat("yyyy-MM-dd"); + private static final ThreadLocalDateFormat mEmailDateTimeFormat = new ThreadLocalDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); @@ -277,7 +280,9 @@ public class Utility { @VisibleForTesting public static long parseEmailDateTimeToMillis(String date) throws ParseException { final GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT")); - if (date.length() <= 20) { + if (date.length() <= 10) { + cal.setTime(mAbbrevEmailDateTimeFormat.parse(date)); + } else if (date.length() <= 20) { cal.setTime(mEmailDateTimeFormat.parse(date)); } else { cal.setTime(mEmailDateTimeFormatWithMillis.parse(date)); diff --git a/tests/src/com/android/emailcommon/utility/UtilityTest.java b/tests/src/com/android/emailcommon/utility/UtilityTest.java index 3d08244b4..c5b0e7f77 100644 --- a/tests/src/com/android/emailcommon/utility/UtilityTest.java +++ b/tests/src/com/android/emailcommon/utility/UtilityTest.java @@ -63,8 +63,10 @@ public class UtilityTest extends TestCase { 2010, 2, 23, 16, 1, 5, 0); testParseEmailDateTimeHelper("2009-02-11T18:03:31.123Z", 2009, 2, 11, 18, 3, 31, 123); + testParseEmailDateTimeHelper("2009-02-11", + 2009, 2, 11, 0, 0, 0, 0); try { - testParseEmailDateTimeHelper("2010-02-23", 1970, 1, 1, 0, 0, 0, 0); + testParseEmailDateTimeHelper("2010-02", 1970, 1, 1, 0, 0, 0, 0); fail("Expected ParseException"); } catch (ParseException e) { // expected