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();  }
@Test public void getDateAsIso8601String() { final Date date_2016_12_22_at_midnight = new Date(1482022800000L); final Date date_2016_12_23_at_midnight = new Date(1482829200000L); final String iso8601Date = "2016-12-22T12:00:00.000Z"; Date ocrDate = null; assertNull(new OcrResponseParser(null).getDate()); assertNull(new OcrResponseParser(new OcrResponse(null, null, null, null, null, null, null, null)).getDate()); assertNull(new OcrResponseParser(new OcrResponse(null, null, null, new OcrResponseField<>(null, 1.0), null, null, null, null)).getDate()); TestTimezoneToggler.setDefaultTimeZone(TimeZone.getTimeZone("America/New_York")); ocrDate = new OcrResponseParser(new OcrResponse(null, null, null, new OcrResponseField<>(iso8601Date, 1.0), null, null, null, null)).getDate(); assertNotNull(ocrDate); assertTrue(ocrDate.after(date_2016_12_22_at_midnight)); assertTrue(ocrDate.before(date_2016_12_23_at_midnight)); assertTrue(getDateAsIso8601String(ocrDate).startsWith("2016-12-22")); TestTimezoneToggler.setDefaultTimeZone(TimeZone.getTimeZone("Europe/London")); ocrDate = new OcrResponseParser(new OcrResponse(null, null, null, new OcrResponseField<>(iso8601Date, 1.0), null, null, null, null)).getDate(); assertNotNull(ocrDate); assertTrue(ocrDate.after(date_2016_12_22_at_midnight)); assertTrue(ocrDate.before(date_2016_12_23_at_midnight)); assertTrue(getDateAsIso8601String(ocrDate).startsWith("2016-12-22")); TestTimezoneToggler.setDefaultTimeZone(TimeZone.getTimeZone("Australia/Sydney")); ocrDate = new OcrResponseParser(new OcrResponse(null, null, null, new OcrResponseField<>(iso8601Date, 1.0), null, null, null, null)).getDate(); assertNotNull(ocrDate); assertTrue(ocrDate.after(date_2016_12_22_at_midnight)); assertTrue(ocrDate.before(date_2016_12_23_at_midnight)); assertTrue(getDateAsIso8601String(ocrDate).startsWith("2016-12-22")); TestTimezoneToggler.setDefaultTimeZone(TimeZone.getTimeZone("Asia/Istanbul")); ocrDate = new OcrResponseParser(new OcrResponse(null, null, null, new OcrResponseField<>(iso8601Date, 1.0), null, null, null, null)).getDate(); assertNotNull(ocrDate); assertTrue(ocrDate.after(date_2016_12_22_at_midnight)); assertTrue(ocrDate.before(date_2016_12_23_at_midnight)); assertTrue(getDateAsIso8601String(ocrDate).startsWith("2016-12-22")); }
@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()); }
@Test public void getDateWithLowConfidence() { final String iso8601Date = "2016-12-22T12:00:00.000Z"; assertNull(new OcrResponseParser(new OcrResponse(null, null, null, new OcrResponseField<>(iso8601Date, 0.4), null, null, null, null)).getDate()); }