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