/** * @param dateString - string for check * @return true if we can parse string without exception */ public static boolean isValidDate( String dateString ) { try { getDateFromString( dateString ); return true; } catch ( ParseException e ) { return false; } }
/** * * @param dateString * date string for parse * @return {@link java.util.Date} converted from dateString by detected format * @throws ParseException * - if we can not detect date format for string or we can not parse date string */ public static Date getDateFromString( String dateString ) throws ParseException { String dateFormat = detectDateFormat( dateString ); if ( dateFormat == null ) { throw new ParseException( "Unknown date format.", 0 ); } return getDateFromStringByFormat( dateString, dateFormat ); }
/** * * @param dateFormat - format which we will try to apply for string * @param dateString - string which contains date * @param locale - locale for date format * @return true if we found that we know dateFormat and it applied for given string */ public static boolean isValidDateFormatToStringDate( String dateFormat, String dateString, String locale ) { String detectedDateFormat = dateFormat != null ? detectDateFormatBiased( dateString, locale, dateFormat ) : detectDateFormat( dateString, locale ); if ( ( dateFormat != null ) && ( dateFormat.equals( detectedDateFormat ) ) ) { return true; } return false; } }
/** * Finds a date format that matches the date value given. Will try the desiredKey format before attempting others. The * first to match is returned. * * @param dateString * the literal value of the date (eg: "01/01/2001") * @param locale * the locale in play * @param desiredKey * the desired format (should be a valid key to DATE_FORMAT_TO_REGEXPS) * @return The key to the format that matched or null if none found. */ public static String detectDateFormatBiased( String dateString, String locale, String desiredKey ) { if ( dateString == null ) { return null; } String regex = (String) getDateFormatToRegExps( locale ).get( desiredKey ); if ( regex != null && dateString.toLowerCase().matches( regex ) ) { return desiredKey; } else { return detectDateFormat( dateString, locale ); } }
if ( !DateDetector.isValidDateFormatToStringDate( dateFormat, value, "en_US" ) ) { evaluationResults.remove( cmm ); } else { try { Object object = DateDetector.getDateFromStringByFormat( value, dateFormat ); cmm.incrementSuccesses(); if ( cmm.getMin() == null || cmm.getConversionMeta().compare( cmm.getMin(), object ) > 0 ) {
/** * * @param dateString * - date string for detect date format * @return {@link java.lang.String} string which represented Date Format or null * */ public static String detectDateFormat( String dateString ) { return detectDateFormat( dateString, null ); }
/** * * @param dateFormat - date format for get regexp * @return regexp for given date format */ public static String getRegexpByDateFormat( String dateFormat ) { return getRegexpByDateFormat( dateFormat, null ); }
/** * * @param regex - regexp for parse date format from string * <br> * <b>NOTES:</b> if regex could be used for US and EU locale. * It returns europeans locale. For en_US locale please use * * {@link #getDateFormatByRegex( String regex, String locale ) } * * @return {@link java.lang.String} string wich represented Date Format */ public static String getDateFormatByRegex( String regex ) { return getDateFormatByRegex( regex, null ); }
/** * * @param dateString - string for check * @param dateFormat - format for check * @return true if we can parse string by format without exception */ public static boolean isValidDate( String dateString, String dateFormat ) { try { getDateFromStringByFormat( dateString, dateFormat ); return true; } catch ( ParseException e ) { return false; } }
/** * * @param dateString * - date string for detect date format * @return {@link java.lang.String} string which represented Date Format or null */ public static String detectDateFormat( String dateString, String locale ) { if ( dateString == null ) { return null; } for ( Object regexp : getDateFormatToRegExps( locale ).values() ) { if ( dateString.toLowerCase().matches( (String) regexp ) ) { return (String) getDateFormatToRegExps( locale ).getKey( regexp ); } } return null; }
/** * * @param dateFormat - format which we will try to apply for string * @param dateString - string which contains date * @return true if we found that we know dateFormat and it applied for given string */ public static boolean isValidDateFormatToStringDate( String dateFormat, String dateString ) { String detectedDateFormat = detectDateFormat( dateString ); if ( ( dateFormat != null ) && ( dateFormat.equals( detectedDateFormat ) ) ) { return true; } return false; }
return simpleDateFormat.format( data ); Date dateFromstring = DateDetector.getDateFromString( String.valueOf( data ) ); return simpleDateFormat.format( dateFromstring ); case STRING:
/** * * @param dateString * date string for parse * @return {@link java.util.Date} converted from dateString by detected format * @throws ParseException * - if we can not detect date format for string or we can not parse date string */ public static Date getDateFromString( String dateString, String locale ) throws ParseException { String dateFormat = detectDateFormat( dateString, locale ); if ( dateFormat == null ) { throw new ParseException( "Unknown date format.", 0 ); } return getDateFromStringByFormat( dateString, dateFormat ); }