From 1ea2e9f399ac7678d2d49e6d1dfbe0bd251360de Mon Sep 17 00:00:00 2001 From: srowen Date: Thu, 7 Jan 2010 09:18:12 +0000 Subject: [PATCH] Improvement from issue 309 git-svn-id: https://zxing.googlecode.com/svn/trunk@1180 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- .../result/AddressBookResultHandler.java | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/android/src/com/google/zxing/client/android/result/AddressBookResultHandler.java b/android/src/com/google/zxing/client/android/result/AddressBookResultHandler.java index 83c74fd2b..004abd566 100644 --- a/android/src/com/google/zxing/client/android/result/AddressBookResultHandler.java +++ b/android/src/com/google/zxing/client/android/result/AddressBookResultHandler.java @@ -37,7 +37,13 @@ import java.util.Date; * @author dswitkin@google.com (Daniel Switkin) */ public final class AddressBookResultHandler extends ResultHandler { - private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd"); + + private static final DateFormat[] DATE_FORMATS = { + new SimpleDateFormat("yyyyMMdd"), + new SimpleDateFormat("yyyyMMdd'T'HHmmss"), + new SimpleDateFormat("yyyy-MM-dd"), + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"), + }; private final boolean[] fields; private int buttonCount; @@ -134,6 +140,19 @@ public final class AddressBookResultHandler extends ResultHandler { } } + private static Date parseDate(String s) { + for (DateFormat currentFomat : DATE_FORMATS) { + synchronized (currentFomat) { + currentFomat.setLenient(false); + Date result = currentFomat.parse(s, new ParsePosition(0)); + if (result != null) { + return result; + } + } + } + return null; + } + // Overriden so we can hyphenate phone numbers, format birthdays, and bold the name. @Override public CharSequence getDisplayContents() { @@ -163,11 +182,10 @@ public final class AddressBookResultHandler extends ResultHandler { String birthday = result.getBirthday(); if (birthday != null && birthday.length() > 0) { - Date date; - synchronized (DATE_FORMAT) { - date = DATE_FORMAT.parse(birthday, new ParsePosition(0)); + Date date = parseDate(birthday); + if (date != null) { + ParsedResult.maybeAppend(DateFormat.getDateInstance().format(date.getTime()), contents); } - ParsedResult.maybeAppend(DateFormat.getDateInstance().format(date.getTime()), contents); } ParsedResult.maybeAppend(result.getNote(), contents);