Java.sql.date and Java.util.date differences and time of day and seconds when inserting bands in a database

Source: Internet
Author: User
Tags time and seconds wrapper

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
    1. <span style="font-size:18px;" > String s= "2012-01-02 03:12:21"  ;
    2. SimpleDateFormat sp = new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss");
    3. java.util.Date du = Sp.parse (s);
    4. 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:

    1. java.util.Date ud = new java.util.Date ();
    2. 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.

    1. pstmt.settimestamp ( new Java.sql.Timestamp (Calendar.getinstance (  ). GetTime (). GetTime ()); //Current time
    2. 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
  1. <span style="font-size:18px;"  >calendar cal = Calendar.getinstance ();
  2. System.out.println (Cal.gettime (). GetTime ());
  3. String string="2012-01-01 01:02:03";
  4. SimpleDateFormat sp = new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss");
  5. java.util.Date da = sp.parse (string);
  6. cal.settime (DA);
  7. 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
    1. SimpleDateFormat ss = New SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss" ); //12-hour system   


[Java]View Plain Copy
    1. SimpleDateFormat sdformat = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss" ); //24-hour system   


[Java]View Plain Copy
  1. Date d = new date ();
  2. SimpleDateFormat ss = new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss"); //12-hour system   
  3. System.out.println (Ss.format (d));
  4.   
  5. Date date = new date ();
  6. SimpleDateFormat Sdformat = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); //24-hour system   
  7. String lgtime = Sdformat.format (date);
  8. System.out.println (lgtime);
  9.   
  10.   
  11. result is
  12. -- : + : Wu   
  13. --- - £ º 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.