The difference between java.util.Date and java.sql.Date
- Util. Date
1 java.util.Date nowutil =new java.util.Date ();//new a util. Date2 simpledateformat sdf=new SimpleDateFormat ("Yyyymmmdd HH:mm:ss");//Formatting 3 String time = Sdf.format (nowutil); 5 System.out.println (nowutil);//Direct Output Date 6 System.out.println (time);//conversion format re-output
Output display
20,187 months 11 21:38:01
Wed Jul 21:38:01 CST 2018
2. sql. Date
1 java.sql.Date nowsql=new java.sql.Date (System.currenttimemillis ());// Get Unix time by calling System.currenttimemillis () 2 simpledateformat sdf=new SimpleDateFormat (" YYYYMMMDD HH:mm:ss "); 3 String time = Sdf.format (nowsql); 4 System.out.println (time); 5 System.out.println (nowsql);
Output display
20,187 months 11 21:49:12
2018-07-11
1 java.sql.Date nowsql=new java.sql.Date (0); 2 simpledateformat sdf=new SimpleDateFormat ("Yyyymmmdd HH:mm:ss"); 3 String time = Sdf.format (nowsql); 4 System.out.println (time); 5 System.out.println (nowsql);
Output display
Description
By contrast, it can be seen that util. The time obtained for date is the system time, SQL. The time for date is the Unix timestamp, so the current system time is also available with Java.sql.Date (System.currenttimemillis ()). Output util without specifying a format. Time information for date is more detailed (Wed Jul 21:38:01 CST 2018), SQL. Date is more coarse only for dates (2018-07-11), but SQL. Date actually has more detailed information about the time, such as the result of the test, will SQL. Date gets the time to format and then output, there is a specific hour-to-minute information.
Attention:
The java.util.Date () parameter can be empty
The java.sql.Date () parameter cannot be empty and can be java.sql.Date (long)/java.sql.Date (int,int,int). The above example , Java.sql.Date (0), takes a Unix timestamp of January 1, 1970, so use System.currenttimemills () to get the number of milliseconds from the time of the UNIX timestamp to the system time, As a long shaping parameter for Java.sql.Date ().
Therefore, to obtain the system time can be directly used java.util.Date date=new java.util.Date ();
Java.util.Date and Java.sql.Date and System.currenttimemillis () related to time issues