I have used the Calendar class to get the current Date. Now I want to convert that date to Date format so that it can be stored in database with format "yyyy-mm-dd". I tried to convert it with using SimpleDate format but I got error. The code is as follows. Help. Thanks in advance.
class dateDemo
{
public static void main(String args[])
{
String stringdate ;
SimpleDateFormat sdf;
String year,month,day;
Calendar currentDate;
Date validity;
currentDate = Calendar.getInstance();
year = "" + currentDate.get( Calendar.YEAR );
month = "" + currentDate.get( Calendar.MONTH );
day = "" + currentDate.get( Calendar.DAY_OF_MONTH );
stringdate = year+"/"+month+"/"+day;
sdf = new SimpleDateFormat("yyyy-mm-dd");
try
{
validity = sdf.parse ( stringdate );
}
catch(Exception e)
{
System.out.println(""+e);
}
System.out.println("Current Date is:"+stringdate);
System.out.println("Date is"+validity);
}
}