HeaderHelper { public static Date parseDateHeader(String header) { for (SimpleDateFormat format : DateFormats.formats) { try { return ((SimpleDateFormat)format.clone()).parse(header); } catch (ParseException ignored) { } } throw new IllegalArgumentException(String.format("Not found appropriated date format for %s", header)); } private  HeaderHelper(); static List<AcceptMediaType> createAcceptMediaTypeList(String header); static List<AcceptLanguage> createAcceptedLanguageList(String header); static List<AcceptToken> createAcceptedCharsetList(String header); static List<AcceptToken> createAcceptedEncodingList(String header); static List<Cookie> parseCookies(String cookiesString); static NewCookie parseNewCookie(String newCookieString); static Date parseDateHeader(String header); static String formatDate(Date date); static long getContentLengthLong(MultivaluedMap<String, String> httpHeaders); @SuppressWarnings({"unchecked"}) static String getHeaderAsString(Object o); static String convertToString(Collection<String> collection); static final Comparator<QualityValue> QUALITY_VALUE_COMPARATOR; }
@Test public void parsesANSI() { String dateHeader = "THU DEC 29 12:03:50 2011"; Date expectedDate = date(2011, 12, 29, 12, 3, 50, "GMT"); Date date = HeaderHelper.parseDateHeader(dateHeader); assertTrue(String.format("Dates are not equal. Expected %s, actual %s", expectedDate, date), Math.abs(expectedDate.getTime() - date.getTime()) < 1000); }
@Test public void parsesRFC_1036() { String dateHeader = "Thursday, 29-Dec-11 12:03:50 EST"; Date expectedDate = date(2011, 12, 29, 12, 3, 50, "EST"); Date date = HeaderHelper.parseDateHeader(dateHeader); assertTrue(String.format("Dates are not equal, %s and %s", expectedDate, date), Math.abs(expectedDate.getTime() - date.getTime()) < 1000); }
@Test public void parsesRFC_1123() { String dateHeader = "Thu, 29 Dec 2011 12:03:50 GMT"; Date expectedDate = date(2011, 12, 29, 12, 3, 50, "GMT"); Date date = HeaderHelper.parseDateHeader(dateHeader); assertTrue(String.format("Dates are not equal, %s and %s", expectedDate, date), Math.abs(expectedDate.getTime() - date.getTime()) < 1000); }
@Test public void failsParseDateIfItDoesNotMatchToAnySupportedFormat() { String dateHeader = "12:03:50 GMT"; thrown.expect(IllegalArgumentException.class); HeaderHelper.parseDateHeader(dateHeader); }