我已使用Calendar類獲取當前日期。現在我想將該日期轉換爲日期格式,以便它可以以格式「yyyy-mm-dd」存儲在數據庫中。我試圖用SimpleDate格式轉換它,但我得到了錯誤。代碼如下。幫幫我。提前致謝。將字符串轉換爲'Date'類型
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);
}
}
什麼是錯誤? – 2013-04-20 12:52:49
如果您計劃使用JDBC將其插入到關係數據庫中,則不應將其轉換爲String,而應將其轉換爲java.sql.Date。 – 2013-04-20 12:56:14
你可以請更新什麼錯誤被拋出.. ?? – 2013-04-20 12:57:54