Difference and conversion between Java. util. Date and Java. SQL. Date

Source: Internet
Author: User

 

1. Set Java. util. convert date to Java. SQL. datejava. SQL. date SD; Java. util. date UD; // initialize the UD such as UD = new Java. util. date (); SD = new Java. SQL. date (ud. gettime (); 2. If you want to insert data to the database and the corresponding field is of the date type, you can use preparedstatement. setdate (INT, Java. SQL. date. SQL. date can be obtained using the above method or the to_date function provided by the database, such as the existing udto_date (New simpledateformat (). format (UD, "yyyy-mm-dd hh: mm: SS"), "YYYY-MM-DD hh24: MI: SS") Note that the representation format in Java is different from the format provided by the database SQL = "Update tablename set timer = to_date ('" + x + "', 'yyyymmddhh24miss ') where ..... "Here, X is a variable similar to: 20080522131223 3. How to convert a string in the" yyyy-mm-dd "format to Java. SQL. date method 1 simpledateformat bartdateformat = new simpledateformat ("yyyy-mm-dd"); string datestringtoparse = "2007-7-12"; try {Java. util. date = bartdateformat. parse (datestringtoparse); Java. SQL. date sqldate = new Java. SQL. date (date. gettime (); system. out. Println (sqldate. gettime ();} catch (exception ex) {system. out. println (ex. getmessage ();} method 2 string strdate = "2002-08-09"; stringtokenizer ST = new stringtokenizer (strdate, "-"); Java. SQL. date = new Java. SQL. date (integer. parseint (St. nexttoken (), integer. parseint (St. nexttoken (), integer. parseint (St. nexttoken (); Java. util. date and Java. SQL. difference between date and Java. SQL. date, Java. SQL. time and Java. SQL. three Timestamp All are subclasses of Java. util. Date (Packaging class ). But why does data truncation occur when the value of Java. SQL. date type is inserted into the Date Field of the database? Java. SQL. date is the data type set to match SQL date. The normalized java. SQL. date only contains the year, month, and day information, and the time, minute, and second milliseconds are cleared. The format is similar to yyyy-mm-dd. When we call the getdate () method of resultset to obtain the returned value, the Java program uses Java. SQL. Date of the "standard" to format the value in the database. Therefore, the information of the nonstandard part in the database will be stolen. Resultset provided by Sun. in Java, retrieves the of 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 set a java. SQL. when the date value is stored in the database through the setdate method of preparestatement, the Java program will. SQL. date normalization, the nonstandard part will be stolen. However, we use Java. SQL. date is generally set by Java. util. date conversion, such as Java. SQL. date sqldate = new Java. SQL. date (New Java. util. date (). gettime ()). obviously, the converted Java. SQL. date is often not a standard Java. SQL. date. to save Java. util. the exact value of date. We need to use Java. SQL. timestamp. use gettimestamp of resultset to obtain data, which will not cause the interception problem (For details, refer to another article: Timestamp basic knowledge and time size comparison) Java. SQL. date only stores date data, not time data
// Time data will be lost
Preparedstatement. setdate (1, new java. SQL. date (date. gettime ()));
// It can be handled in this way
Preparedstatement. settimestamp (1, new timestamp (New java. util. Date (). gettime ()));

// You can obtain the complete data, including the date and time.
Java. util. Date d = resultset. gettimestamp (1 );
// This method is more suitable to avoid some potential timestamp problems.
Java. util. Date d = new java. util. Date (resultset. gettimestamp (1). gettime ());
When storing data in the database, you can receive the java. util. date type and use the gettime () method to obtain the long value representing the date object. Then, you can use this long value to construct a timestamp object and store it in the database.
When getting data from the database, you can first get Timestamp and use its gettime () method to get the long value, and then construct a Java with this long value. util. date object. For example, new simpledateformat ("yyyyy-mm-dd hh: mm: SS"). Format (date) or format (timestamp) can be used ~ Calendar: Calendar calendar = calendar. getinstance (); // get the current time and declare the time variable int year = calendar. get (calendar. year); // get the year int month = calendar ar. get (calendar. month); // get the month, but the month must be added with 1 month = month + 1; int date = calendar ar. get (calendar. date); // obtain the date string today = "" + year + "-" + month + "-" + date + "";

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.