The difference and mutual transformation between java.util.Date and Java.sql.Date

Source: Internet
Author: User

Java.util.Date is used in cases other than SQL statements.
Java.sql.Date is used for SQL statements, it contains only dates and no time parts
They all have a gettime method that returns the number of milliseconds that can be built directly. Java.util.Date is the parent class of Java.sql.Date, the former is a commonly used class of representing time, we usually format or get the current time is used by him, the latter in reading and writing the database with him, because Preparedstament's setdate () The 2nd parameter of the resultset and the 2nd parameter of the GETDATE () method are java.sql.Date.

Java.sql.Date converted to Java.util.Date
Java.sql.Date date=new java.sql.Date ();
Java.util.Date d=new java.util.Date (Date.gettime ());


Java.util.Date converted to Java.sql.Date
Java.util.Date utildate=new Date ();
Java.sql.Date sqldate=new java.sql.Date (Utildate.gettime ());
Java.util.Date utildate=new Date ();
Java.sql.Date sqldate=new java.sql.Date (Utildate.gettime ());
Java.sql.Time stime=new Java.sql.Time (Utildate.gettime ());
Java.sql.Timestamp stp=new Java.sql.Timestamp (Utildate.gettime ());


All time dates here can be SimpleDateFormat formatted format ()
SimpleDateFormat f=new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss");
F.format (STP);
F.format (stime);
F.format (sqldate);
F.format (Utildate)
Java.sql.Date sqldate=java.sql.date.valueof ("2005-12-12");
Utildate=new java.util.Date (Sqldate.gettime ());


Alternative means of acquiring a date:
Import Java.text.SimpleDateFormat;
Import java.util.*;
Java.util.Date Date = new Java.util.Date ();
If you want to get the YYYYMMDD format SimpleDateFormat
Sy1=new SimpleDateFormat ("YyyyMMDD");
String Dateformat=sy1.format (date);
If want to separate get year, month, day SimpleDateFormat
Sy=new SimpleDateFormat ("yyyy");
SimpleDateFormat sm=new SimpleDateFormat ("MM");
SimpleDateFormat sd=new SimpleDateFormat ("DD");
String Syear=sy.format (date);
String Smon=sm.format (date);
String Sday=sd.format (date);

The difference and mutual transformation between java.util.Date and 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.