Parse abbreviated date fields

Add support for abbreviated dates with no timestamp (eg, "2009-01-02") to
parseEmailDateTimeToMillis.

Bug:14496986
Change-Id: Ifc77cb75fd9e23536b48c8f6ecefc0e2e8f1cc2c
This commit is contained in:
Jay Shrauner 2014-05-02 16:17:17 -07:00
parent f21e6a8983
commit 2fc13087e7
2 changed files with 9 additions and 2 deletions

View File

@ -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));

View File

@ -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