1
    @Override
public void onBindViewHolder(AirListAdapter.AirListViewHolder holder, int position) {
    final Flight flight = flightList.get(position);

    DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    try {
        Date date = format.parse(flight.getRoutePoints().get(0).getDepartureTime());
        holder.departureTime.setText(date.toString());

    } catch (ParseException e) {
        e.printStackTrace();
    }

    holder.airportName.setText(flightList.get(position).getRoutePoints().get(0).getLocation().getAirport_name());
    holder.airportNameDestination.setText(flightList.get(position).getRoutePoints().get(0).getDestination().getAirportName());

}

PS : Unparseable date: "2018-07-22T14:00:00-03:00" Is equals getDepartureTime.

Help me please.

  • 1
    `-03:00` is not equal to literal `'Z'`. Don't quote the `Z`. --- Actually, replace `'Z'` with `X`. – Andreas Sep 25 '18 at 20:52
  • 1
    More close to a duplicate of: [*Java SimpleDateFormat for time zone with a colon separator?*](https://stackoverflow.com/q/2375222/642706). Look to the Answers using `OffsetDateTime` as in: `OffsetDateTime.parse( "2018-07-22T14:00:00-03:00" )` – Basil Bourque Sep 26 '18 at 00:37
  • 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. Sep 26 '18 at 07:29
  • An explanation: for the zero (=UTC) time zone **Z** is displayed otherwise +/- hh:mm. So your date format would only accept UTC dates. – Joop Eggen Sep 26 '18 at 07:37

1 Answers1

1

Replace your SimpleDateFormat with this:

DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");

The time zone information in your string is an ISO 8601 time zone, which is represented by X. Additonally, you don't want to quote the X since you're not looking for an actual letter X.

Ben P.
  • 52,661
  • 6
  • 95
  • 123
  • Thank u. The output I got was Sun Jul 22 17:00:00 GMT + 00: 00 2018. What do I do to have an exit dd / MM / yyyy, 14:00 – Alan Bastos Sep 25 '18 at 21:18
  • 1
    FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). Instead, `OffsetDateTime` works here. – Basil Bourque Sep 26 '18 at 00:37
  • The question is tagged `android`, and (very) unfortunately Android doesn't get java features on the normal schedule. However, there are plenty of significantly-better-than-java.util.Date third-party libraries available to be used. – Ben P. Sep 26 '18 at 00:59
  • I believe the best third-party library would be [the ThreeTenABP library](https://github.com/JakeWharton/ThreeTenABP), the backport of java.time, the modern Java date and time API, to earlier Android. Almost not a third-party library since the core was developed by the same people developing java.time. – Ole V.V. Sep 26 '18 at 07:45