-1

In Java for my Android app, I am trying to display Date & Time in the XML which I get "startDateTime" from API response. Below are the details:

This is what I get from API Response:

"startDateTime": "2021-06-26T13:30:00Z"

In Java activity file, I am doing this:

import java.text.SimpleDateFormat;
import java.util.TimeZone;

TextView tvStartDateTime;

tvStartDateTime = itemView.findViewById(R.id.tvStartDateTime);

SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm");
format.setTimeZone(TimeZone.getTimeZone("GMT"));
String dateTime = format.format(match.getStartDateTime());
holder.tvStartDateTime.setText(dateTime);

This is what I do in XML:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tvStartDateTime"
    android:textColor="@color/black"
    android:maxLines="1"
    android:textSize="16sp"
    android:paddingLeft="@dimen/side_padding"
    android:paddingRight="@dimen/side_padding"
    android:paddingTop="@dimen/side_padding"
/>

In the app, when I run with this code, I see blank data.

Can anyone help ? Thanks in advance

SaAsh Techs
  • 169
  • 10
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends including `TimeZone`. See if you either can use [desugaring](https://developer.android.com/studio/write/java8-support-table) or add [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. Jun 28 '21 at 04:33
  • What does `getStartDateTime()` return? Which declared type? Which runtime type? `String`? – Ole V.V. Jun 28 '21 at 04:36
  • Are you getting any error or is any exception being thrown? Does this answer your question? [Java : Cannot format given Object as a Date](https://stackoverflow.com/questions/10649782/java-cannot-format-given-object-as-a-date) – Ole V.V. Jun 28 '21 at 04:40
  • [I downvoted because lacking an MCVE makes it hard to answer](http://idownvotedbecau.se/nomcve/). – Ole V.V. Jun 29 '21 at 04:44

1 Answers1

1

Try with the below code it will help you.

  LocalDateTime datetime  = LocalDateTime.ofInstant(Instant.parse(match.getStartDateTime()),TimeZone.getTimeZone("GMT").toZoneId() );
  DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
  holder.tvStartDateTime.setText(formatter.format(datetime));
Dharmender Manral
  • 1,504
  • 1
  • 6
  • 7