@Test public void testParseUIDate() throws Exception { HashMap<String, OffsetDateTime> testCases = new HashMap<>(); testCases.put("3/22/2014" + FilterUtil.TIMEZONE_SEPARATOR + "+0700", OffsetDateTime.of(2014, 3, 22, 0, 0, 0, 0, ZoneOffset.of("+0000"))); testCases.put("03/22/2014" + FilterUtil.TIMEZONE_SEPARATOR + "+0330", OffsetDateTime.of(2014, 3, 22, 0, 0, 0, 0, ZoneOffset.of("+0000"))); testCases.put("4/2/2005" + FilterUtil.TIMEZONE_SEPARATOR + "-1000", OffsetDateTime.of(2005, 4, 2, 0, 0, 0, 0, ZoneOffset.of("+0000"))); testCases.put("12/02/2014" + FilterUtil.TIMEZONE_SEPARATOR + "-0700", OffsetDateTime.of(2014, 12, 2, 0, 0, 0, 0, ZoneOffset.of("+0000"))); for (Map.Entry<String, OffsetDateTime> testCase : testCases.entrySet()) { String[] extracted = FilterUtil.extractTimeZone(testCase.getKey()); Assert.assertEquals("Date not correctly parsed.", testCase.getValue(), FilterUtil.parseUIDate(extracted[0], extracted[1])); } testCases.clear(); testCases.put("some string" + FilterUtil.TIMEZONE_SEPARATOR + "+0000", OffsetDateTime.now()); testCases.put("5/4/12" + FilterUtil.TIMEZONE_SEPARATOR + "+0000", OffsetDateTime.now()); testCases.put("5/4/412" + FilterUtil.TIMEZONE_SEPARATOR + "+0000", OffsetDateTime.now()); testCases.put("1/0/12" + FilterUtil.TIMEZONE_SEPARATOR + "+0000", OffsetDateTime.now()); testCases.put("1 OffsetDateTime.now()); testCases.put(" OffsetDateTime.now()); for (Map.Entry<String, OffsetDateTime> testCase : testCases.entrySet()) { String[] extracted = FilterUtil.extractTimeZone(testCase.getKey()); try { OffsetDateTime result = FilterUtil.parseUIDate(extracted[0], extracted[1]); Assert.fail("No exception was thrown for " + testCase.getKey() + ", got: " + result); } catch (PaginationException ignored) { } catch (Exception exception) { Assert.fail("Got unexpected exception for " + testCase.getKey() + ": " + exception); } } }
public static OffsetDateTime parseUIDate(String dateString, String timeZoneOffset) { try { TemporalAccessor tempAccessor = DateTimeFormatter.ofPattern("M/d/yyyyZ").parse(dateString + timeZoneOffset); return OffsetDateTime.of(java.time.LocalDate.from(tempAccessor), LocalTime.MIDNIGHT, ZoneOffset.UTC); } catch (DateTimeParseException parseException) { throw new PaginationException(ErrorCode.FILTER_KEY_UNPROCESSABLE, "Wrong format: Can not parse date (" + dateString + ") , must be of " + "format MM/dd/yyyy , e.g. 05/23/2014 or 4/7/2013", parseException); } }
FilterUtil { public static OffsetDateTime parseUIDate(String dateString, String timeZoneOffset) { try { TemporalAccessor tempAccessor = DateTimeFormatter.ofPattern("M/d/yyyyZ").parse(dateString + timeZoneOffset); return OffsetDateTime.of(java.time.LocalDate.from(tempAccessor), LocalTime.MIDNIGHT, ZoneOffset.UTC); } catch (DateTimeParseException parseException) { throw new PaginationException(ErrorCode.FILTER_KEY_UNPROCESSABLE, "Wrong format: Can not parse date (" + dateString + ") , must be of " + "format MM/dd/yyyy , e.g. 05/23/2014 or 4/7/2013", parseException); } } }
FilterUtil { public static OffsetDateTime parseUIDate(String dateString, String timeZoneOffset) { try { TemporalAccessor tempAccessor = DateTimeFormatter.ofPattern("M/d/yyyyZ").parse(dateString + timeZoneOffset); return OffsetDateTime.of(java.time.LocalDate.from(tempAccessor), LocalTime.MIDNIGHT, ZoneOffset.UTC); } catch (DateTimeParseException parseException) { throw new PaginationException(ErrorCode.FILTER_KEY_UNPROCESSABLE, "Wrong format: Can not parse date (" + dateString + ") , must be of " + "format MM/dd/yyyy , e.g. 05/23/2014 or 4/7/2013", parseException); } }  }
FilterUtil { public static OffsetDateTime parseUIDate(String dateString, String timeZoneOffset) { try { TemporalAccessor tempAccessor = DateTimeFormatter.ofPattern("M/d/yyyyZ").parse(dateString + timeZoneOffset); return OffsetDateTime.of(java.time.LocalDate.from(tempAccessor), LocalTime.MIDNIGHT, ZoneOffset.UTC); } catch (DateTimeParseException parseException) { throw new PaginationException(ErrorCode.FILTER_KEY_UNPROCESSABLE, "Wrong format: Can not parse date (" + dateString + ") , must be of " + "format MM/dd/yyyy , e.g. 05/23/2014 or 4/7/2013", parseException); } }  static boolean extractTimeZoneAndTestDate(Date date, String filter); static OffsetDateTime convertDateToOffsetDateTime(Date date); static String[] extractTimeZone(String filter); static OffsetDateTime parseUIDate(String dateString, String timeZoneOffset); }
FilterUtil { public static OffsetDateTime parseUIDate(String dateString, String timeZoneOffset) { try { TemporalAccessor tempAccessor = DateTimeFormatter.ofPattern("M/d/yyyyZ").parse(dateString + timeZoneOffset); return OffsetDateTime.of(java.time.LocalDate.from(tempAccessor), LocalTime.MIDNIGHT, ZoneOffset.UTC); } catch (DateTimeParseException parseException) { throw new PaginationException(ErrorCode.FILTER_KEY_UNPROCESSABLE, "Wrong format: Can not parse date (" + dateString + ") , must be of " + "format MM/dd/yyyy , e.g. 05/23/2014 or 4/7/2013", parseException); } }  static boolean extractTimeZoneAndTestDate(Date date, String filter); static OffsetDateTime convertDateToOffsetDateTime(Date date); static String[] extractTimeZone(String filter); static OffsetDateTime parseUIDate(String dateString, String timeZoneOffset);  }