Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaHow to Convert java.util.Date to java.sql.Date in Java?

How to Convert java.util.Date to java.sql.Date in Java?

Date class is present in both java.util package and java.sql package. Though the name of the class is the same for both packages, their utilities are different. Date class of java.util package is required when data is required in a java application to do any computation or for other various things, while Date class of java.sql package is used whenever we need to store or read the data of DATE type in SQL, also Date class of java.sql package stores information only regarding the date, whereas Date class of java.util package stores both date and time information.

It must be remembered that when we need to convert one data form to another, we must use getTime() method of the Date class of java.util package.Though java.sql.Date class is a subclass of java.util.Date class, we can’t use java.sql.Date class wherever java.util.Date class must be passed, else it will violate the Liskov Substitution principle and our program will throw run time errors on execution, therefore it is not advised to pass SQL Date to methods that expect util date. Let us do discuss getTime() method prior to landing upon the implementation part.

The getTime() method of Java Date class returns the number of milliseconds since January 1, 1970, 00:00:00 GTM which is represented by the Date object.

Syntax:

public long getTime()

Parameters: The function does not accept any parameter.

Return Value: It returns the number of milliseconds since January 1, 1970, 00:00:00 GTM.

Exception: The function does not throw any exceptions.

Example:

Java




// Java Program to Convert java.sql.Date to java.util.Date
 
// Importing utility package
// Importing SQL package
import java.sql.*;
import java.util.*;
 
// Main Class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Date class of Util package contains both date and
        // time information
        java.util.Date utilPackageDate
            = new java.util.Date();
 
        // Print and display the utility package date in
        // java
        System.out.println("Util Package date in Java is : "
                           + utilPackageDate);
 
        // Date class of sql package contains only date
        // information without time
        java.sql.Date sqlPackageDate
            = new java.sql.Date(utilPackageDate.getTime());
 
        // Print and display the SQL java package
        System.out.println("SQL Package  date in Java : "
                           + sqlPackageDate);
    }
}


 
 

Output

Util Package date in Java is : Wed Mar 17 11:56:06 UTC 2021
SQL Package  date in Java : 2021-03-17

Note: The above date and time are fetched at the time program is being compiled and run. It will vary along the passage of time where the baseline for time calculations is epoch time

 

RELATED ARTICLES

Most Popular

Recent Comments