I use the date type in my database. When programming with java, IMPORT also did java.util.*And java.sql.*And found that the DATE type was directly declared
Date dt;
I will report an error. After checking it, I found that there are java.util.date and java.sql.date. When defining the date type type, use the full name, just like this:
java.util.date udt; java.sql.date sdt;
Then I checked the differences and usage of java.util.date and java.sql.date. The online information in this regard is quite complete, so I won't go into detail .Date only supported the date. I also used String to Java.sql.date. This can also be found on the Internet. I briefly introduce my favorite method.
First of all, prepare a SimpleDateFormat object, using the SimpleDateFormat class requires import java.text.simpleDateFormat
SimpleDateFormat DF = New SimpleDateFormat ("Yyyy-MM-DD"); // Yyyy-MM-DD is the date format we want to convert.
Then, prepare a java.util.date object and a string to be converted
Copy code code as follows:
String str = "2011-06-30"; // It cannot be written here as 2011/06/30 or other looks, which will report an error.
java.util.date udt = null;
Then use DF to convert STR to java.util.date and assign a value to UDT
udt = df.parse (STR);
Then get the java.sql.date we need from UDT
java.sql.date sdt = new java.sql.date (udt.gettime ());
To sum up, first use SimpleDateFormat to format the string to be converted into the date into the java.util.date type, and then obtain the Java.SQL.DATE object from the obtained java.util.date object. We can write it into a function, The code is as follows:
Public Java.SQL.DATE StringTosqldate (String Str) {SimpleDateFormat SDF = New SimpleDateFormat ("Yyyy-MM-DD"); java.util.date dt = sdf.parse (str);} Catch ( Exceprion E) {e.printstacktrace ();} java.sql.date sdt = new java.sql.date (udt.gettime ())); Return SDT;}The above is all the contents of this article. I hope everyone can like it.