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, getTimezoneOffset, 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. Date Object |
|
you want 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.
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.
eg
[Java]View Plain Copy
- <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);
- Java.sql.Timestamp st = New Java.sql.Timestamp (Du.gettime ());</span>
Write a date value into the database, found by Java.util.Date dump to Java.sql.Date, only a month and a day, there is no time division seconds:
- 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 ()); //Specified time
You can also set the SetTime () method of the Calendar class for a specified time
[Java]View Plain Copy
- <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>
You need to use timestamp to insert 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 stored in, take time to to_date or in code)
There's one more thing to say.
The yyyy of the format time when using SimpleDateFormat. MM.DD is the date of the year. 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 Plain Copy
- SimpleDateFormat ss = New SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss" ); //12-hour system
[Java]View Plain Copy
- SimpleDateFormat sdformat = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss" ); //24-hour system
[Java]View Plain Copy
- Date d = new date ();
- SimpleDateFormat ss = new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss"); //12-hour system
- 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);
-
-
- result is
- -- : + : Wu
- --- - £ º Wu
The date class is seldom used. More use of Calendar
Calendar Date = Calendar.getinstance ();
Date.get (Calendar. Hour_of_day );//Get 24-hour mechanism
Date.get (Calendar. HOUR); /Get a 12-hour mechanism
Java.sql.date and Java.util.date differences and time of day and seconds when inserting bands in a database