Improvement from issue 309

git-svn-id: https://zxing.googlecode.com/svn/trunk@1180 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2010-01-07 09:18:12 +00:00
parent 46a7febc7f
commit 1ea2e9f399

View file

@ -37,7 +37,13 @@ import java.util.Date;
* @author dswitkin@google.com (Daniel Switkin) * @author dswitkin@google.com (Daniel Switkin)
*/ */
public final class AddressBookResultHandler extends ResultHandler { 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 final boolean[] fields;
private int buttonCount; 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. // Overriden so we can hyphenate phone numbers, format birthdays, and bold the name.
@Override @Override
public CharSequence getDisplayContents() { public CharSequence getDisplayContents() {
@ -163,11 +182,10 @@ public final class AddressBookResultHandler extends ResultHandler {
String birthday = result.getBirthday(); String birthday = result.getBirthday();
if (birthday != null && birthday.length() > 0) { if (birthday != null && birthday.length() > 0) {
Date date; Date date = parseDate(birthday);
synchronized (DATE_FORMAT) { if (date != null) {
date = DATE_FORMAT.parse(birthday, new ParsePosition(0)); ParsedResult.maybeAppend(DateFormat.getDateInstance().format(date.getTime()), contents);
} }
ParsedResult.maybeAppend(DateFormat.getDateInstance().format(date.getTime()), contents);
} }
ParsedResult.maybeAppend(result.getNote(), contents); ParsedResult.maybeAppend(result.getNote(), contents);