Java program to convert 'java.util.Date' into 'java.sql.Date'

Learn how to Java program to convert 'java.util.Date' into 'java.sql.Date' in Java?
Submitted by Nidhi, on March 29, 2022

Problem Solution:

In this program, we will create an object of the "java.util.Date" class and convert it into the "java.sql.Date" class and print the result.

Program/Source Code:

The source code to convert java.util.Date into java.sql.Date is given below. The given program is compiled and executed successfully.

// Java program to convert java.util.Date 
// into java.sql.Date

import java.util.*;
import java.sql.*;
import java.text.*;

public class Main {
  public static void main(String[] args) {
    java.util.Date utilDate = new java.util.Date();
    java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());

    DateFormat dtFormat = new SimpleDateFormat("dd-MM-yyyy");
    final String strDate = dtFormat.format(utilDate);

    System.out.println("Util date: " + strDate);
    System.out.println("SQL date : " + sqlDate);
  }
}

Output:

Util date: 29-03-2022
SQL date : 2022-03-29

Explanation:

In the above program, we created a class Main that contains a static method main(). The main() method is the entry point for the program, here we created the object of the "java.util.Date" class and converted it into "java.sql.Date" class, and printed the result.

Java Date and Time Programs »



Related Programs

ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.