Java.util.Date and Java.sql.Date and System.currenttimemillis () related to time issues

Source: Internet
Author: User

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

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.