Java date format verification several instance programs

Source: Internet
Author: User
Tags java format

This article will introduce you to a date format verification example. This is a date verification program written by another student. I hope this method will be helpful to you.

Requirement: because the system has many date formats, the input of the date verification function is a date string and a format string. The format string is defined in Java format (reference address ).
At the beginning, I thought it was very simple. I wrote the following code directly.

The Code is as follows: Copy code

Public static boolean isDate (String dttm, String format ){
Boolean retValue = false;
If (dttm! = Null ){
SimpleDateFormat formatter = new SimpleDateFormat (format );
Try {
Formatter. parse (dttm );
RetValue = true;
} Catch (ParseException e ){
}
}
Return retValue;
}

After writing it, I tested it and found that the date 2012/12/43 also returned True, so I checked the data and set the conversion without carrying formatter. setLenient (false); then I used it, and the code is

The Code is as follows: Copy code

Public static boolean isDate (String dttm, String format ){
Boolean retValue = false;
If (dttm! = Null ){
SimpleDateFormat formatter = new SimpleDateFormat (format );
Formatter. setLenient (false );
Try {
Formatter. parse (dttm );
RetValue = true;
} Catch (ParseException e ){
}
}
Return retValue;
}

Here, I thought everything would be fine. However, tests later showed that only the first part of the date will be converted when there is a separator. For example, 2012/12/43ABCD will return True. After thinking about it, I thought it would be converted to the date type, then to the string type, and then compare whether the two are equal, but I immediately rejected this scheme because the customer requested a non-strict date format, for example, if the format is yyyy/MM/dd, enter 2012/1/2 to pass the verification. Then I thought about it. I thought we should first use a regular expression for verification and then verify whether it is a date type. The Code is as follows:

The Code is as follows: Copy code

Public static boolean isDate (String dttm, String format ){
Boolean retValue = false;

If (dttm = null | dttm. isEmpty () | format = null | format. isEmpty ()){
Return retValue;
}

String regFormat = format;
RegFormat = regFormat. replaceAll ("(^ ') | (' $ )","");
RegFormat = regFormat. replaceAll ("'([^'])", "$1 ");
RegFormat = regFormat. replace ("''","'");
RegFormat = regFormat. replace ("\","\\");
RegFormat = regFormat. replaceAll ("[MdHmsS] +", "\ d + ");
RegFormat = regFormat. replaceAll ("[y] +", "\ d {1, 4 }");
If (! Dttm. matches ("^" + regFormat + "$ ")){
Return false;
}

SimpleDateFormat formatter = new SimpleDateFormat (format );
Formatter. setLenient (false );
Try {
Formatter. parse (dttm );
RetValue = true;
} Catch (ParseException e ){
}

Return retValue;
}

The above Code only corresponds to yMdHmsS. Although it is enough for the system at that time, it still does not feel very good. I think there should be a general method. Check the Java API and find that the parse method also has a parameter method. After understanding how to use it, change the code to the following:

The Code is as follows: Copy code

Private static boolean isDate (String dttm, String format ){
If (dttm = null | dttm. isEmpty () | format = null | format. isEmpty ()){
Return false;
}

DateFormat formatter = new SimpleDateFormat (format );
Formatter. setLenient (false );
ParsePosition pos = new ParsePosition (0 );
Date date = formatter. parse (dttm, pos );

If (date = null | pos. getErrorIndex ()> 0 ){
Return false;
}

If (pos. getIndex ()! = Dttm. length ()){
Return false;
}

Return true;
}

I thought it would be fine, but two bugs were found in subsequent tests. One is that when the input date does not have a year (the year is not input by default), the default value is 1970. In this case, if the year is a leap year, the verification fails on the 9th. The other is that the date size of Java is different from that of Oracle. Oracle seems to support up to 2/29, while Java can support more than 20 thousand years. So the code is changed to the following:

The Code is as follows: Copy code

Private static boolean isDate (String dttm, String format ){
If (dttm = null | dttm. isEmpty () | format = null | format. isEmpty ()){
Return false;
}

If (format. replaceAll ("'. +? '"," "). IndexOf (" y ") <0 ){
Format + = "/yyyy ";
DateFormat formatter = new SimpleDateFormat ("/yyyy ");
Dttm + = formatter. format (new Date ());
}

DateFormat formatter = new SimpleDateFormat (format );
Formatter. setLenient (false );
ParsePosition pos = new ParsePosition (0 );
Date date = formatter. parse (dttm, pos );

If (date = null | pos. getErrorIndex ()> 0 ){
Return false;
}
If (pos. getIndex ()! = Dttm. length ()){
Return false;
}

If (formatter. getCalendar (). get (Calendar. YEAR)> 9999 ){
Return false;
}

Return true;
}

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.