@Test public void dateConvertTest() throws Exception { for (Map.Entry<String, String> entry : asserts.entrySet()){ Date expected = new SimpleDateFormat(entry.getValue()).parse(entry.getKey()); assertEquals(expected, FilterConverterHelper.dateConvert(entry.getKey())); } }
public static Date dateConvert(String value) { if (value == null) { return null; } Map<String, String> formats = new HashMap<>(); formats.put("dd.MM.yyyy HH:mm", "^\\d{2}\\.\\d{2}\\.\\d{4}\\s+\\d{2}:\\d{2}$"); formats.put("dd-MM-yyyy HH:mm", "^\\d{2}-\\d{2}-\\d{4}\\s+\\d{2}:\\d{2}$"); formats.put("dd/MM/yyyy HH:mm", "^\\d{2}/\\d{2}/\\d{4}\\s+\\d{2}:\\d{2}$"); formats.put("yyyy/dd/MM HH:mm", "^\\d{4}/\\d{2}/\\d{2}\\s+\\d{2}:\\d{2}$"); formats.put("yyyy.MM.dd HH:mm", "^\\d{4}\\.\\d{2}\\.\\d{2}\\s+\\d{2}:\\d{2}$"); formats.put("yyyy-MM-dd HH:mm", "^\\d{4}-\\d{2}-\\d{2}\\s+\\d{2}:\\d{2}$"); formats.put("dd.MM.yyyy", "^\\d{2}\\.\\d{2}\\.\\d{4}$"); formats.put("dd-MM-yyyy", "^\\d{2}-\\d{2}-\\d{4}$"); formats.put("dd/MM/yyyy", "^\\d{2}/\\d{2}/\\d{4}$"); formats.put("yyyy/dd/MM", "^\\d{4}/\\d{2}/\\d{2}$"); formats.put("yyyy.MM.dd", "^\\d{4}\\.\\d{2}\\.\\d{2}$"); formats.put("yyyy-MM-dd", "^\\d{4}-\\d{2}-\\d{2}$"); for (Map.Entry<String, String> format : formats.entrySet()) { try { if (value.trim().matches(format.getValue())) { Date result = new SimpleDateFormat(format.getKey()).parse(value.trim()); if (logger.isDebugEnabled()) { logger.debug("Matched format " + format +" for " + value); } return result; } } catch (ParseException ex) { } } return null; }
FilterConverterHelper { public static Date dateConvert(String value) { if (value == null) { return null; } Map<String, String> formats = new HashMap<>(); formats.put("dd.MM.yyyy HH:mm", "^\\d{2}\\.\\d{2}\\.\\d{4}\\s+\\d{2}:\\d{2}$"); formats.put("dd-MM-yyyy HH:mm", "^\\d{2}-\\d{2}-\\d{4}\\s+\\d{2}:\\d{2}$"); formats.put("dd/MM/yyyy HH:mm", "^\\d{2}/\\d{2}/\\d{4}\\s+\\d{2}:\\d{2}$"); formats.put("yyyy/dd/MM HH:mm", "^\\d{4}/\\d{2}/\\d{2}\\s+\\d{2}:\\d{2}$"); formats.put("yyyy.MM.dd HH:mm", "^\\d{4}\\.\\d{2}\\.\\d{2}\\s+\\d{2}:\\d{2}$"); formats.put("yyyy-MM-dd HH:mm", "^\\d{4}-\\d{2}-\\d{2}\\s+\\d{2}:\\d{2}$"); formats.put("dd.MM.yyyy", "^\\d{2}\\.\\d{2}\\.\\d{4}$"); formats.put("dd-MM-yyyy", "^\\d{2}-\\d{2}-\\d{4}$"); formats.put("dd/MM/yyyy", "^\\d{2}/\\d{2}/\\d{4}$"); formats.put("yyyy/dd/MM", "^\\d{4}/\\d{2}/\\d{2}$"); formats.put("yyyy.MM.dd", "^\\d{4}\\.\\d{2}\\.\\d{2}$"); formats.put("yyyy-MM-dd", "^\\d{4}-\\d{2}-\\d{2}$"); for (Map.Entry<String, String> format : formats.entrySet()) { try { if (value.trim().matches(format.getValue())) { Date result = new SimpleDateFormat(format.getKey()).parse(value.trim()); if (logger.isDebugEnabled()) { logger.debug("Matched format " + format +" for " + value); } return result; } } catch (ParseException ex) { } } return null; } }
FilterConverterHelper { public static Date dateConvert(String value) { if (value == null) { return null; } Map<String, String> formats = new HashMap<>(); formats.put("dd.MM.yyyy HH:mm", "^\\d{2}\\.\\d{2}\\.\\d{4}\\s+\\d{2}:\\d{2}$"); formats.put("dd-MM-yyyy HH:mm", "^\\d{2}-\\d{2}-\\d{4}\\s+\\d{2}:\\d{2}$"); formats.put("dd/MM/yyyy HH:mm", "^\\d{2}/\\d{2}/\\d{4}\\s+\\d{2}:\\d{2}$"); formats.put("yyyy/dd/MM HH:mm", "^\\d{4}/\\d{2}/\\d{2}\\s+\\d{2}:\\d{2}$"); formats.put("yyyy.MM.dd HH:mm", "^\\d{4}\\.\\d{2}\\.\\d{2}\\s+\\d{2}:\\d{2}$"); formats.put("yyyy-MM-dd HH:mm", "^\\d{4}-\\d{2}-\\d{2}\\s+\\d{2}:\\d{2}$"); formats.put("dd.MM.yyyy", "^\\d{2}\\.\\d{2}\\.\\d{4}$"); formats.put("dd-MM-yyyy", "^\\d{2}-\\d{2}-\\d{4}$"); formats.put("dd/MM/yyyy", "^\\d{2}/\\d{2}/\\d{4}$"); formats.put("yyyy/dd/MM", "^\\d{4}/\\d{2}/\\d{2}$"); formats.put("yyyy.MM.dd", "^\\d{4}\\.\\d{2}\\.\\d{2}$"); formats.put("yyyy-MM-dd", "^\\d{4}-\\d{2}-\\d{2}$"); for (Map.Entry<String, String> format : formats.entrySet()) { try { if (value.trim().matches(format.getValue())) { Date result = new SimpleDateFormat(format.getKey()).parse(value.trim()); if (logger.isDebugEnabled()) { logger.debug("Matched format " + format +" for " + value); } return result; } } catch (ParseException ex) { } } return null; }  }
FilterConverterHelper { public static Date dateConvert(String value) { if (value == null) { return null; } Map<String, String> formats = new HashMap<>(); formats.put("dd.MM.yyyy HH:mm", "^\\d{2}\\.\\d{2}\\.\\d{4}\\s+\\d{2}:\\d{2}$"); formats.put("dd-MM-yyyy HH:mm", "^\\d{2}-\\d{2}-\\d{4}\\s+\\d{2}:\\d{2}$"); formats.put("dd/MM/yyyy HH:mm", "^\\d{2}/\\d{2}/\\d{4}\\s+\\d{2}:\\d{2}$"); formats.put("yyyy/dd/MM HH:mm", "^\\d{4}/\\d{2}/\\d{2}\\s+\\d{2}:\\d{2}$"); formats.put("yyyy.MM.dd HH:mm", "^\\d{4}\\.\\d{2}\\.\\d{2}\\s+\\d{2}:\\d{2}$"); formats.put("yyyy-MM-dd HH:mm", "^\\d{4}-\\d{2}-\\d{2}\\s+\\d{2}:\\d{2}$"); formats.put("dd.MM.yyyy", "^\\d{2}\\.\\d{2}\\.\\d{4}$"); formats.put("dd-MM-yyyy", "^\\d{2}-\\d{2}-\\d{4}$"); formats.put("dd/MM/yyyy", "^\\d{2}/\\d{2}/\\d{4}$"); formats.put("yyyy/dd/MM", "^\\d{4}/\\d{2}/\\d{2}$"); formats.put("yyyy.MM.dd", "^\\d{4}\\.\\d{2}\\.\\d{2}$"); formats.put("yyyy-MM-dd", "^\\d{4}-\\d{2}-\\d{2}$"); for (Map.Entry<String, String> format : formats.entrySet()) { try { if (value.trim().matches(format.getValue())) { Date result = new SimpleDateFormat(format.getKey()).parse(value.trim()); if (logger.isDebugEnabled()) { logger.debug("Matched format " + format +" for " + value); } return result; } } catch (ParseException ex) { } } return null; }  static T floatingPointConvert(String value, Class<?> clazz); static List<T> integerArrayConvert(String value, Class<?> clazz); static T integerConvert(String value, Class<?> clazz); static Object booleanConvert(String value, Class clazz); static Date dateConvert(String value); }
FilterConverterHelper { public static Date dateConvert(String value) { if (value == null) { return null; } Map<String, String> formats = new HashMap<>(); formats.put("dd.MM.yyyy HH:mm", "^\\d{2}\\.\\d{2}\\.\\d{4}\\s+\\d{2}:\\d{2}$"); formats.put("dd-MM-yyyy HH:mm", "^\\d{2}-\\d{2}-\\d{4}\\s+\\d{2}:\\d{2}$"); formats.put("dd/MM/yyyy HH:mm", "^\\d{2}/\\d{2}/\\d{4}\\s+\\d{2}:\\d{2}$"); formats.put("yyyy/dd/MM HH:mm", "^\\d{4}/\\d{2}/\\d{2}\\s+\\d{2}:\\d{2}$"); formats.put("yyyy.MM.dd HH:mm", "^\\d{4}\\.\\d{2}\\.\\d{2}\\s+\\d{2}:\\d{2}$"); formats.put("yyyy-MM-dd HH:mm", "^\\d{4}-\\d{2}-\\d{2}\\s+\\d{2}:\\d{2}$"); formats.put("dd.MM.yyyy", "^\\d{2}\\.\\d{2}\\.\\d{4}$"); formats.put("dd-MM-yyyy", "^\\d{2}-\\d{2}-\\d{4}$"); formats.put("dd/MM/yyyy", "^\\d{2}/\\d{2}/\\d{4}$"); formats.put("yyyy/dd/MM", "^\\d{4}/\\d{2}/\\d{2}$"); formats.put("yyyy.MM.dd", "^\\d{4}\\.\\d{2}\\.\\d{2}$"); formats.put("yyyy-MM-dd", "^\\d{4}-\\d{2}-\\d{2}$"); for (Map.Entry<String, String> format : formats.entrySet()) { try { if (value.trim().matches(format.getValue())) { Date result = new SimpleDateFormat(format.getKey()).parse(value.trim()); if (logger.isDebugEnabled()) { logger.debug("Matched format " + format +" for " + value); } return result; } } catch (ParseException ex) { } } return null; }  static T floatingPointConvert(String value, Class<?> clazz); static List<T> integerArrayConvert(String value, Class<?> clazz); static T integerConvert(String value, Class<?> clazz); static Object booleanConvert(String value, Class clazz); static Date dateConvert(String value);  }