How to convert Java.util.Date to Java.sql.Date

Source: Internet
Author: User

The following information will help you

1. Convert Java.util.Date to Java.sql.Date

Java.util.Date utildate = new Java.util.Date ();

Java.sql.Date sqldate = new Java.sql.Date (Utildate.gettime ());

2. To insert into the database and the corresponding field is of the date type

You can use the preparedstatement.setdate (int, java.sql.Date) method

The java.sql.Date can be obtained using the above method

You can also use the database to provide to_date functions

such as existing UD

To_date (New SimpleDateFormat (). Format (UD, "Yyyy-mm-dd HH:mm:ss"),

"Yyyy-mm-dd HH24:MI:SS")

Note that in Java, the format differs from the format provided by the database

Sql= "UPDATE tablename set Timer=to_date ('" +x+ "', ' Yyyymmddhh24miss ') where ..."

The x here is similar to the variable: 20080522131223

3. How to convert a string in "YYYY-MM-DD" format to Java.sql.Date

Method 1

Simpledateformatbartdateformat = new SimpleDateFormat ("Yyyy-mm-dd");

Stringdatestringtoparse= "2007-7-12";

try{

Java.util.datedate= Bartdateformat.parse (Datestringtoparse);

java.sql.datesqldate= New Java.sql.Date (Date.gettime ());

System.out.println (Sqldate.gettime ());

}

catch (Exception ex) {

System.out.println (Ex.getmessage ());

}

How to convert Java.util.Date to Java.sql.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.