Skip to content

Commit

Permalink
Prevent invalid dates from throwing an error on Android
Browse files Browse the repository at this point in the history
fix #4757
  • Loading branch information
EvanBacon committed Jan 7, 2020
1 parent b8e06d2 commit 2a39db5
Showing 1 changed file with 30 additions and 26 deletions.
Expand Up @@ -384,34 +384,38 @@ public Bundle toMap(Set<String> fieldSet) throws ParseException {
SimpleDateFormat datePattern = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
SimpleDateFormat noYearPattern = new SimpleDateFormat("--MM-dd", Locale.getDefault());

ArrayList<Bundle> datesArray = new ArrayList();
for (BaseModel item : dates) {
Bundle details = new Bundle();
String dateString = item.getData();
String label = item.getLabel();

hasYear = !dateString.startsWith("--");

if (hasYear) {
calendar.setTime(datePattern.parse(dateString));
} else {
calendar.setTime(noYearPattern.parse(dateString));
}

if (hasYear) {
details.putInt("year", calendar.get(Calendar.YEAR));
}
details.putInt("month", calendar.get(Calendar.MONTH));
details.putInt("day", calendar.get(Calendar.DAY_OF_MONTH));
// TODO: Evan: The type is only supported in 26+
details.putString("format", "gregorian");
if (showBirthday && label != null && label.equals("birthday")) {
contact.putBundle("birthday", details);
} else {
details.putString("label", label);
datesArray.add(details);
ArrayList<Bundle> datesArray = new ArrayList();
try {
for (BaseModel item : dates) {
Bundle details = new Bundle();
String dateString = item.getData();
String label = item.getLabel();

hasYear = !dateString.startsWith("--");

if (hasYear) {
calendar.setTime(datePattern.parse(dateString));
} else {
calendar.setTime(noYearPattern.parse(dateString));
}

if (hasYear) {
details.putInt("year", calendar.get(Calendar.YEAR));
}
details.putInt("month", calendar.get(Calendar.MONTH));
details.putInt("day", calendar.get(Calendar.DAY_OF_MONTH));
// TODO: Evan: The type is only supported in 26+
details.putString("format", "gregorian");
if (showBirthday && label != null && label.equals("birthday")) {
contact.putBundle("birthday", details);
} else {
details.putString("label", label);
datesArray.add(details);
}
}
} catch (Exception e) {
}

if (showDates && datesArray.size() > 0) {
contact.putParcelableArrayList("dates", datesArray);
}
Expand Down

0 comments on commit 2a39db5

Please sign in to comment.