java.sql.Date Examples

The java.sql.Date extends java.util.Date class. java.sql.Date is used in the JDBC API, as a wrapper around java.util.Date that handles SQL specific requirements. This class exists to represent SQL DATE, which keeps years, months and days, with no time. java.sql.Date should be used only when working with databases, like to set a date on a java.sql.PreparedStatement, get a date from a java.sql.ResultSet, working with java.sql.SQLData, etc.

Initialize java.sql.Date

We can instantiate a java.sql.Date object using following constructor:

  • Date(long date): Constructs a Date object using the given milliseconds time value.

Here the example:

SqlDateInitExample.java
import java.sql.Date;

public class SqlDateInitExample {
    
    public static void main(String[] args) {
        long now = System.currentTimeMillis();
        Date sqlDate = new Date(now);
        System.out.println("currentTimeMillis: " + now);
        System.out.println("SqlDate          : " + sqlDate);
        System.out.println("SqlDate.getTime(): " + sqlDate.getTime());
    }
}
                    

Which will produce (result may vary)

currentTimeMillis: 1563925312859
SqlDate          : 2019-07-24
SqlDate.getTime(): 1563925312859

Or, you can use static valueOf(...) methods to get an instance of java.sql.Date object

  • static Date valueOf(String s): Converts a string in JDBC date escape format to a Date value.
  • static Date valueOf(LocalDate date): Obtains an instance of Date from a LocalDate object with the same year, month and day of month value as the given LocalDate. This static method available for Java 8 and above.
SqlDateValueOfExample.java
import java.sql.Date;
import java.time.LocalDate;
import java.time.Month;

public class SqlDateValueOfExample {
    
    public static void main(String[] args) {
        Date sqlDate1 = Date.valueOf("1980-04-09");
        System.out.println("SqlDate1: " + sqlDate1);
        
        Date sqlDate2 = Date.valueOf(LocalDate.of(1983, Month.JULY, 12));
        System.out.println("SqlDate2: " + sqlDate2);
    }
}
                    

SqlDate1: 1980-04-09
SqlDate2: 1983-07-12

java.sql.Date Methods

Some of methods of java.sql.Date are deprecated. It's because java.sql.Date extends java.util.Date and thus inherits its deprecated methods, or the methods is unusable for java.sql.Date which only keeps date only, without time. Here some methods available for java.sql.Date:

  • void setTime​(long date): Sets an existing Date object using the given milliseconds time value.
  • LocalDate toLocalDate(): Creates a LocalDate instance using the year, month and day from this Date object. This method available for Java 8 and above.
  • String toString(): Formats a date in the date escape format yyyy-mm-dd.
SqlDateMethodsExample.java
import java.sql.Date;
import java.time.Instant;
import java.time.LocalDate;

public class SqlDateMethodsExample {
    
    public static void main(String[] args) {
        long now = System.currentTimeMillis();
        Date sqlDate = new Date(now);
        // implicitely call sqlDate.toString()
        System.out.println("SqlDate  : " + sqlDate);
        
        sqlDate.setTime(356373000000L);
        System.out.println("SqlDate  : " + sqlDate);
        
        LocalDate localDt = sqlDate.toLocalDate();
        System.out.println("LocalDate: " + localDt);
        
        Instant instant = sqlDate.toInstant();
        System.out.println("Instant  : " + instant);
    }
}
                    

Which will produce (result may vary)

SqlDate  : 2019-07-24
SqlDate  : 1981-04-18
LocalDate: 1981-04-18
Exception in thread "main" java.lang.UnsupportedOperationException
        at java.sql.Date.toInstant(Unknown Source)
        at com.dariawan.datetime.SqlDateMethodsExample.main(SqlDateMethodsExample.java:58)

toInstant() method (available for Java 8 and above) always throws an UnsupportedOperationException and should not be used because SQL Date values do not have a time component.

Since java.sql.Date extends java.util.Date, so java.util.Date methods also available for java.sql.Date. Let's check following example:

SqlDateInheritMethodsExample.java
import java.sql.Date;

public class SqlDateInheritMethodsExample {
    
    public static void main(String[] args) {
        long now = System.currentTimeMillis();
        Date sqlDate1 = new Date(now);
        System.out.println("sqlDate1: " + sqlDate1);

        Date sqlDate2 = new Date(1586361600000L);
        System.out.println("sqlDate2: " + sqlDate2);
        System.out.println();

        System.out.println("sqlDate1 after sqlDate2    : " + sqlDate1.after(sqlDate2));
        System.out.println("sqlDate2 after sqlDate1    : " + sqlDate2.after(sqlDate1));

        System.out.println("sqlDate1 before sqlDate2   : " + sqlDate1.before(sqlDate2));
        System.out.println("sqlDate2 before sqlDate1   : " + sqlDate2.before(sqlDate1));
        
        System.out.println("sqlDate1 compareTo sqlDate2: " + sqlDate1.compareTo(sqlDate2));
        System.out.println("sqlDate2 compareTo sqlDate1: " + sqlDate2.compareTo(sqlDate1));        
    }
}
                    

sqlDate1: 2019-07-24
sqlDate2: 2020-04-09

sqlDate1 after sqlDate2    : false
sqlDate2 after sqlDate1    : true
sqlDate1 before sqlDate2   : true
sqlDate2 before sqlDate1   : false
sqlDate1 compareTo sqlDate2: -1
sqlDate2 compareTo sqlDate1: 1

Check out java.util.Date for more details.

Conclusion

Although java.sql.Date inherits from java.util.Date, java.sql.Date only keeps the date, not the time. If you need to keep the time, use a java.sql.Timestamp instead.