Q:

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

belongs to collection: Java Date and Time Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Date and Time Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to convert \'java.util.Date\... >>
<< Program to find differences of two dates in days i...