Home » java.sql.Date

java.sql.Date

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

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

Some constructors and methods of java.sql.Date class has been deprecated. Here, we are not giving the 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 the java.sql.Date class.

FileName: SQLDateExample.java

Test it Now

Output:

2015-03-30  

Java String to java.sql.Date Example

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

FileName: StringToSQLDateExample.java

Test it Now

Output:

2015-03-31  

java.sql.Date Example: void setTime()

Let’s see the working of the setTime() method.

FileName: SetTimeExample.java

Output:

Initial date is: Fri Nov 26 11:52:20 GMT 2021  Date after the setting the time is: Thu Jan 01 00:16:40 GMT 1970  

java.sql.Date Example: void toLocalDate()

Let’s see the working of the toLocalDate() method.

FileName: ToLocalDateExample.java

Output:

The date is: 2021-11-26  

java.sql.Date Example: void toInstant()

Let’s see the working of the toInstant() method.

FileName: ToInstantExample.java

Output:

The original Date is: Thu Aug 12 12:41:01 GMT 2021  The instant is: 2021-08-12T12:41:01.635Z  

Next TopicJava Calendar

You may also like