/** * * @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 - 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; } }
/** * * @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; }
/** * * @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 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 ); }
/** * 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 ); } }