0

Is there any way I can remove characters from an EditText brought from a JSONObject response? Example: 2019-03-06T00:00:00 and only present the chain in this way: 2019-02-10, when making the conversion I throw an exception which is:

    java.lang.IllegalArgumentException: Illegal pattern character 'T'

Code:

 String yourJsonDateString =  jsonResponse.getString(DataManager.Birthdate);
                    try {
                        Date yourJsonDate = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS").parse(yourJsonDateString);
                        String newDateString = new SimpleDateFormat("yyyy-MM-dd").format(yourJsonDate);
                        System.out.println(newDateString);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • What are you using to parse the Json string? What you are receiving is a Date from the Json perspective not a String. – Juan Mar 22 '19 at 01:52
  • 1
    Check [this answer](https://stackoverflow.com/a/2597111/6383857) out – StaticBeagle Mar 22 '19 at 01:53
  • From the json get this string. `"2019-03-06T00:00:00"` – Gregorio Tancitaro Mar 22 '19 at 14:44
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Mar 23 '19 at 09:48
  • I cannot reproduce. Your code runs nicely on my JDK 11. – Ole V.V. Mar 23 '19 at 09:49
  • 1
    Nice link, @StaticBeagle, thanks. [This one](https://stackoverflow.com/a/54023929/5772882) may match even a bit better. – Ole V.V. Mar 23 '19 at 09:52

1 Answers1

0

To simply 2019-03-06T00:00:00 into 2019-03-06 you can try this.

String dt = dateString.substring(0, 10)

if you use <1000 year dates, or even BCE format. you can use this

String dt = dateString.substring(0, dateString.indexOf("T"))
  • 1
    Careful, this will fail for <1000 year dates. Should do something like `dateString.substring(0, dateString.indexOf("T"))` – Morgan Mar 22 '19 at 02:34