2012-01-06 108 views
1

我有來自以下形式輸入的日期:無法解析日期例外:格式輸入日期爲指定格式

無法解析日期:"Sun Jan 08 18:38:54 CST 2012"

我想使用的SimpleDateFormat

(formatter = new SimpleDateFormat("dd-MM-yy");) and chop off the time xx:xx:xx CST piece at the end. The date should be instead: 07-01-2012. 

如何解析上述輸入日期到這個日期格式dd-MM-yy

編輯:

String str_date = (String)this.studentForm.getDateOfBirth().getValue().toString(); 

try { 
        date = (Date)formatter.parse(str_date); // exception 
       } catch (ParseException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
+0

後你所得到的錯誤.. – 2012-01-06 01:23:14

回答

1

使用

String s = "Sun Jan 08 18:38:54 CST 2012"; 
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy"); 
SimpleDateFormat f1 = new SimpleDateFormat("dd-MM-yy"); 

Date d = formatter.parse(s); 
String d1 = f1.format(d); 
//and if you want date object create date from string d1. 
+0

我只是嘗試這樣做。仍然出現錯誤:請參閱「編輯」 – Warz 2012-01-06 01:02:25

+0

我已經更新了答案,您可以從字符串d1 – codemaster 2012-01-06 01:22:42

+0

完美創建Date對象。謝謝。你是如何從d1創建一個Date對象的:Date d3 =(Date)formatter.parse(d1); – Warz 2012-01-06 01:37:12