@Test public void testFormatDateTimeAsUI() throws Exception { HashMap<String, String> testCases = new HashMap<>(); testCases.put("+0000", "Feb 28, 2015 03:54:35 AM"); testCases.put("-0700", "Feb 27, 2015 20:54:35 PM"); testCases.put("+0700", "Feb 28, 2015 10:54:35 AM"); testCases.put("+1030", "Feb 28, 2015 14:24:35 PM"); OffsetDateTime date = FilterUtil.convertDateToOffsetDateTime(testCalendar.getTime()); for (Map.Entry<String, String> testCase : testCases.entrySet()) { Assert.assertEquals("Date not formatted correctly.", testCase.getValue(), FilterUtil.formatDateTimeAsUI(date, testCase.getKey())); } try { FilterUtil.formatDateTimeAsUI(date, " 0200"); Assert.fail("Was able to format incorrect timezone."); } catch (PaginationException expected) { } }
static String formatDateTimeAsUI(OffsetDateTime date, String timeZoneOffset) { try { return date.format(DateTimeFormatter.ofPattern("MMM d, YYYY HH:mm:ss a") .withZone(ZoneId.ofOffset("UTC", ZoneOffset.of(timeZoneOffset)))); } catch (DateTimeException dateTimeException) { throw new PaginationException(ErrorCode.FILTER_KEY_UNPROCESSABLE, "Wrong format: Can not parse timezone '" + timeZoneOffset + "' or date " + date.toString() + " properly.", dateTimeException); } }
FilterUtil { static String formatDateTimeAsUI(OffsetDateTime date, String timeZoneOffset) { try { return date.format(DateTimeFormatter.ofPattern("MMM d, YYYY HH:mm:ss a") .withZone(ZoneId.ofOffset("UTC", ZoneOffset.of(timeZoneOffset)))); } catch (DateTimeException dateTimeException) { throw new PaginationException(ErrorCode.FILTER_KEY_UNPROCESSABLE, "Wrong format: Can not parse timezone '" + timeZoneOffset + "' or date " + date.toString() + " properly.", dateTimeException); } } }
FilterUtil { static String formatDateTimeAsUI(OffsetDateTime date, String timeZoneOffset) { try { return date.format(DateTimeFormatter.ofPattern("MMM d, YYYY HH:mm:ss a") .withZone(ZoneId.ofOffset("UTC", ZoneOffset.of(timeZoneOffset)))); } catch (DateTimeException dateTimeException) { throw new PaginationException(ErrorCode.FILTER_KEY_UNPROCESSABLE, "Wrong format: Can not parse timezone '" + timeZoneOffset + "' or date " + date.toString() + " properly.", dateTimeException); } }  }
FilterUtil { static String formatDateTimeAsUI(OffsetDateTime date, String timeZoneOffset) { try { return date.format(DateTimeFormatter.ofPattern("MMM d, YYYY HH:mm:ss a") .withZone(ZoneId.ofOffset("UTC", ZoneOffset.of(timeZoneOffset)))); } catch (DateTimeException dateTimeException) { throw new PaginationException(ErrorCode.FILTER_KEY_UNPROCESSABLE, "Wrong format: Can not parse timezone '" + timeZoneOffset + "' or date " + date.toString() + " properly.", dateTimeException); } }  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 { static String formatDateTimeAsUI(OffsetDateTime date, String timeZoneOffset) { try { return date.format(DateTimeFormatter.ofPattern("MMM d, YYYY HH:mm:ss a") .withZone(ZoneId.ofOffset("UTC", ZoneOffset.of(timeZoneOffset)))); } catch (DateTimeException dateTimeException) { throw new PaginationException(ErrorCode.FILTER_KEY_UNPROCESSABLE, "Wrong format: Can not parse timezone '" + timeZoneOffset + "' or date " + date.toString() + " properly.", dateTimeException); } }  static boolean extractTimeZoneAndTestDate(Date date, String filter); static OffsetDateTime convertDateToOffsetDateTime(Date date); static String[] extractTimeZone(String filter); static OffsetDateTime parseUIDate(String dateString, String timeZoneOffset);  }