我已经使用日历类来获取当前日期。现在我想将该日期转换为日期格式,以便它可以以“yyyy-mm-dd”格式存储在数据库中。我尝试使用 SimpleDate 格式对其进行转换,但出现错误。代码如下。帮助。提前致谢。
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);
}
}