1. Use Regular Expressions in Java to determine whether the date format is correct
DateType. java:
/*** @ Title: DateType. java * @ Package: com. you. dao * @ Description: * @ Author: youhaidong * @ date: 10:54:50, January 1, March 8, 2014 * @ Version V1.2.3 */package com. you. dao; import java. util. regex. matcher; import java. util. regex. pattern;/*** @ Class Name: DateType * @ Description: * @ Author: youhaidong * @ date: march 8, 2014 10:54:50 */public class DateType {/***** @ class Name: DateSelect * @ Description: * @ Author: You Haidong * @ date: march 8, 2014 11:09:37 */public static CIA Ss DateSelect {public boolean isDate (String date) {/*** determine the date format and range */String rexp = "^ (\ d {2} ([02468] [048]) | ([13579] [26]) [\-\/\ s]? (0? [13578]) | (1 [02]) [\-\/\ s]? (0? [1-9]) | ([1-2] [0-9]) | (3 [01]) | (0? [469]) | (11) [\-\/\ s]? (0? [1-9]) | ([1-2] [0-9]) | (30) | (0? 2 [\-\/\ s]? (0? [1-9]) | ([1-2] [0-9]) | (\ d {2} ([02468] [1235679]) | ([13579] [01345789]) [\-\/\ s]? (0? [13578]) | (1 [02]) [\-\/\ s]? (0? [1-9]) | ([1-2] [0-9]) | (3 [01]) | (0? [469]) | (11) [\-\/\ s]? (0? [1-9]) | ([1-2] [0-9]) | (30) | (0? 2 [\-\/\ s]? (0? [1-9]) | (1 [0-9]) | (2 [0-8]) "; Pattern pat = Pattern. compile (rexp); Matcher mat = pat. matcher (date); boolean dateType = mat. matches (); return dateType ;}/ *** @ Title: main * @ Type: DateType * @ date: 10:54:50, January 1, March 8, 2014 * @ Description: * @ param args */public static void main (String [] args) {/*** date format is correct */String date1 = "; /*** incorrect date range --- no 29th on July 15, */String date2 = "February "; /*** incorrect date month range --- month does not have 13 months */String date3 = "2014-13-03 "; /*** incorrect date range --- no 31st on March 31 */String date4 = "June "; /*** incorrect date range ---- January more than 31 days */String date5 = "2014-01-32 "; /*** this test year */String date6 = "0014-01-03"; DateSelect date = new DateSelect ();/*** print the correct date format */System. out. println (date. isDate (date1);/*** print date1 */System. out. println (date. isDate (date2);/*** print date3 */System. out. println (date. isDate (date3);/*** print date4 */System. out. println (date. isDate (date4);/*** print date5 */System. out. println (date. isDate (date5);/*** print date6 */System. out. println (date. isDate (date6 ));}}
2. Running result
truefalsefalsefalsefalsetrue