@Test public void testConvertFlexibleDate() throws ParseException { Assert.assertEquals(mar_16_1989,DateUtil.convertFlexibleDate("03-16-1989")); Assert.assertEquals(mar_16_1989,DateUtil.convertFlexibleDate("03/16/1989")); Assert.assertEquals(jan_01_1901,DateUtil.convertFlexibleDate("1901")); Assert.assertEquals(dec_01_2009,DateUtil.convertFlexibleDate("December 2009")); Assert.assertEquals(jun_30_1932,DateUtil.convertFlexibleDate("June 30, 1932")); Assert.assertEquals(jun_30_1932,DateUtil.convertFlexibleDate("June 30 1932")); Assert.assertEquals(dec_01_2045,DateUtil.convertFlexibleDate("12/2045")); Assert.assertEquals(mar_01_1999,DateUtil.convertFlexibleDate("03-1999")); Assert.assertEquals(mar_01_1999,DateUtil.convertFlexibleDate("1999-03-01")); Assert.assertEquals(jan_02_2001,DateUtil.convertFlexibleDate("2001-01-02")); }
public static Date convertFlexibleDate(String strDate, String[] formats) throws ParseException { if (StringUtil.isEmpty(strDate)) return null; for (int i=0; i<formats.length; i++) { try { SimpleDateFormat dtFormatter = new SimpleDateFormat(formats[i]); dtFormatter.setLenient(false); return dtFormatter.parse(strDate.trim()); } catch (ParseException e) { } } throw new ParseException("No matching date format for "+strDate, 0); }
DateUtil { public static Date convertFlexibleDate(String strDate, String[] formats) throws ParseException { if (StringUtil.isEmpty(strDate)) return null; for (int i=0; i<formats.length; i++) { try { SimpleDateFormat dtFormatter = new SimpleDateFormat(formats[i]); dtFormatter.setLenient(false); return dtFormatter.parse(strDate.trim()); } catch (ParseException e) { } } throw new ParseException("No matching date format for "+strDate, 0); } }
DateUtil { public static Date convertFlexibleDate(String strDate, String[] formats) throws ParseException { if (StringUtil.isEmpty(strDate)) return null; for (int i=0; i<formats.length; i++) { try { SimpleDateFormat dtFormatter = new SimpleDateFormat(formats[i]); dtFormatter.setLenient(false); return dtFormatter.parse(strDate.trim()); } catch (ParseException e) { } } throw new ParseException("No matching date format for "+strDate, 0); } private  DateUtil(); }
DateUtil { public static Date convertFlexibleDate(String strDate, String[] formats) throws ParseException { if (StringUtil.isEmpty(strDate)) return null; for (int i=0; i<formats.length; i++) { try { SimpleDateFormat dtFormatter = new SimpleDateFormat(formats[i]); dtFormatter.setLenient(false); return dtFormatter.parse(strDate.trim()); } catch (ParseException e) { } } throw new ParseException("No matching date format for "+strDate, 0); } private  DateUtil(); static String dateToString(Date obj, String format); static Date stringToDate(String strDate, String format); static Date convertFlexibleDate(String strDate, String[] formats); static String convertShortDate(Date obj); static Date convertShortDate(String str); static Date convertShortDate(String str, Date defaultDate); static Date convertFlexibleDate(String strDate); static boolean compareNullableDates(Date date1, Date date2); static boolean hasTime(Date date); static Date getClientCurrentDate(final Calendar calendar, final TimeZone timeZone); static Date getDateFrom(int count, Date date); static Date getDayOfTheWeek(int day, int week, int year); static Date removeTime(Date date); }
DateUtil { public static Date convertFlexibleDate(String strDate, String[] formats) throws ParseException { if (StringUtil.isEmpty(strDate)) return null; for (int i=0; i<formats.length; i++) { try { SimpleDateFormat dtFormatter = new SimpleDateFormat(formats[i]); dtFormatter.setLenient(false); return dtFormatter.parse(strDate.trim()); } catch (ParseException e) { } } throw new ParseException("No matching date format for "+strDate, 0); } private  DateUtil(); static String dateToString(Date obj, String format); static Date stringToDate(String strDate, String format); static Date convertFlexibleDate(String strDate, String[] formats); static String convertShortDate(Date obj); static Date convertShortDate(String str); static Date convertShortDate(String str, Date defaultDate); static Date convertFlexibleDate(String strDate); static boolean compareNullableDates(Date date1, Date date2); static boolean hasTime(Date date); static Date getClientCurrentDate(final Calendar calendar, final TimeZone timeZone); static Date getDateFrom(int count, Date date); static Date getDayOfTheWeek(int day, int week, int year); static Date removeTime(Date date);  }