使用 android-数据绑定方法格式化友好的日期
Format friendly Date using android-databinding approach
我想将我的日期 YYYY/MM/DD 格式化为更友好的模式。
我使用 android-数据绑定。
我期望的输出应该是示例:2006 年 8 月 22 日,星期二。
我来自 Json 的当前输入是“2018-09-27”(模型中的字符串数据)
我的代码:
public class DateUtils {
SimpleDateFormat fromServer = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat myFormat = new SimpleDateFormat("dddd, dd MMMM yyyy");
public String getDateToFromat (String reciveDate) {
String newFormatString = myFormat.format(fromServer.parse(reciveDate));
return newFormatString;
};
}
我的布局:
<layout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data class ="CurrencyBindingDetailItem">
<import type="com.example.htw.currencyconverter.utils.DateUtils"/>
<import type="android.view.View" />
<variable name="currencyItemDetailDate" type="com.example.htw.currencyconverter.model.CurrencyDate"/>
<variable name="currencyBindingItemDetail" type="com.example.htw.currencyconverter.model.CurrencyBinding"/>
<variable name="callback" type="com.example.htw.currencyconverter.callback.ClickCallback"/>
</data>
<TextView
android:textSize="28dp"
android:text="@{DateUtils.getDateToFromat(currencyItemDetailDate.date)}"
android:textColor="@color/primary_text"
android:id="@+id/date_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
我确实有错误:
Found data binding errors.
****/ data binding error ****msg:**cannot find method getDateToFromat**(java.lang.String) in class com.example.htw.currencyconverter.utils.DateUtils
我清理并重新启动并重建。
您需要两个 DateFormat
对象。一个用于格式化您从服务器收到的字符串,另一个用于格式化您想要的格式。
SimpleDateFormat fromServer = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat myFormat = new SimpleDateFormat("dddd, dd MMMM yyyy");
String inputDateStr="2018-09-27";
Date date = fromServer.parse(inputDateStr);
String outputDateStr =myFormat.format(date);
为什么不创建一个数据绑定适配器,让您的 xml 保持更清晰?由于您来自服务器的日期是字符串格式,因此适配器将如下所示:
@BindingAdapter("bindServerDate")
public static void bindServerDate(@NonNull TextView textView, String date) {
/*Parse string data and set it in another format for your textView*/
}
它的用法:
在您的 viewModel 中创建 ObservableField<String> serverDate
并根据您的响应设置值,在 xml 中设置 app:bindServerDate="@{viewModel.serverDate}"
。不要忘记将 viewModel
添加为 variable
并从您的 activity/fragment
进行设置
@BindingAdapter("formatDate")
fun TextView.setDate(order_date: String) {
var outputDate: String? = null
try {
val curFormater = SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss")
val postFormater = SimpleDateFormat("MMM dd, yyyy")
val dateObj = curFormater.parse(order_date)
outputDate = postFormater.format(dateObj)
this.setText(outputDate)
} catch (e: ParseException) {
e.printStackTrace()
}
}
我想将我的日期 YYYY/MM/DD 格式化为更友好的模式。
我使用 android-数据绑定。
我期望的输出应该是示例:2006 年 8 月 22 日,星期二。 我来自 Json 的当前输入是“2018-09-27”(模型中的字符串数据)
我的代码:
public class DateUtils {
SimpleDateFormat fromServer = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat myFormat = new SimpleDateFormat("dddd, dd MMMM yyyy");
public String getDateToFromat (String reciveDate) {
String newFormatString = myFormat.format(fromServer.parse(reciveDate));
return newFormatString;
};
}
我的布局:
<layout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data class ="CurrencyBindingDetailItem">
<import type="com.example.htw.currencyconverter.utils.DateUtils"/>
<import type="android.view.View" />
<variable name="currencyItemDetailDate" type="com.example.htw.currencyconverter.model.CurrencyDate"/>
<variable name="currencyBindingItemDetail" type="com.example.htw.currencyconverter.model.CurrencyBinding"/>
<variable name="callback" type="com.example.htw.currencyconverter.callback.ClickCallback"/>
</data>
<TextView
android:textSize="28dp"
android:text="@{DateUtils.getDateToFromat(currencyItemDetailDate.date)}"
android:textColor="@color/primary_text"
android:id="@+id/date_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
我确实有错误:
Found data binding errors.
****/ data binding error ****msg:**cannot find method getDateToFromat**(java.lang.String) in class com.example.htw.currencyconverter.utils.DateUtils
我清理并重新启动并重建。
您需要两个 DateFormat
对象。一个用于格式化您从服务器收到的字符串,另一个用于格式化您想要的格式。
SimpleDateFormat fromServer = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat myFormat = new SimpleDateFormat("dddd, dd MMMM yyyy");
String inputDateStr="2018-09-27";
Date date = fromServer.parse(inputDateStr);
String outputDateStr =myFormat.format(date);
为什么不创建一个数据绑定适配器,让您的 xml 保持更清晰?由于您来自服务器的日期是字符串格式,因此适配器将如下所示:
@BindingAdapter("bindServerDate")
public static void bindServerDate(@NonNull TextView textView, String date) {
/*Parse string data and set it in another format for your textView*/
}
它的用法:
在您的 viewModel 中创建 ObservableField<String> serverDate
并根据您的响应设置值,在 xml 中设置 app:bindServerDate="@{viewModel.serverDate}"
。不要忘记将 viewModel
添加为 variable
并从您的 activity/fragment
@BindingAdapter("formatDate")
fun TextView.setDate(order_date: String) {
var outputDate: String? = null
try {
val curFormater = SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss")
val postFormater = SimpleDateFormat("MMM dd, yyyy")
val dateObj = curFormater.parse(order_date)
outputDate = postFormater.format(dateObj)
this.setText(outputDate)
} catch (e: ParseException) {
e.printStackTrace()
}
}