Java Date Value Check isDateFormatString(String s, String dateFormat, Locale locale)

Here you can find the source of isDateFormatString(String s, String dateFormat, Locale locale)

Description

Return true if a string can be parsed by the dateFormat with locale.

License

LGPL

Parameter

Parameter Description
s a parameter
dateFormat a parameter
locale a parameter

Declaration


public static boolean isDateFormatString(String s, String dateFormat, Locale locale) 

Method Source Code

//package com.java2s;
/*/*ww w  .j a  v  a  2s  .  c o m*/
 * OpenClinica is distributed under the
 * GNU Lesser General Public License (GNU LGPL).
    
 * For details see: http://www.openclinica.org/license
 * copyright 2003-2005 Akaza Research
 */

import java.text.SimpleDateFormat;

import java.util.Locale;

public class Main {
    /**
     * Return true if a string can be parsed by the dateFormat with locale.
     *
     * @param s
     * @param dateFormat
     * @param locale
     * @return
     */
    //ywang (Oct., 2011)
    public static boolean isDateFormatString(String s, String dateFormat, Locale locale) {
        String dateformat = parseDateFormat(dateFormat);
        SimpleDateFormat f = new SimpleDateFormat(dateformat, locale);
        f.setLenient(false);
        try {
            f.parse(s);
            return true;
        } catch (Exception ex) {
            return false;
        }
    }

    /**
     * return dateFormat with lowercase "y" and "d"
     *
     * @param dateFormat
     * @return
     */
    public static String parseDateFormat(String dateFormat) {
        String s = dateFormat;
        while (s.contains("Y")) {
            s = s.replace("Y", "y");
        }
        while (s.contains("D")) {
            s = s.replace("D", "d");
        }
        return s;
    }
}

Related

  1. isDateBefore(String date1)
  2. isDateBefore(String date1, String date2)
  3. isDateBefore(String date1, String date2)
  4. isDateFormat(String dateString)
  5. isDateFormat(String str, String format)
  6. isDateFormatValid(final String pattern)
  7. isDateInRange(Date startDt, Date endDt, Date theDate)
  8. isDateInRange(Date toCheck, Date minRange, Date maxRange)
  9. IsDateOK(String date)