java.sql.Date – Java SQL Date

Filed Under: Java

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

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.


package com.journaldev.examples;

import java.sql.Date;

/**
 * Java initialize Sql Date object
 * 
 * @author pankaj
 *
 */
public class SqlDateInitialization {

	public static void main(String[] args) {
		long d = System.currentTimeMillis();
		Date date = new Date(d);
		System.out.println(date);
	}
}

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.

package com.journaldev.examples;

import java.sql.Date;

/**
 * Java setTime in sql Date
 * 
 * @author pankaj
 *
 */
public class SqlDateSetTime {

	public static void main(String[] args) {
		Date date = new Date(0);
		System.out.println("Before setTime: "+date);
		long d = System.currentTimeMillis();
		date.setTime(d);
		System.out.println("After setTime: "+date);
	}
}

Output:


Before setTime: 1970-01-01
After setTime: 2018-07-22
  1. toString(): This method formats a date in the date escape format yyyy-mm-dd and returns the string in yyyy-mm-dd.

package com.journaldev.examples;

import java.sql.Date;

/**
 * Java toString method example
 * 
 * @author pankaj
 *
 */
public class SqlDateToStringExample {

	public static void main(String[] args) {
		long d = System.currentTimeMillis();
		Date date = new Date(d);
		String stringDate = date.toString();
		System.out.println(stringDate);
	}
}

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.

package com.journaldev.examples;

import java.sql.Date;

/**
 * Java valueOf method example
 * 
 * @author pankaj
 *
 */
public class SqlDateValueOfMethodExample {

	public static void main(String[] args) {
		String str = "2018-7-22";
		
		System.out.println(Date.valueOf(str));
	}
}

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


package com.journaldev.examples;

import java.sql.Date;

/**
 * Java convert java.util.Date to java.sql.Date
 * 
 * @author pankaj
 *
 */
public class UtilDatetoSQLDateExample {

	public static void main(String[] args) {
		java.util.Date utilDate = new java.util.Date();
		Date sqlDate = new Date(utilDate.getTime());
		
		System.out.println("Util Date: "+utilDate);
		System.out.println("SQL Date: "+sqlDate);
	}
}

Output:


Util Date: Sun Jul 22 18:10:58 IST 2018
SQL Date: 2018-07-22

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.

package com.journaldev.examples;

import java.sql.Date;
import java.time.LocalDate;

/**
 * Java 8 SQL Date methods example
 * 
 * @author pankaj
 *
 */
public class SQLDateJava8MethodsExample {

	public static void main(String[] args) {
		Date date = new Date(System.currentTimeMillis());
		
		LocalDate localDate = date.toLocalDate();
		
		Date sqldate = Date.valueOf(localDate);
		
		System.out.println("Local Date: "+localDate);
		System.out.println("SQL Date: "+sqldate);
	}
}

Output:


Local Date: 2018-07-22
SQL Date: 2018-07-22

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

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages