-6

I am getting response string 2018-10-03T09:00:36.845+0000

and i have to parse it for PrettyTime some min ago .

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));

its not working, can you tell me Dateformat string to add in SimpleDateFormat ?

Here is My code

:

 try {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
      sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
      long time = sdf.parse(device_for_bottomviews.get(i).lastupdate).getTime();
      PrettyTime prettyTime = new PrettyTime(Locale.getDefault());
      String ago = prettyTime.format(new Date(time));
      lastupdate.setText(ago);
      } 
catch (Exception e)
{
      e.printStackTrace();
      lastupdate.setText(device_for_bottomviews.get(i).lastupdate);
}

exception i am getting

java.lang.IllegalArgumentException
        at java.util.Date.parse(Date.java:633)
        at java.util.Date.<init>(Date.java:272)
        at com.test.busmanagement.MapActivity$DeviceAdapter.getView(MapActivity.java:721)
        at android.widget.AbsListView.obtainView(AbsListView.java:2363)
        at android.widget.ListView.makeAndAddView(ListView.java:1970)
        at android.widget.ListView.fillDown(ListView.java:704)
        at android.widget.ListView.fillFromTop(ListView.java:765)
Soham Pandya
  • 386
  • 1
  • 14
  • 1
    What means *its not working*? Any Exception if yes please post the stackstrace. If not explain what *its not working* means – Jens Oct 05 '18 at 08:19
  • W/System.err: java.lang.IllegalArgumentException at java.util.Date.parse(Date.java:633) at java.util.Date.(Date.java:272) at com.test.busmanagement.MapActivity$DeviceAdapter.getView(MapActivity.java:721) at android.widget.AbsListView.obtainView(AbsListView.java:2363) at android.widget.ListView.makeAndAddView(ListView.java:1970) at android.widget.ListView.fillDown(ListView.java:704) at android.widget.ListView.fillFromTop(ListView.java:765) – Soham Pandya Oct 05 '18 at 08:20
  • java.lang.IllegalArgumentException – Soham Pandya Oct 05 '18 at 08:20
  • 4
    Don't post your code in the comments, edit your original post. – Zun Oct 05 '18 at 08:20
  • 1
    Add it to your question using the edit link, not as comment – Jens Oct 05 '18 at 08:20
  • 2
    You get the excpetion in `Date.parse` not in SimpleDateFormat?! Please add a [mcve] – Jens Oct 05 '18 at 08:21
  • 1
    The classes `java.util.Date` and `java.util.Calendar` are both outdated after the appearance of the `java.time` package in Java 8. Use those classes. Additionally, the constructor `Date(String)` and the factory method `Date.parse(String)` are both deprecated. And why are you setting up a `SimpleDateFormat` if you are not using it? – Seelenvirtuose Oct 05 '18 at 08:24
  • kindly refer post i have added my code with exceptions. – Soham Pandya Oct 05 '18 at 08:25
  • 1
    Seems to me like you want `"yyyy-MM-dd'T'HH:mm:ss.SSSZ"` (note the lack of single quotes around the `Z`). – Michael Oct 05 '18 at 08:25
  • thanks it did work. – Soham Pandya Oct 05 '18 at 08:36
  • [Wikipedia article: ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) – Ole V.V. Oct 05 '18 at 08:48
  • Similar: [org.threeten.bp.format.DateTimeParseException: Text '2018-07-22T14:00:00-03:00' could not be parsed at index 19](https://stackoverflow.com/questions/52545720/org-threeten-bp-format-datetimeparseexception-text-2018-07-22t140000-0300). I think it could be helpful. – Ole V.V. Oct 05 '18 at 08:53
  • 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. Oct 05 '18 at 08:54
  • Possible duplicate of [Java: Date parsing, why do I get an error](https://stackoverflow.com/questions/48666263/java-date-parsing-why-do-i-get-an-error) – Ole V.V. Oct 08 '18 at 19:14

2 Answers2

0

You can use this format as pattern

yyyy-MM-dd'T'HH:mm:ss.SSSX

Feedforward
  • 4,521
  • 4
  • 22
  • 34
0

You don't need to quote the 'zone' part of the date format string, here is a corrected version:

"yyyy-MM-dd'T'HH:mm:ss.SSSZ"
SpacedMonkey
  • 2,725
  • 1
  • 16
  • 17
Soham Pandya
  • 386
  • 1
  • 14