2016-01-25 41 views
1

我正在开发Android Studio 1.5.1中的项目。我有 TextView startDate =(TextView)findViewById(R.id.newevent_startdate);从文本转换为日期android

现在我需要使用startDate的值,但作为java.sql.Date类型。我该如何转换?

+0

这取决于日期的格式,你没有指定,但[这](http://stackoverflow.com/questions/10413350/date-conversion-from-string-to-sql- date-in-java-giving-different-output)应该有你需要的所有部分。 –

回答

1

假设您的TextView中的startDate格式为“yyyy-mm-dd”,即2016-01-26,那么您可以使用下面的代码将其解析为Date对象。

String startDateString = startDate.getText().toString() ; // where startDate is your TextView 
SimpleDateFormat simpleDateFormat= new SimpleDateFormat("yyyy-MM-dd"); // same date format as your TextView supports 
try { 
    Date date = simpleDateFormat.parse(startDateString); // parses the string date to get a date object 
} catch (ParseException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
}