When we are dealing with frontend application we must always take an object of java.util.Date for representing date and time information but when we are dealing with database date?s we must take an object of java.sql.Date
In order to convert java.util.Date into java.sql.Date we have two ways:
First way:
Read the date in string format
For example:
System.out.println ("ENTER THE DATE IN DD-MM-YYYY"); String d1=dis.readLine ();
To convert string date into java.sql.Date we have the following method:
For example:
java.sql.Date sd=java.sql.Date.valueOf (d1);
Second way:
In order to convert string date into java.util.Date we must use the following class:
For example:
SimpleDateFormat sdf=new SimpleDateFormat ("DD-MM-YYYY"); java.util.Date ud=sdf.parse (d1);
To convert java.util.Date into java.sql.Date we must use the following statement:
For example:
java.sql.Date sd=new java.util.Date (ud.getTime ());