7

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?

Adam N
  • 97
  • 1
  • 1
  • 8

4 Answers4

10

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
Ken Wolf
  • 23,133
  • 6
  • 63
  • 84
  • Thank you :) How can I use Calenda to do it? I was trying to get date from Calendar instance, but resualts was wrong. – Adam N Jul 16 '13 at 10:51
  • Well, I'm not really sure what you're trying to do since in your example you seem to be converting to date only to convert back exactly to the same string. Use a calendar for calculations involving dates. Anyway, hope that helps. – Ken Wolf Jul 16 '13 at 10:56
2

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);
Semyon Danilov
  • 1,753
  • 1
  • 17
  • 37
0

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

JúlioCézar
  • 447
  • 5
  • 8
  • 2
    Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Rohit Gupta Jun 18 '15 at 20:24
0

java.time through desugaring or ThreeTenABP

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.

What went wrong in your code?

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.

Question: Doesn’t java.time require Android API level 26?

java.time works nicely on both older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On older Android either use desugaring or the Android edition of ThreeTen Backport. It’s called ThreeTenABP. In the latter case make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161