ParseUtils { public static Date parseStringAsDate(String in) throws IndexParseFieldException { DateAdapter adapter = getTimezonedTimestampAdaptor(); try { return adapter.parse(in); } catch (ParseException e) { Parser parser = new Parser(TimeZone.getTimeZone("GMT")); List<DateGroup> dg = parser.parse(in); if (dg.isEmpty()) { throw new IndexParseFieldException(in); } List<Date> dates = dg.get(0).getDates(); if (dates.isEmpty()) { throw new IndexParseFieldException(in); } return dates.get(0); } }  static ByteBuffer parseBase64StringAsByteBuffer(String in); static boolean parseStringAsBoolean(String in); static Date parseStringAsDate(String in); static double parseStringAsDouble(String in); static int parseStringAsInt(String in); static UUID parseStringAsUUID(String in); static Long parseStringAsLong(String in);  }
@Test public void testParseStringAsDate() throws Exception { TimeZone.setDefault(TimeZone.getTimeZone("GMT")); System.out.println("parseStringAsDate"); Date testDate = new Date(); String in = testDate.toString(); Date result = ParseUtils.parseStringAsDate(in); assertEquals(testDate.getTime(), result.getTime(), 100l); DateFormat format = DateFormat.getDateInstance(DateFormat.LONG); in = format.format(testDate); result = ParseUtils.parseStringAsDate(in); assertEquals(testDate.getTime(), result.getTime(), 3600l); testDate = new Date(); testDate.setYear(3); testDate.setMonth(11); testDate.setDate(17); testDate.setHours(0); testDate.setMinutes(0); testDate.setSeconds(0); in = "12/17/1903"; result = ParseUtils.parseStringAsDate(in); assertEquals(testDate.getTime(), result.getTime(), 0l); in = "17 Dec 1903 00:00:00"; result = ParseUtils.parseStringAsDate(in); assertEquals(testDate.getTime(), result.getTime(), 0l); SimpleDateFormat simpleDtFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); in = simpleDtFormat.format(testDate); result = ParseUtils.parseStringAsDate(in); assertEquals(testDate.getTime(), result.getTime(), 100l); in = "2015-07-04T23:54:00.000-06:00"; result = ParseUtils.parseStringAsDate(in); assertEquals(simpleDtFormat.parse(in).getTime(), result.getTime(), 100l); in = "2015-07-04T23:54:00-06:00"; simpleDtFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX"); result = ParseUtils.parseStringAsDate(in); assertEquals(simpleDtFormat.parse(in).getTime(), result.getTime(), 100l); in = "2015-07-04T23:54-06:00"; simpleDtFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mmXXX"); result = ParseUtils.parseStringAsDate(in); assertEquals(simpleDtFormat.parse(in).getTime(), result.getTime(), 100l); simpleDtFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"); in = simpleDtFormat.format(testDate); result = ParseUtils.parseStringAsDate(in); assertEquals(testDate.getTime(), result.getTime(), 100l); simpleDtFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); in = simpleDtFormat.format(testDate); result = ParseUtils.parseStringAsDate(in); assertEquals(testDate.getTime(), result.getTime(), 100l); simpleDtFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX"); in = simpleDtFormat.format(testDate); result = ParseUtils.parseStringAsDate(in); assertEquals(testDate.getTime(), result.getTime(), 100l); }