Reputation: 97
I try to get date from edittext in Android, but code return wrong text.
Java code:
SimpleDateFormat df = new SimpleDateFormat("dd-MM-YYYY");
java.util.Date myDate;
myDate = df.parse(editText1.getText().toString());
String myText = myDate.getDay() + "-" + myDate.getMonth() + "-" + myDate.getYear() + "abcd";
XML code:
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="date">
When I write text "23-08-2013" in EditText, code return "5-7-113-abcd". What is wrong? How can I get correct date from EditText?
Upvotes: 7
Views: 56554
Reputation: 86296
I recommend that you consider java.time, the modern Java date and time API, for your date work.
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
String textFromEditText = "23-08-2013";
LocalDate myDate = LocalDate.parse(textFromEditText, dateFormatter);
String myText = myDate.format(dateFormatter);
System.out.println("Formatted again: " + myText);
Output is:
Formatted again: 23-08-2013
Just as you use a formatter for parsing a string to a LocalDate
, also use a formatter when formatting the ohter way, from a LocalDate
to a String
. And if you want the same format again, just use the same formatter again.
The Date
class is poorly designed and often confusing. The answer by Ken Wolf is correct about the confusing behaviour of the methods that have been deprecated since Java 1.1 (February 1997).
There is one more problem in your code, the use of uppercase YYYY
in your format pattern string. Uppercase YYYY
is for week based year and only useful with a week number. Lower case yyyy
is for year of era. In fact, because of this error I got 1-11-112abcd
from your code, which is still further off than the result that you reported. All three numbers are incorrect.
java.time works nicely on both older and newer Android devices. It just requires at least Java 6.
org.threeten.bp
with subpackages.java.time
was first described.java.time
to Java 6 and 7 (ThreeTen for JSR-310).Upvotes: 0
Reputation: 1773
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);
Upvotes: 2
Reputation: 447
the Android doc is always the best reference. here is an link of of the Calendar doc page:
http://developer.android.com/reference/java/util/Calendar.html
Upvotes: 0
Reputation: 23269
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
Upvotes: 10