To write some date processing with the database connection, the type of Pstmt.setdate () is java.sql.Date type, this type of conforming specification actually does not save the time and minutes in the database, so the access should use Settimestamp () or Gettimestamp ().
Finishing One:
One, to the database to save date data
Java.sql.Date Store only date data does not store time data
will lose time data
Preparedstatement.setdate (1, New Java.sql.Date (Date.gettime ()));
You can handle this.
Preparedstatement.settimestamp (1, New Timestamp (New Java.util.Date (). GetTime ()));
Ii. fetching data from the database
To get the full data, including the date and time, you can do this
Java.util.Date d = resultset.gettimestamp (1);
This is a more appropriate process to avoid some potential timestamp problems
Java.util.Date d = new Java.util.Date (Resultset.gettimestamp (1). GetTime ());
Note:
When storing to the database, you can receive the Java.util.Date type and then use the GetTime () method to get a long value representing that date object, and then construct a timestamp object into the database with this long value.
When you take it from a stored database, you can get timestamp to use his gettime () method to get a long value and then construct a Java.util.Date object with this long value so that you can manipulate the date object. For example, New SimpleDateFormat ("Yyyyy-mm-dd HH:mm:ss"). Format (Date) or format (Timestamp) All rows ~
Finishing two:
It is convenient to use timestamp to record date times, but sometimes it does not require milliseconds after the decimal digit, so you need to redefine the format when converting to string.
Timestamp into string:
SimpleDateFormat df = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
Defines a format that does not display milliseconds
Timestamp now = new Timestamp (System.currenttimemillis ());
Gets the system current time
String str = Df.format (now);
String into timestamp:
SimpleDateFormat df = new SimpleDateFormat ("Yyyy-mm-ddhh:mm:ss");
String time = Df.format (new Date ());
Timestamp ts = timestamp.valueof (time);
Finishing three:
The setdate or getdate data types that we often use in resultset are java.sql.Date, whereas in normal Java programs we are generally accustomed to using java.util.Date. So in the DAO layer we often encounter conversions between these two types of data.
The relationship between the two
Java.lang.Object
|
+---Java.util.Date
|
+----Java.sql.Date
Mutual conversion
1. Use the gettime () function
These two classes provide the gettime () function to return the corresponding number of milliseconds (long). You can use this function to implement transformations:
Java.util.Date utildate = new Java.util.Date (Sqldate.gettime ()); SQL-> Util
Java.sql.Date sqldate = new Java.sql.Date (Utildate.gettime ()); Util-> SQL
2. Implementing transformations using the SimpleDateFormat class
SimpleDateFormat is a specific class that formats and analyzes data in a country-sensitive manner. It allows formatting (date-> text), parsing (text-> date), and normalization.
SimpleDateFormat DateFormat = new Simpledateformate ("Yyyy-mm-dd HH:mm:ss");
Java.util.Date utildate = Dateformat.parse (sqldate.tostring ());
3. Direct conversion
As Java.sql.Date is inherited from the java.util.Date, it can be directly used: Utildate = sqldate;
4. Alternative methods of obtaining dates:
SimpleDateFormat sy=new SimpleDateFormat ("yyyy");
SimpleDateFormat sm=new SimpleDateFormat ("MM");
SimpleDateFormat sd=new SimpleDateFormat ("DD");
String Syear=sy.format (date);
String Smon=sm.format (date);
String Sday=sd.format (date);
The getyear () in the
Ps:1. Java.util.Date class should be added with 1900 to get the actual value, GetMonth () to add 1. &NBSP
2. Converting from string to Date: date.valueof (str), remember when the fuse is to be the date package of the SQL, is not a util date package
3. String to timestamp conversion timestamp ts = Timestamp.valueof (time);