All for Joomla All for Webmasters


The java.sql.Date class represents only date in java. It inherits java.util.Date class.

The java.sql.Date instance is widely used in JDBC because it represents the date that can be stored in database.

Some constructors and methods of java.sql.Date class has been deprecated. Here, we are not giving list of any deprecated constructor and method.

java.sql.Date Constructor

No. Constructor Description
1) Date(long milliseconds) Creates a sql date object for the given milliseconds since January 1, 1970, 00:00:00 GMT.

java.sql.Date Methods

No. Method Description
1) void setTime(long time) changes the current sql date to given time.
2) Instant toInstant() converts current sql date into Instant object.
3) LocalDate toLocalDate() converts current sql date into LocalDate object.
4) String toString() converts this sql date object to a string.
5) static Date valueOf(LocalDate date) returns sql date object for the given LocalDate.
6) static Date valueOf(String date) returns sql date object for the given String.

java.sql.Date Example: get current date

Let’s see the example to print date in java using java.sql.Date class.

  1. public class SQLDateExample {
  2.     public static void main(String[] args) {
  3.         long millis=System.currentTimeMillis();
  4.         java.sql.Date date=new java.sql.Date(millis);
  5.         System.out.println(date);
  6.     }
  7. }



Java String to java.sql.Date Example

Let’s see the example to convert string into java.sql.Date using valueOf() method.

  1. import java.sql.Date;
  2. public class StringToSQLDateExample {
  3. public static void main(String[] args) {
  4.     String str=“2015-03-31”;
  5.     Date date=Date.valueOf(str);//converting string into sql date
  6.     System.out.println(date);
  7. }
  8. }


submit to reddit

Leave a Reply