Java 中的日期格式转换不起作用
Date format conversion in Java not working
在我的 Android 应用程序的 Java 中,我试图在 XML 中显示日期和时间,我从 API 响应中获得“startDateTime”。详情如下:
这是我从 API 得到的回复:
"startDateTime": "2021-06-26T13:30:00Z"
在 Java activity 文件中,我这样做:
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);
这就是我在 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"
/>
在应用程序中,当我 运行 使用此代码时,我看到空白数据。
有人可以帮忙吗?提前致谢
试试下面的代码,它会对你有所帮助。
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));
在我的 Android 应用程序的 Java 中,我试图在 XML 中显示日期和时间,我从 API 响应中获得“startDateTime”。详情如下:
这是我从 API 得到的回复:
"startDateTime": "2021-06-26T13:30:00Z"
在 Java activity 文件中,我这样做:
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);
这就是我在 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"
/>
在应用程序中,当我 运行 使用此代码时,我看到空白数据。
有人可以帮忙吗?提前致谢
试试下面的代码,它会对你有所帮助。
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));