java sql date class

java.util.Date

The java.sql.Date class inherits java.util.Date class. It adds formatting and parsing operations to support the JDBC.

Constructor of java.sql.Date class:

    • Date(long milliseconds): Construct a Date using a milliseconds time value.

Java SQL Date Class Methods

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

java.sql.date class example

package com.w3schools;
 
import java.sql.Date;
 
public class SQLDateTest {
	public static void main(String args[]){
		long millis=System.currentTimeMillis();  
        Date date=new Date(millis);  
        System.out.println(date);   
	}
}

Output:

2018-04-09

Java string to java.sql.date example

package com.w3schools;
 
import java.sql.Date;
 
public class StringToSQLDateTest {
	public static void main(String args[]){
	    String str="2018-04-09";  
	    //String to sql date conversion 
	    Date date=Date.valueOf(str);
	    System.out.println(date);    
	}
}

Output:

2018-04-09