2013-05-28 23 views
0

我想通過從JSON獲取日期,將它放入類的屬性中,並從中獲取它以便將其顯示在字符串中。放入類屬性時出現錯誤的日期格式

我的課的重要一方是:

Date _begin; 
public Date getBegin(){ return this._begin; } 
public void setBegin(Date begin){ this._begin = begin; } 

和我有Testoutput這裏(JSON的環路的重要組成部分)使用它:

SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); 
dateFormat.setTimeZone(TimeZone.getTimeZone("Europe/Berlin")); 
String tempDateString = c.getString("begin"); 
try { 
    Date date = null; 
    try { 
     date = dateFormat.parse(tempDateString); 
    } catch (java.text.ParseException e) { 
     e.printStackTrace(); 
    } 
    thisData.setBegin(date); 
    Log.d("SimpleDateFormat(EventActivity Template)", "dd-MM-yyyy HH:mm:ss"); 
    Log.d("SimpleDateFormat(EventActivity String)", tempDateString); 
    Log.d("SimpleDateFormat(EventActivity Output)", dateFormat.format(thisData.getBegin())); 
} catch (ParseException e) { 
    e.printStackTrace(); 
} 

而且testoutput是:

05-28 10:02:31.588: D/SimpleDateFormat(EventActivity Template)(10396): dd-MM-yyyy HH:mm:ss 
05-28 10:02:31.588: D/SimpleDateFormat(EventActivity String)(10396): 2013-09-07 20:00:00 
05-28 10:02:31.588: D/SimpleDateFormat(EventActivity Output)(10396): 05-03-0013 20:00:00 

爲什麼我回到日期這種奇怪的格式?

+0

tempDateString顯然不符合你的格式。這是行不通的。 – njzk2

+0

爲解析你使用錯誤的模式。它應該是「yyyy-MM-dd HH:mm:ss」 – Blackbelt

+0

omg。你的權利。那很明顯。對不起,也許我需要一杯咖啡。 –

回答

0

您必須添加1900年,然後添加日期然後日期返回的正確值和格式。