Android使用SimpleDateFormat解析字符串到日期时间

我有String 11/08/2013 08:48:10

我使用SimpleDateFormat("MM/dd/yyyy HH:mm:ss")

当我解析它时抛出一个exception:不可解析的日期

它出什么问题了 ?

  String result = han.ExecuteUrl("http://"+han.IP+":8015/api/Values/GetLastChange"); Log.d("Dal","result date time "+result); #result is 11/08/2013 08:48:10 SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); Date convertedDate = new Date(); try { convertedDate = dateFormat.parse(result); } catch (ParseException e) { e.printStackTrace(); } 

它的工作尝试解析你的日期,就像这样..

 String dtStart = "11/08/2013 08:48:10"; SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); try { date = format.parse(dtStart); System.out.println("Date ->" + date); } catch (ParseException e) { e.printStackTrace(); } 

工作代码在这里。

您可以使用下面的代码将String转换为Date

 String myStrDate = "11/08/2013 08:48:10"; SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); try { Date date = format.parse(myStrDate); System.out.println(date); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } 

对于反向,它意味着将DateDate转换为String

 SimpleDateFormat myFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); try { Date date = new Date(); String datetime = myFormat.format(date); System.out.println("Current Date Time in give format: " + datetime); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } 

有关日期和时间格式的更多参考。 访问开发者网站