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

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

Problem Solution:

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

Program/Source Code:

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

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

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

public class Main {
  public static void main(String[] args) {

    long ms = System.currentTimeMillis();

    java.sql.Date sqlDt = new java.sql.Date(ms);
    java.util.Date utilDt = new java.util.Date(sqlDt.getTime());

    DateFormat dtFormat = new SimpleDateFormat("dd/MM/yyyy");

    final String strDate = dtFormat.format(utilDt);

    System.out.println("Util Date: " + strDate);
    System.out.println("Sql Date:  " + sqlDt);
  }
}

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.sql.Date" class and converted it into "java.util.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.