Date from EditText
getDay()
returns the day of week, so this is wrong. Use getDate()
.
getMonth()
starts at zero so you need to add 1 to it.
getYear()
returns a value that is the result of subtracting 1900 from the year so you need to add 1900 to it.
abcd
- well, you are explicitly adding that to the end of the string so no surprises there :)
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
Date myDate;
try {
myDate = df.parse(date);
String myText = myDate.getDate() + "-" + (myDate.getMonth() + 1) + "-" + (1900 + myDate.getYear());
Log.i(TAG, myText);
} catch (ParseException e) {
e.printStackTrace();
}
All of these are deprecated though and you should really use a Calendar
instead.
Edit: quick example of Calendar
Calendar cal = Calendar.getInstance();
cal.setTime(myDate);
cal.get(Calendar.DAY_OF_MONTH); // and so on
Please, don`t be afraid to look at Java Documentation. These methods are deprecated. (And btw you are using wrong methods for getting values) Use Calendar:
Calendar c = Calendar.getInstance();
c.setTime(myDate)
String dayOfMonth = c.get(Calendar.DAY_OF_MONTH);
String month = c.get(Calendar.MONTH);
String year = c.get(Calendar.YEAR);