Java Date Value Check isDateFormat(String str, String format)

Here you can find the source of isDateFormat(String str, String format)

Description

is Date Format

License

Open Source License

Declaration

public static boolean isDateFormat(String str, String format) throws ParseException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {

    public static boolean isDateFormat(String str, String format) throws ParseException {
        if (hasText(str) || hasText(format)) {
            return false;
        }//from   ww w. j a  v  a2s  .  c o m
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        sdf.setLenient(true);

        Date date = sdf.parse(str);
        String str2 = sdf.format(date);

        if (!str.equals(str2)) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * Check whether the given CharSequence has actual text.
     * More specifically, returns <code>true</code> if the string not <code>null</code>,
     * its length is greater than 0, and it contains at least one non-whitespace character.
     * <p><pre>
     * StringUtils.hasText(null) = false
     * StringUtils.hasText("") = false
     * StringUtils.hasText(" ") = false
     * StringUtils.hasText("12345") = true
     * StringUtils.hasText(" 12345 ") = true
     * </pre>
     * @param str the CharSequence to check (may be <code>null</code>)
     * @return <code>true</code> if the CharSequence is not <code>null</code>,
     * its length is greater than 0, and it does not contain whitespace only
     * @see java.lang.Character#isWhitespace
     */
    public static boolean hasText(CharSequence str) {
        if (!hasLength(str)) {
            return false;
        }
        int strLen = str.length();
        for (int i = 0; i < strLen; i++) {
            if (!Character.isWhitespace(str.charAt(i))) {
                return true;
            }
        }
        return false;
    }

    /**
     * Check that the given CharSequence is neither <code>null</code> nor of length 0.
     * Note: Will return <code>true</code> for a CharSequence that purely consists of whitespace.
     * <p><pre>
     * StringUtils.hasLength(null) = false
     * StringUtils.hasLength("") = false
     * StringUtils.hasLength(" ") = true
     * StringUtils.hasLength("Hello") = true
     * </pre>
     * @param str the CharSequence to check (may be <code>null</code>)
     * @return <code>true</code> if the CharSequence is not null and has length
     * @see #hasText(String)
     */
    public static boolean hasLength(CharSequence str) {
        return (str != null && str.length() > 0);
    }

    /**
     * Check that the given String is neither <code>null</code> nor of length 0.
     * Note: Will return <code>true</code> for a String that purely consists of whitespace.
     * @param str the String to check (may be <code>null</code>)
     * @return <code>true</code> if the String is not null and has length
     * @see #hasLength(CharSequence)
     */
    public static boolean hasLength(String str) {
        return hasLength((CharSequence) str);
    }
}

Related

  1. isDate(String value, Locale locale)
  2. isDateBefore(String date1)
  3. isDateBefore(String date1, String date2)
  4. isDateBefore(String date1, String date2)
  5. isDateFormat(String dateString)
  6. isDateFormatString(String s, String dateFormat, Locale locale)
  7. isDateFormatValid(final String pattern)
  8. isDateInRange(Date startDt, Date endDt, Date theDate)
  9. isDateInRange(Date toCheck, Date minRange, Date maxRange)