Java.sql.date,java.sql.time and Java.sql.Timestamp Three are subclasses of Java.util.Date (wrapper class). Java.sql.Date is a subclass of Java.util.Date, a thin wrapper that wraps a millisecond value, allowing JDBC to identify the millisecond value as a SQL DATE value. The millisecond value represents the number of milliseconds that have elapsed since the January 1, 1970 00:00:00 GMT. In order to be consistent with the definition of SQL DATE, the millisecond value that is wrapped by the java.sql.Date instance must be normalized by setting the time, minute, second, and millisecond to zero in the specific time zone associated with the instance. Plainly, Java.sql.Date is a type that corresponds to the database date, and java.util.Date is a pure Java date. Methods of inheriting from class Java.util.Date after, before, clone, CompareTo, Equals, GetDate, GetDay, GetMonth, GetTime, gettimezoneoffs ET, getYear, hashcode, Parse, SetDate, Setmonth, Setyear, togmtstring, toLocaleString, Utc sql. Date also has time if you do not want to use Oracle's To_date function, you can generate your own SQL. Date Object String s = "2012-06-21 00:10:00"; SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); java.util.Date D1 = Sdf.parse (s); Convert the string to util first. Date object Java.sql.Date d2 = new Java.sql.Date (D1.gettime ()); Convert to SQL again. The Date object wants to insert time and seconds into the database, in addition to the To_date () method of the Oracle database. We can use the timestamp class to achieve. Java.sql.Date is the time after normalization, when the seconds and minutes are truncated. 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. : in the sun-provided Resultset.java to annotate getdate retrieves The of the designated column in the Curr Ent row of this <code>ResultSet</code> object as a "Java.sql.Date" object in the Java programming LANGUAGE.&NB Sp 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, the conversion of java.sql.Date is often not a normative java.sql.Date. To save the exact value of java.util.Date, we need to use Java.sql.timestamp.eg: [java] View Plaincopy<span style= "FONT-SIZE:18PX;" > string s= "2012-01-02 03:12:21"; SimpleDateFormat sp = new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss"); java.util.Date du = Sp.parse (s); &NBSp Java.sql.Timestamp st = new Java.sql.Timestamp (Du.gettime ());</span> Write a date value to the database Found by Java.util.Date dump for java.sql.Date, only months and days, no time: java.util.date ud = new Java.util.Date (); java.sql.date sd = new Java.sql.Date (Ud.gettime ()); This is not possible, the user must be at least accurate to points. Because java.sql.Date to be able to comply with the SQL date standard, all the time and seconds are 0. Only use timestamp to save, because timestamp is a subclass, so there is no need to modify the data type in the well-written bean. pstmt.settimestamp (New Java.sql.Timestamp (Calendar.getinstance (). GetTime (). GetTime ())); //Current Time pstmt.settimestamp (+, New Java.sql.Timestamp (Userfile.getcreatetime (). GetTime ())); //Specify time for a specified time you can also use the SetTime () method of the Calendar class to set [Java] View Plaincopy<span style= "FONT-SIZE:18PX;" >calendar cal = Calendar.getinstance (); SYSTEM.OUT.PRINTLN (Cal.gettime (). GetTime ()); String string= "2012-01-01 01:02:03"; SimpleDateFormat sp = new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss"); java.util.Date da = sp.parse (string); cal.settime (DA); SYSTEM.OUT.PRINTLN (Cal.gettime (). GetTime ());</span> Timestamp is required to insert a time and seconds in the database. Generally do this kind of operation with the framework mostly, I say hibernate it. In a database table, the field type is set to the date data type, and the field type mapped in the code is set to Timestamp type, private Timestamp date; In the mapping file <property name= "date" type= "timestamp" column= "SJ"/> type is also timestamp type. The value of the assignment is evaluated directly by using the previous operation to get the timestamp object to the desired time. Then you can use Hibernate execution method to save time and seconds in the database. (but that doesn't seem like a big deal, most of it is directly varchar2 in, taking time to to_date or working in code) There is a need to say that the format of the time when using SimpleDateFormat yyyy. MM.DD is a year or so and if you want the format time to be 12-hour, use HH:MM:SS if you want the format time to be 24-hour, use HH:MM:SS, on the code: [Java] view Plaincopysimpledateformat SS = New SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss");//12-hour [java] View Plaincopysimpledateformat Sdformat = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");//24-hour [java] View plaincopydate d = new Date (); SimpleDateFormat ss = new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss");//12-hour SYSTEM.OUT.PRINTLN (Ss.format (d)); Date date = new Date (); SimpleDateFormat Sdformat = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");//24-hour system String lgtime = Sdformat.format (date); SYSTEM.OUT.PRINTLN (lgtime); The result is 2008-05-28 01:32:54 2008-05-28 13:32:54 date class, which has been seldom used. More using calendar calendar date = calendar.getinstance (); Date.get (calendar.hour_of_day );//Get 24-hour mechanism date.get ( Calendar.hour);// Get 12-hour mechanism
Java.sql.date differs from Java.util.date and the time in the database when the band is inserted in seconds