×

Java Programs

Java Practice

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 statement

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.

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

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



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.