Java Date Value Check isDate(String dttm, String format)

Here you can find the source of isDate(String dttm, String format)

Description

is Date

License

Apache License

Declaration

public static boolean isDate(String dttm, String format) 

Method Source Code


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

import java.text.DateFormat;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main {

    public static boolean isDate(String dttm, String format) {
        if (dttm == null || dttm.isEmpty() || format == null || format.isEmpty()) {
            return false;
        }/*  w  w  w.ja  v  a2  s  . c  om*/

        if (format.replaceAll("'.+?'", "").indexOf("y") < 0) {
            format += "/yyyy";
            DateFormat formatter = new SimpleDateFormat("/yyyy");
            dttm += formatter.format(new Date());
        }

        DateFormat formatter = new SimpleDateFormat(format);
        formatter.setLenient(false);
        ParsePosition pos = new ParsePosition(0);
        Date date = formatter.parse(dttm, pos);

        if (date == null || pos.getErrorIndex() > 0) {
            return false;
        }
        if (pos.getIndex() != dttm.length()) {
            return false;
        }

        if (formatter.getCalendar().get(Calendar.YEAR) > 9999) {
            return false;
        }

        return true;
    }
}

Related

  1. isDate(final String strDate, final String pattern)
  2. isDate(Object object)
  3. isDate(String date)
  4. isDate(String dateStr, String dateFormat)
  5. isDate(String dateString)
  6. isDate(String input)
  7. isDate(String input)
  8. isDate(String pattern, String text)
  9. isDate(String s)