Java Date Value Check isDate(String s)

Here you can find the source of isDate(String s)

Description

is Date

License

Open Source License

Declaration

public static boolean isDate(String s) 

Method Source Code


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

import java.text.ParsePosition;
import java.text.SimpleDateFormat;

public class Main {
    public static boolean isDate(String s) {
        if (s != null) {
            if (s.matches("^\\d{4}([/.-])\\d{2}\\1\\d{2}$")) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                sdf.setLenient(false);/*w  w  w .  j av a2  s .  c o m*/
                return sdf.parse(s.replaceAll("[/.]", "-"), new ParsePosition(0)) != null;
            } else if (s.matches("^\\d{2}([/.-])\\d{2}\\1\\d{4}$")) {
                SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
                sdf.setLenient(false);
                return sdf.parse(s.replaceAll("[/.]", "-"), new ParsePosition(0)) != null;
            }
        }
        return false;
    }
}

Related

  1. isDate(String dateString)
  2. isDate(String dttm, String format)
  3. isDate(String input)
  4. isDate(String input)
  5. isDate(String pattern, String text)
  6. isDate(String str)
  7. isDate(String str)
  8. IsDate(String str)
  9. isDate(String str, String format)