Java Date Format Check checkDateByMask(String sDateValue, String sDateFormat)

Here you can find the source of checkDateByMask(String sDateValue, String sDateFormat)

Description

check the date by pattern

License

Apache License

Parameter

Parameter Description
sDateValue a parameter
sDateFormat a parameter

Exception

Parameter Description
ParseException an exception

Return

boolean,if check the date ok,then return true

Declaration

public static boolean checkDateByMask(String sDateValue, String sDateFormat) throws ParseException 

Method Source Code

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

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

public class Main {
    /**//from ww w .  j  a v  a  2  s.co m
     * check the date by pattern
     * 
     * @param sDateValue
     * @param sDateFormat
     * @return boolean,if check the date ok,then return true
     * @throws ParseException
     */
    public static boolean checkDateByMask(String sDateValue, String sDateFormat) throws ParseException {
        boolean isTrue = false;
        if (sDateValue == null || sDateFormat == null || sDateValue.equals(""))
            return false;
        if (sDateValue.length() != sDateFormat.length())
            return false;
        java.util.Date date = stringToDate(sDateValue, sDateFormat);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        if (sDateFormat.indexOf("yyyy") > -1) {
            isTrue = (cal.get(Calendar.YEAR) == Integer
                    .parseInt(sDateValue.substring(sDateFormat.indexOf("yyyy"), sDateFormat.indexOf("yyyy") + 4))
                    && cal.get(Calendar.MONTH) == (Integer.parseInt(
                            sDateValue.substring(sDateFormat.indexOf("MM"), sDateFormat.indexOf("MM") + 2)) - 1)
                    && cal.get(Calendar.DATE) == Integer.parseInt(
                            sDateValue.substring(sDateFormat.indexOf("dd"), sDateFormat.indexOf("dd") + 2)));
        } else {
            isTrue = (cal.get(Calendar.YEAR) == Integer
                    .parseInt(sDateValue.substring(sDateFormat.indexOf("yy"), sDateFormat.indexOf("yy") + 2))
                    && cal.get(Calendar.MONTH) == (Integer.parseInt(
                            sDateValue.substring(sDateFormat.indexOf("MM"), sDateFormat.indexOf("MM") + 2)) - 1)
                    && cal.get(Calendar.DATE) == Integer.parseInt(
                            sDateValue.substring(sDateFormat.indexOf("dd"), sDateFormat.indexOf("dd") + 2)));
        }
        return isTrue;
    }

    /**
     * Convert string to Date
     * 
     * @return a java.util.Date object converted.
     */
    public static java.util.Date stringToDate(String pstrValue, String pstrDateFormat) {
        if ((pstrValue == null) || (pstrValue.equals(""))) {
            return null;
        }
        java.util.Date dttDate = null;
        try {
            SimpleDateFormat oFormatter = new SimpleDateFormat(pstrDateFormat);
            dttDate = oFormatter.parse(pstrValue);
            oFormatter = null;
        } catch (Exception e) {
            return null;
        }

        return dttDate;
    }
}

Related

  1. checkDate(Date date)
  2. checkDate(String begingDate, String endDate)
  3. checkDate(String deadline)
  4. checkDateBetween(String time)
  5. checkDateFormat()
  6. checkDateFormat()
  7. checkDateFormat(String pattern)
  8. checkDateFormat(String strTime, String pattern)