我有一个来自org.threeten:threetenbp包的OffsetDateTime,我想在我的Android视图中格式化它。
我有一个DTO:
public class SomeDto {
private org.threeten.bp.OffsetDateTime timestamp;
// getters and setters...
}以及一个具有绑定和TextView的视图:
<data>
<variable
name="dto"
type="com.example.SomeDto" />
</data>
...
<TextView
...
android:text="@{@string/formatTime(dto.timestamp)}"
/>和一个strings.xml
<string name="formatTime">%1$tH:%1$tM</string>但我似乎不能让它工作。我得到了:
java.util.IllegalFormatConversionException: H != org.threeten.bp.OffsetDateTimeAndroid文档详细介绍了格式化程序here。
我可以让格式化程序与字符串一起工作。但是无论我在strings.xml中放入哪种日期/时间格式,我都会得到上面的异常。
格式化根本不适用于OffsetDateTime吗
发布于 2018-07-25 02:39:01
正如@pskink在上面评论的那样,我使用了BindingAdapter
@BindingAdapter("formatTime")
public static void formatTime(TextView textView, OffsetDateTime dateTime) {
textView.setText(dateTime.format(DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM)));
}然后在我看来:
<TextView
...
formatTime="@{dto.timestamp}"
/>https://stackoverflow.com/questions/51430463
复制相似问题