Java Date Value Check isDate(String strDate, String pattern)

Here you can find the source of isDate(String strDate, String pattern)

Description

is Date

License

Apache License

Declaration

public static boolean isDate(String strDate, String pattern) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.text.DateFormat;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.HashMap;

import java.util.Map;

public class Main {
    private static final Map<String, DateFormat> DFS = new HashMap<String, DateFormat>();

    public static boolean isDate(String strDate, String pattern) {
        if (strDate == null || strDate.trim().length() <= 0)
            return false;
        try {/*w  w  w.ja  v  a2  s .c o m*/
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            sdf.parse(strDate);
            return true;
        } catch (ParseException e) {
            return false;
        }
    }

    public static Date parse(String source, String pattern) {
        if (source == null) {
            return null;
        }
        Date date;
        try {
            date = getFormat(pattern).parse(source);
        } catch (ParseException e) {
            return null;
        }
        return date;
    }

    public static DateFormat getFormat(String pattern) {
        DateFormat format = DFS.get(pattern);
        if (format == null) {
            format = new SimpleDateFormat(pattern);
            DFS.put(pattern, format);
        }
        return format;
    }
}

Related

  1. IsDate(String str)
  2. isDate(String str)
  3. isDate(String str, String format)
  4. isDate(String str, String pattern)
  5. isDate(String strDate, SimpleDateFormat sdf)
  6. isDate(String strDate, String pattern)
  7. isDate(String strDate, String pattern)
  8. isDate(String text, String pattern)
  9. isDate(String val, SimpleDateFormat formatter)