Comparison between start time and End Time (Java Regular Expression implementation)

Source: Internet
Author: User
Package javaapplication1; import Java. text. simpledateformat; import Java. util. date; import Java. util. regEx. pattern;/***** @ author yjmao * @ version v1.0.0 * @ date 2012-10-11 * @ deprecated start time and end time comparison (Regular Expression comparison) */public class checkdate {public static void main (string [] ARGs) {string starttime = "12:25:56"; string endtime = "12:25:55"; string error = checkdateflag (starttime, endtime ); System. err. println (error);} public static string checkdateflag (string starttime, string endtime) {stringbuilder year_mounth_date_hour_minute_second = new stringbuilder (); // [1-9] {1} [0-9] {3 }: matching year 1000-9999 year_mounth_date_hour_minute_second.append ("[1-9] {1} [0-9] {3 }"). append ("-"); // ([1-9] | [0] {1} [1-9] {1} | [1] {1} [0-2] {1 }) matched month: 1-12, 01-12, or year_mounth_date_hour_minute_second.append ("([1-9] | [0] {1} [1-9] {1} | [1] {1} [0-2] {1 })"). append ("-"); // ([1-9] | [0] {1} [1-9] {1} | [1-2] {1} [0-9] {1} | [3] {1} [0-1] {1 }) matching day: 1-31 or 01-31 year_mounth_date_hour_minute_second.append ("([1-9] | [0] {1} [1-9] {1} | [1-2] {1} [0 -9] {1} | [3] {1} [0-1] {1 }) "). append ("\ s *"); // ([0-9] | [0-1] {1} [0-9] {1} | [2] {1} [0-3] {1}): matching hour: 00-23 or 0-23 year_mounth_date_hour_minute_second.append ("([0-9] | [0-1] {1} [0-9] {1} | [2] {1} [0 -3] {1 }): "); // ([0-9] | [0-5] {1} [0-9] {1}): matching minutes: 00-59 or 0-59 year_mounth_date_hour_minute_second.append ("([0-9] | [0-5] {1} [0-9] {1 }):"); // ([0-9] | [0-5] {1} [0-9] {1}): matching seconds: 00-59 or 0-59 year_mounth_date_hour_minute_second.append ("([0-9] | [0-5] {1} [0-9] {1 })"); // matched string regtime = year_mounth_date_hour_minute_second.tostring (); stringbuilder errorinfo = new stringbuilder (); If (starttime. indexof ("/")! =-1) {starttime = starttime. Replace ("/", "-");} If (endtime. indexof ("/")! =-1) {endtime = endtime. replace ("/", "-");} pattern = pattern. compile (regtime); Boolean starttimeflag = pattern. matcher (starttime ). matches (); Boolean endtimeflag = pattern. matcher (endtime ). matches (); If (! Starttimeflag) {errorinfo. append ("Incorrect start time format! ");} If (! Endtimeflag) {errorinfo. append ("the end time format is incorrect! ");} Simpledateformat SDF = new simpledateformat (" yyyy-mm-dd hh: mm: SS "); Date startdate = NULL, enddate = NULL; try {startdate = SDF. parse (starttime); enddate = SDF. parse (endtime);} catch (exception e) {} If (startdate. after (enddate) {errorinfo. append ("the start time cannot be earlier than the end time! ") ;}Return errorinfo. tostring ();}}

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.