Here you can find the source of checkDateByMask(String sDateValue, String sDateFormat)
Parameter | Description |
---|---|
sDateValue | a parameter |
sDateFormat | a parameter |
Parameter | Description |
---|---|
ParseException | an exception |
public static boolean checkDateByMask(String sDateValue, String sDateFormat) throws ParseException
//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; } }