The code demonstrates how to convert a java.util.Date object to a java.sql.Date object. First, a new java.util.Date object is created using the default constructor, which creates a Date object with the current date and time.
Then, a new java.sql.Date object is created by passing the time value of the java.util.Date object to the constructor of java.sql.Date. Next, a DateFormat object is created with a specific date format of "dd-MM-yyyy". This is used to format the java.util.Date object into a string with the specified format. Finally, the formatted string and the java.sql.Date object are printed to the console.
import java.util.*; import java.sql.*; import java.text.*; public class Convert_Date { public static void main(String[] args) { java.util.Date util_dt = new java.util.Date(); java.sql.Date sql_dt = new java.sql.Date(util_dt.getTime()); DateFormat dtFormat = new SimpleDateFormat("dd-MM-yyyy"); final String str_dt = dtFormat.format(util_dt); System.out.println("Util Date : " + str_dt); System.out.println("SQL Date : " + sql_dt); } }
Util Date : 17-12-2022 SQL Date : 2022-12-17
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions