1. How do I convert java.util.Date to java.sql.Date?
Transformation:
Java.sql.Date SD;
Java.util.Date ud;
Initialize the UD such as UD = new Java.util.Date ();
SD = new Java.sql.Date (Ud.gettime ());
2. How to convert the acquired java.sql.Date to year-month-day output
Java.sql.Date SD;
String dateTime = sd.tostring ();
3. If you are inserting into a database and the corresponding fields are of the date type
Then you can use the preparedstatement.setdate (int, java.sql.Date) method
The java.sql.Date can be obtained using the above method
You can also use the database to provide to_date functions
such as existing UD
To_date (New SimpleDateFormat (). Format (UD, "Yyyy-mm-dd HH:mm:ss"),
"Yyyy-mm-dd HH24:MI:SS")
Note that in Java, the format differs from the format provided by the database
A practical example
Sql= "UPDATE tablename set Timer=to_date ('" +t+ "', ' Yyyymmddhh24miss ') where ..."
The t here is similar to the variable: 20051211131223
4. How to convert a string in "YYYY-MM-DD" format to Java.sql.Date
Method 1
SimpleDateFormat Bartdateformat =
New SimpleDateFormat ("Yyyy-mm-dd");
String datestringtoparse = "2007-7-12";
try {
Java.util.Date Date = Bartdateformat.parse (datestringtoparse);
Java.sql.Date sqldate = new Java.sql.Date (Date.gettime ());
System.out.println (Sqldate.gettime ());
}
catch (Exception ex) {
System.out.println (Ex.getmessage ());
}
------------------------------------------------------------
Method 2
String strdate = "2002-08-09";
StringTokenizer st = new StringTokenizer (strdate, "-");
Java.sql.Date Date = new Java.sql.Date (Integer.parseint (St.nexttoken ()),
Integer.parseint (St.nexttoken ()),
Integer.parseint (St.nexttoken ()));
Similarities and differences of Java.util.Date and Java.sql.Date
Java.sql.date,java.sql.time and Java.sql.Timestamp Three are subclasses of Java.util.Date (wrapper class).
But why data interception occurs when a value of type java.sql.Date is inserted into the Date field in the database?
Java.sql.Date is the data type that is set to match SQL date. The "normalized" java.sql.Date contains only the month-date information, and seconds and minutes are zeroed. Format similar to: YYYY-MM-DD. When we call ResultSet's getdate () method to get the return value, the Java program formats the values in the database with reference to the java.sql.Date of the specification. Therefore, if the information in the non-normalized part of the database exists, it will be robbed.
This getdate is commented on in the sun-provided Resultset.java:
Retrieves the the designated column in the current row of this <code>ResultSet</code> object as a "java.sql . Date "Object in the Java programming language.
Similarly. If we put a java.sql.Date value through the Preparestatement setdate method into the database, the Java program will normalize the incoming java.sql.Date, the denormalized part will be robbed. However, we java.sql.Date generally converted by java.util.Date, such as: Java.sql.Date sqldate=new java.sql.Date (New Java.util.Date (). GetTime ()) .
Obviously, such conversion of java.sql.Date is often not a normative java.sql.Date. To save the exact value of the java.util.Date,
We need to use Java.sql.Timestamp.
Calendar
Calendar calendar=calendar.getinstance ();
Get current time, declare time variable
int Year=calendar.get (calendar.year);
Get year
int Month=calendar.get (calendar.month);
Get the month, but the month to add 1
month=month+1;
int Date=calendar.get (calendar.date);
Date obtained
String today= "+year+"-"+month+"-"+date+" ";
Transferred from: http://www.blogjava.net/yutian727/archive/2007/11/12/159888.html