Java SQL Date class is part of java.sql package. java.sql.Date is a sub class of java.util.Date class.

Java SQL Date

Java SQL Date object can be obtained by wrapping around a millisecond value that allows JDBC to identify this as an SQL DATE value. A milliseconds value represents the number of milliseconds that have passed.

The difference between java.sql.Date and java.util.Date is that java.sql.Date does not represent time value; it keeps the value of date only.

java.sql_.date_

java.sql.Date Constructors

  1. Date(int year, int month, int day) (Deprecated): Creates an SQL Date object using specified values of year, month and day.
  2. Date(long date): Creates an SQL Date object using specified milliseconds time value.

Initialize SQL Date Object

Date object can be initialized using the given milliseconds time value.

Let’s have look at the below example program.

Java SQL Date Methods

Let’s have a look at the below methods of SQL Date class with examples.

  1. getHours()(Deprecated): This method returns the hour represented by this date.
  2. getMinutes()(Deprecated): This method returns the number of minutes past the hour represented by this date.
  3. getSeconds()(Deprecated): This method returns the number of seconds past the minute represented by this date.
  4. setHours(int i)(Deprecated): This method sets the hours value of this date by using specified value of int.
  5. setMinutes(int i)(Deprecated): This method sets the minutes value of this date by using specified value of int.
  6. setSeconds(int i)(Deprecated): This method sets the seconds value of this date by using specified value of int.
  7. setTime(long date): This method sets an existing Date object using the given milliseconds time value. If the given milliseconds’ value contains time information, the driver will set the time components to the time in the default time zone (the time zone of the Java virtual machine running the application) that corresponds to zero GMT.

Output:

  1. toString(): This method formats a date in the date escape format yyyy-mm-dd and returns the string in yyyy-mm-dd.

Output: 2018-07-22

  1. valueOf(String s): This method converts a string in JDBC date escape format to a Date value using specified value of s- a String object representing a date in the format “yyyy-[m]m-[d]d”. The leading zero for mm and dd may also be omitted.

Convert java.util.Date to java.sql.Date

Output:

Java 8 SQL Date Methods

Let’s have a look at the below Java 8 methods of SQL Date class with examples.

  1. toLocalDate(): This method Converts this Date object to a LocalDate. The conversion creates a LocalDate that represents the same date value as this Date in local time zone and returns a LocalDate object representing the same date value.
  2. valueOf(LocalDate date): This method Obtains an instance of Date from a LocalDate object with the same year, month and day of month value as the given LocalDate. The provided LocalDate is interpreted as the local date in the local time zone.

Output:

Also, check Java 8 Date for more about Date in java.

That’s all for Java SQL Date, I hope nothing important got missed here.

Reference: API Doc

By admin

Leave a Reply

%d bloggers like this: