The difference between Date, Calendar, timestamp, conversion and use
1 Java.util.Date
Contains the year, month, day, time, minute, and second information.
1 // string conversion to date 2 String datestr= "2013-8-13 23:23:23"; 3 String pattern= "Yyyy-mm-dd HH:mm:ss"; 4 dateformate dateformat=new SimpleDateFormat (pattern); 5 Date date=dateformat.parse (datestr); 6
2 Java.sql.Date
Contains information about the year, month, and day, noting that the time, minute, and second information is not included .
Inherit from Java.util.Date. A field used to read and write to the time type in the database, and cannot use the Java.util.Date type to manipulate the time Type field in the database.
1 // java.util.Date conversion to java.sql.Date 2 new Java.sql.Date (Utildate.gettime ()); //
3 Java.util.Calendar
Contains the year, month, day, time, minute, second, and millisecond information.
JDK1.1 introduced to replace Java.util.Date.
1 // date to Calendar 2 Date date=new date (); 3 Calendar calendar=calendar.getinstance (); 4 calendar.settime (date); 5 6 // Calendar to date 7 Calendar ca=calendar.getinstance (); 8
4 Java.sql.Timestamp
Contains the year, month, day, time, minute, second, nanosecond (nano) information.
The
inherits from Java.util.Date. Contains more information than Java.sql.Date. Used in database-related operations, such as Rs.gettimestamp,ps.settimestamp. For example, if a field in a database is hiredate as the date type of Oracle, the year, month, day, time, minute, and second information can be removed when using Gettimestamp, but only the year, month, and day information can be removed when using getdate. Therefore, it is generally recommended to use Gettimestamp.
1 // Java.util.Calendar conversion to Java.sql.Timestamp 2 New Timestamp (Calendar.getinstance (). Gettimeinmillis ()); 3 // java.util.Date conversion to Java.sql.Timestamp 4 New Timestamp (Date.gettime ()); 5 // string conversion to java.sql.timestamp,string format: Yyyy-mm-dd hh:mm:ss[.f ...], square brackets indicate Optional 6
Database Time Type operations
1 inserting time in the database
PreparedStatement PS = con.preparestatement ("INSERT into TableName (daddtime) VALUES (?)");
There are three ways of doing this:
1) ps.setdate (1,new java.sql.Date (System.currenttimemillis ())); 2) Ps.settime (2,new java.sql.Time (System.currenttimemillis ())); 3) Ps.settimestamp (3,new Java.sql.Timestamp (System.currenttimemillis ()));
The first type is only inserted in the month of 0000-00-00
The second type only inserts time 00:00:00
The third type is inserted for the full time 0000-00-00 00:00:00.000.000 is the number of milliseconds.
2 Remove Database time
There are usually only two types:
// Remove date format: 0000-00-00 // Remove date and time format: 0000-00-00 00:00:00.000
Example:
Public classScriptInsertNo32 { Public Static voidMain (string[] args) {Connection Connection=NULL; PreparedStatement PreparedStatement1=NULL; PreparedStatement PreparedStatement2=NULL; Calendar Calendar=calendar.getinstance (); Date nowdate=NULL; Try{String Driverclass= "Com.microsoft.sqlserver.jdbc.SQLServerDriver"; String URL= "Jdbc:sqlserver://localhost:1433;databasename=water"; String userstring= "SA"; String passwardstring= "sicc2005"; Class.forName (Driverclass); Connection=drivermanager.getconnection (URL, userstring, passwardstring); String SQL1 = "INSERT into dbo. Monitordata VALUES "+" (' 2c94a03d475d56f201475d71905e0006 ', ' 2c94a03d475d56f201475d5dc8570003 ',?,?, ?)"; String SQL2= "INSERT into dbo. Monitordata VALUES "+" (' 2c94941448a657ff0148a718521c0007 ', ' 2c94941448a657ff0148a66db3df0003 ',?,?, ?)"; PreparedStatement1=connection.preparestatement (SQL1); PreparedStatement2=connection.preparestatement (SQL2); Nowdate=NewDate (); Calendar.settime (nowdate); Random Random=NewRandom (); DecimalFormat DF=NewDecimalFormat ("0.00"); for(inti = 0; I < 5; ++i) {DoubleVal1 = ((Double) (Random.nextint ()%80)/10); DoubleVal2 = ((Double) (Random.nextint ()%1800)/10); //System.out.println ("val:" + Df.format (val));Val1 =double.parsedouble (Df.format (VAL1)); Val2=double.parsedouble (Df.format (val2)); Val1=Math.Abs (VAL1); Val2=Math.Abs (VAL2); Calendar.add (Calendar.minute,5); Timestamp Timestamp=NewTimestamp (Calendar.gettimeinmillis ()); Preparedstatement1.settimestamp (1, timestamp); Preparedstatement1.settimestamp (2, timestamp); Preparedstatement2.settimestamp (1, timestamp); Preparedstatement2.settimestamp (2, timestamp); Preparedstatement1.setdouble (3, Val1); Preparedstatement2.setdouble (3, Val2); Preparedstatement1.execute (); Preparedstatement2.execute (); } } Catch(Exception e) {e.printstacktrace (); } finally { Try { if(Connection! =NULL) {connection.close (); } } Catch(Exception e) {e.printstacktrace (); } Try { if(PreparedStatement1! =NULL) {preparedstatement1.close (); } } Catch(Exception e) {e.printstacktrace (); } Try { if(PreparedStatement2! =NULL) {preparedstatement2.close (); } } Catch(Exception e) {e.printstacktrace (); } } }}
Resources:
http://blog.csdn.net/kingzone_2008/article/details/9256287
Http://wenku.baidu.com/link?url= Hz9q8woifhnmnsmenlkpdjpjctfo0wyadcw6vr-ju3lw7mq-pxuodafi-fqx5tkqd1mccf9afn4stt-q3t67q4rueztpnsxswmhgd-fg2lu
Manipulating database datetime data with Java