×

Java Programs

Java Practice

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 statement

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.

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

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



Comments and Discussions!

Load comments ↻





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