@Test public void getDateForBadString() { final String iso8601Date = "badDate"; assertNull(new OcrResponseParser(new OcrResponse(null, null, null, new OcrResponseField<>(iso8601Date, 1.0), null, null, null, null)).getDate()); }
@Nullable public Date getDate() { if (ocrResponse != null && ocrResponse.getDate() != null && ocrResponse.getDate().getData() != null && ocrResponse.getDate().getConfidenceLevel() > MINIMUM_DATE_CONFIDENCE) { try { final SimpleDateFormat iso8601Formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US); final GregorianCalendar ocrResponseCalendar = new GregorianCalendar(); ocrResponseCalendar.setTime(iso8601Formatter.parse(ocrResponse.getDate().getData())); ocrResponseCalendar.set(Calendar.HOUR_OF_DAY, 0); ocrResponseCalendar.set(Calendar.MINUTE, 0); ocrResponseCalendar.set(Calendar.SECOND, 0); ocrResponseCalendar.set(Calendar.MILLISECOND, 0); final GregorianCalendar localTimeCalendar = new GregorianCalendar(); localTimeCalendar.setTime(new Date(System.currentTimeMillis())); ocrResponseCalendar.add(Calendar.HOUR_OF_DAY, localTimeCalendar.get(Calendar.HOUR_OF_DAY)); ocrResponseCalendar.add(Calendar.MINUTE, localTimeCalendar.get(Calendar.MINUTE)); ocrResponseCalendar.add(Calendar.SECOND, localTimeCalendar.get(Calendar.SECOND)); ocrResponseCalendar.add(Calendar.MILLISECOND, localTimeCalendar.get(Calendar.MILLISECOND)); return new Date(ocrResponseCalendar.getTime().getTime()); } catch (ParseException e) { Logger.error(this, "Failed to parse OCR Date.", e); } } return null; }
OcrResponseParser { @Nullable public Date getDate() { if (ocrResponse != null && ocrResponse.getDate() != null && ocrResponse.getDate().getData() != null && ocrResponse.getDate().getConfidenceLevel() > MINIMUM_DATE_CONFIDENCE) { try { final SimpleDateFormat iso8601Formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US); final GregorianCalendar ocrResponseCalendar = new GregorianCalendar(); ocrResponseCalendar.setTime(iso8601Formatter.parse(ocrResponse.getDate().getData())); ocrResponseCalendar.set(Calendar.HOUR_OF_DAY, 0); ocrResponseCalendar.set(Calendar.MINUTE, 0); ocrResponseCalendar.set(Calendar.SECOND, 0); ocrResponseCalendar.set(Calendar.MILLISECOND, 0); final GregorianCalendar localTimeCalendar = new GregorianCalendar(); localTimeCalendar.setTime(new Date(System.currentTimeMillis())); ocrResponseCalendar.add(Calendar.HOUR_OF_DAY, localTimeCalendar.get(Calendar.HOUR_OF_DAY)); ocrResponseCalendar.add(Calendar.MINUTE, localTimeCalendar.get(Calendar.MINUTE)); ocrResponseCalendar.add(Calendar.SECOND, localTimeCalendar.get(Calendar.SECOND)); ocrResponseCalendar.add(Calendar.MILLISECOND, localTimeCalendar.get(Calendar.MILLISECOND)); return new Date(ocrResponseCalendar.getTime().getTime()); } catch (ParseException e) { Logger.error(this, "Failed to parse OCR Date.", e); } } return null; } }
OcrResponseParser { @Nullable public Date getDate() { if (ocrResponse != null && ocrResponse.getDate() != null && ocrResponse.getDate().getData() != null && ocrResponse.getDate().getConfidenceLevel() > MINIMUM_DATE_CONFIDENCE) { try { final SimpleDateFormat iso8601Formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US); final GregorianCalendar ocrResponseCalendar = new GregorianCalendar(); ocrResponseCalendar.setTime(iso8601Formatter.parse(ocrResponse.getDate().getData())); ocrResponseCalendar.set(Calendar.HOUR_OF_DAY, 0); ocrResponseCalendar.set(Calendar.MINUTE, 0); ocrResponseCalendar.set(Calendar.SECOND, 0); ocrResponseCalendar.set(Calendar.MILLISECOND, 0); final GregorianCalendar localTimeCalendar = new GregorianCalendar(); localTimeCalendar.setTime(new Date(System.currentTimeMillis())); ocrResponseCalendar.add(Calendar.HOUR_OF_DAY, localTimeCalendar.get(Calendar.HOUR_OF_DAY)); ocrResponseCalendar.add(Calendar.MINUTE, localTimeCalendar.get(Calendar.MINUTE)); ocrResponseCalendar.add(Calendar.SECOND, localTimeCalendar.get(Calendar.SECOND)); ocrResponseCalendar.add(Calendar.MILLISECOND, localTimeCalendar.get(Calendar.MILLISECOND)); return new Date(ocrResponseCalendar.getTime().getTime()); } catch (ParseException e) { Logger.error(this, "Failed to parse OCR Date.", e); } } return null; } OcrResponseParser(@Nullable OcrResponse ocrResponse); }
OcrResponseParser { @Nullable public Date getDate() { if (ocrResponse != null && ocrResponse.getDate() != null && ocrResponse.getDate().getData() != null && ocrResponse.getDate().getConfidenceLevel() > MINIMUM_DATE_CONFIDENCE) { try { final SimpleDateFormat iso8601Formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US); final GregorianCalendar ocrResponseCalendar = new GregorianCalendar(); ocrResponseCalendar.setTime(iso8601Formatter.parse(ocrResponse.getDate().getData())); ocrResponseCalendar.set(Calendar.HOUR_OF_DAY, 0); ocrResponseCalendar.set(Calendar.MINUTE, 0); ocrResponseCalendar.set(Calendar.SECOND, 0); ocrResponseCalendar.set(Calendar.MILLISECOND, 0); final GregorianCalendar localTimeCalendar = new GregorianCalendar(); localTimeCalendar.setTime(new Date(System.currentTimeMillis())); ocrResponseCalendar.add(Calendar.HOUR_OF_DAY, localTimeCalendar.get(Calendar.HOUR_OF_DAY)); ocrResponseCalendar.add(Calendar.MINUTE, localTimeCalendar.get(Calendar.MINUTE)); ocrResponseCalendar.add(Calendar.SECOND, localTimeCalendar.get(Calendar.SECOND)); ocrResponseCalendar.add(Calendar.MILLISECOND, localTimeCalendar.get(Calendar.MILLISECOND)); return new Date(ocrResponseCalendar.getTime().getTime()); } catch (ParseException e) { Logger.error(this, "Failed to parse OCR Date.", e); } } return null; } OcrResponseParser(@Nullable OcrResponse ocrResponse); @Nullable String getMerchant(); @Nullable String getTotalAmount(); @Nullable String getTaxAmount(); @Nullable Date getDate(); }
OcrResponseParser { @Nullable public Date getDate() { if (ocrResponse != null && ocrResponse.getDate() != null && ocrResponse.getDate().getData() != null && ocrResponse.getDate().getConfidenceLevel() > MINIMUM_DATE_CONFIDENCE) { try { final SimpleDateFormat iso8601Formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US); final GregorianCalendar ocrResponseCalendar = new GregorianCalendar(); ocrResponseCalendar.setTime(iso8601Formatter.parse(ocrResponse.getDate().getData())); ocrResponseCalendar.set(Calendar.HOUR_OF_DAY, 0); ocrResponseCalendar.set(Calendar.MINUTE, 0); ocrResponseCalendar.set(Calendar.SECOND, 0); ocrResponseCalendar.set(Calendar.MILLISECOND, 0); final GregorianCalendar localTimeCalendar = new GregorianCalendar(); localTimeCalendar.setTime(new Date(System.currentTimeMillis())); ocrResponseCalendar.add(Calendar.HOUR_OF_DAY, localTimeCalendar.get(Calendar.HOUR_OF_DAY)); ocrResponseCalendar.add(Calendar.MINUTE, localTimeCalendar.get(Calendar.MINUTE)); ocrResponseCalendar.add(Calendar.SECOND, localTimeCalendar.get(Calendar.SECOND)); ocrResponseCalendar.add(Calendar.MILLISECOND, localTimeCalendar.get(Calendar.MILLISECOND)); return new Date(ocrResponseCalendar.getTime().getTime()); } catch (ParseException e) { Logger.error(this, "Failed to parse OCR Date.", e); } } return null; } OcrResponseParser(@Nullable OcrResponse ocrResponse); @Nullable String getMerchant(); @Nullable String getTotalAmount(); @Nullable String getTaxAmount(); @Nullable Date getDate();  }