JDBC Tutorial

Dealing With Date

  1. In order to deal with java date or frontend application date we should use a class called java.util.Date
  2. In order to deal with database date or backend application date we should use a class called java.sql.Date
  3. java.sql.Date is the subclass of java.util.Date

Converting java.util.Date into java.sql.Date:

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:

java sql date

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:

java sql date second

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 ());