Today I wrote a small program, which is a registration page. I added the registration information to the MySQL database and did not validate the data on the registration page. After writing the code, I tried it myself, found very good. Then let the students try again, and an error occurs when the result is run. It was later discovered that the data-type data conversion was wrong.
I used
Data Birthday = java. SQL. Date. valueof (sbirthday. Trim ());
This method is used to convert data. The result is displayed here.
The input date is. If the input date is not in the format of yyyy-mm-dd, an error occurs. I wonder if such a date cannot be converted?
Then I checked the jdk1.6 source code: it is actually handled like this:
Public static date valueof (string s ){
Final int year_length = 4; // defines the length of a year.
Final int month_length = 2; // defines the length of a month.
Final int day_length = 2; // defines the length of a day.
Final int max_month = 12; // defines the maximum length of a month.
Final int max_day = 31; // defines the maximum length of a day.
Int firstdash; // defines a badge (I don't know what to call it)
Int seconddash; // defines a badge (I don't know what to call it)
Date d = NULL; // predefines the date.
If (S = NULL ){
Throw new java. Lang. illegalargumentexception ();
}
Firstdash = S. indexof ('-');
Seconddash = S. indexof ('-', firstdash + 1 );
If (firstdash> 0) & (seconddash> 0) & (seconddash <S. Length ()-1 )){
String yyyy = S. substring (0, firstdash );
String Mm = S. substring (firstdash + 1, seconddash );
String dd = S. substring (seconddash + 1 );
// The problem occurs here. It first checks whether the cut date is 4 bits per year, 2 bits per month, and 2 bits per day. Otherwise, D is null, throw an exception.
// Isn't it handled by foreigners?
If (YYYY. Length () = year_length & mm. Length () = month_length &&
Dd. Length () = day_length ){
Int year = integer. parseint (yyyy );
Int month = integer. parseint (mm );
Int day = integer. parseint (dd );
If (month> = 1 & month <= max_month ){
Int maxdays = max_day;
Switch (month ){
// February determine if a leap year or not
Case 2:
If (Year % 4 = 0 &&! (Year % 100 = 0) | (Year % 400 = 0 )){
Maxdays = MAX_DAY-2; // leap year so 29 days in February
} Else {
Maxdays = MAX_DAY-3; // not a leap year so 28 days in February
}
Break;
// 10000l, June, Sept, Nov 30 day months
Case 4:
Case 6:
Case 9:
Case 11:
Maxdays = MAX_DAY-1;
Break;
}
If (day> = 1 & day <= maxdays ){
D = new date (year-1900, month-1, Day );
}
}
}
}
// If your date format is incorrect, wait and throw an exception !!!
If (D = NULL ){
Throw new java. Lang. illegalargumentexception ();
}
Return D;
}
It seems that writing code by foreigners is very rigorous. He will hand over the problem to the user and strictly handle it according to the qualified data you have handed over to him. Otherwise, there will be a lot of data that is not strictly formatted and submitted to JDK, which will be messy!
Check the source code to find many problems.