Android 数据绑定格式化程序 org.threeten.bp.OffsetDateTime
Android Databinding Formatter with org.threeten.bp.OffsetDateTime
我有一个来自 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.OffsetDateTime
Android 文档详细介绍了格式化程序 here。
我可以让格式化程序处理字符串。但无论我在 strings.xml
中输入什么 date/time 格式,我都会得到上述异常。
格式化是否根本不适用于 OffsetDateTime
?
正如@pskink 在上面评论的那样,我使用了 BindingAdapter
:
@BindingAdapter("formatTime")
public static void formatTime(TextView textView, OffsetDateTime dateTime) {
textView.setText(dateTime.format(DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM)));
}
然后在我看来:
<TextView
...
formatTime="@{dto.timestamp}"
/>
我有一个来自 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.OffsetDateTime
Android 文档详细介绍了格式化程序 here。
我可以让格式化程序处理字符串。但无论我在 strings.xml
中输入什么 date/time 格式,我都会得到上述异常。
格式化是否根本不适用于 OffsetDateTime
?
正如@pskink 在上面评论的那样,我使用了 BindingAdapter
:
@BindingAdapter("formatTime")
public static void formatTime(TextView textView, OffsetDateTime dateTime) {
textView.setText(dateTime.format(DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM)));
}
然后在我看来:
<TextView
...
formatTime="@{dto.timestamp}"
/>