---restore content starts---
JAVA processing Time-java.sql.Date, java.util.Date and the conversion method of the Date field in the database, and the conversion of the date class and the string under the Util package
The date time class used in the Java environment is typically java.util.Date, while the database declares a variable of type date, which corresponds to Java.sql.Date, so when the Java.util.Date time object is obtained in Java code,
Need to convert to java.sql.Date class to fit into the database
1, how to convert Java.util.Date to java.sql.Date?
Java.util.Date utildate=new java.util.Date ();
Java.sql.Date sqldate=new java.sql.Date (Utildate.gettime ());
2. If you want to insert into the database and the corresponding field is of the date type
Then you can use the preparedstatement.setdate (int parameterindex, Java.sql.Date sqldate) method
The first parameter is the parameter index of the placeholder, and the second parameter is a java. SQL. Date object of date type
The java.sql.Date can be obtained using the above method
PreparedStatement PST;
Java.util.Date utildate=new java.util.Date ();
pst.setdate (int parameterindex, java.sql.Date (Utildate.gettime ())); The date here is in SQL, and we get the dates .
Pst.settime (int parameterindex, Java.sql.Time (Utildate.gettime ()))//sql The time in the package:
Pst.setobject (int parameterindex, Java.sql.Timestamp (Utildate.gettime ())); :: Get the date and time
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
3. How to convert a string in "YYYY-MM-DD" format to Java.sql.Date
Method 1
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd"); /**YYYY-MM-DD format for strings **/
string stringtoparse = "2007-7-12"; /** to convert to java.util.Date strings **/
try {
java.util.Date Date = Sdf.parse (stringtoparse);
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+" ";
---restore content ends---
JAVA processing Time-java.sql.Date, java.util.Date and the conversion method of the Date field in the database, and the conversion of the date class to the string under the Util package