Use regular expressions in Java to determine whether the date format is correct

Source: Internet
Author: User

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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.