Tutorials

Java Date


java.sql.Date


The java.sql.Date class extends java.util.Date class.

java.sql.Date is useful when working with JDBC to store date. 

 

The java.sql.Date class provides method and constructors to work with only date in java. 

Example on java.sql.Date:


package Date;

import java.sql.Date;

public class SqlDateTime {

      public static void main(String[] args) {

            long dateMillis = System.currentTimeMillis();
            Date date = new Date(dateMillis);

            System.out.println(date);

      }

}

OUTPUT:

2017-12-21

 

Example on java.sql.Date:


package Date;

import java.sql.Date;

public class SqlDateTime {

      public static void main(String[] args) {           

            String dateString = "2017-12-21";
            Date date = Date.valueOf(dateString);

            System.out.println(date);

      }

}

OUTPUT:

2017-12-21

 

java.sql.Date Constructor

From the above two example, we can see two constructors in action:

  1. Date today = new Date(long dateMillis)

Date(long dateMillis) ⇒ This constructor takes time in milliseconds as parameter and create sql date instance to represent current date and time.

 

Note: We are not discussing about the deprecated constructors in this tutorial.



Please Share this page
Views : 24
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!