2

I'm working on a project in Android Studio 1.5.1. and I have TextView startDate = (TextView) findViewById(R.id.newevent_startdate);

now I need to use the value of startDate but as a type of java.sql.Date. how can I convert this?

Nicky Mirfallah
  • 1,004
  • 4
  • 16
  • 38
  • It depends on the format of the date and you didn't specify it but [this](http://stackoverflow.com/questions/10413350/date-conversion-from-string-to-sql-date-in-java-giving-different-output) should have all the pieces you need. – George Mulligan Jan 25 '16 at 19:29

1 Answers1

2

Suppose if you have a startDate in your TextView with this format "yyyy-mm-dd" i.e 2016-01-26, then you can parse that to Date object using below code.

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();  
}
Mukesh Rana
  • 4,051
  • 3
  • 27
  • 39