使用数据绑定时从getTimeInMillis获取日期 [英] Getting Date from getTimeInMillis when using Databinding

查看:68
本文介绍了使用数据绑定时从getTimeInMillis获取日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个timeInMillis值,我知道我可以用类似的东西从其中获取Date

I have a timeInMillis value, which I know I can get a Date from with something like;

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); 
String dateString = formatter.format(new Date(dateInMillis)));

我正在使用DataBinding填充RecyclerView.我还知道,在将DataBinding与类似的内容一起使用时,我可以操纵字符串;

I'm using DataBinding to populate a RecyclerView. I am also aware that I can manipulate strings when using DataBinding with something like this;

android:text='@{String.format("%.1f", example.double)}'

但是,我无法确定如何从我的timeInMillis值中用格式化的Date填充TextView.

However, I cannot work out how to populate the TextView with a formatted Datefrom my timeInMillis value.

推荐答案

您可以在模型中添加一个函数,该函数可以将timeInMillis转换为格式化的Date.

You can add in your model a function which it can transform your timeInMillis to a formatted Date.

在您的模型中使用的数据绑定布局:

public String getDateFormatted(){
    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); 
    return formatter.format(new Date(dateInMillis)));;
}

在您的布局中:

<layout>
   <data>
      <variable name="yourModel"
                type="com.example.model.yourModel"/>
   </data>
...
<TextView 
...
   android:text="@{yourModel.dateFomatted}"
/>

这篇关于使用数据绑定时从getTimeInMillis获取日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆