小编htw*_*tw 的帖子

使用 android-databinding 方法格式化友好的日期

我想将我的日期 YYYY/MM/DD 格式化为更友好的模式。

我使用android-databinding。

我期望的输出应该是示例: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;
   };

}
Run Code Online (Sandbox Code Playgroud)

我的布局:

<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" />
Run Code Online (Sandbox Code Playgroud)

我确实有错误:

Found data binding …
Run Code Online (Sandbox Code Playgroud)

android android-studio android-databinding

1
推荐指数
1
解决办法
4452
查看次数