Kotlin:Java Util Date to String for Databindings
Kotlin: Java Util Date to String for Databindings
我想通过数据绑定在视图中使用我的数据 class 的日期值。
如果我在 Date 字段上使用 toString() 方法,它就可以工作。但我想自定义日期值。
所以我用 Method 创建了 Utils 对象。这是 Util 对象
object DateUtils {
fun toSimpleString(date: Date) : String {
val format = SimpleDateFormat("dd/MM/yyy")
return format.format(date)
}
}
但是如果我想在xml中像这样使用这个方法
<data>
<import type="de.mjkd.journeylogger.Utils.DateUtils"/>
<variable
name="journey"
type="de.mjkd.journeylogger.data.Journey"/>
</data>
...
android:text="@{DateUtils.toSimpleString(journey.date)}"
我收到一个错误 cannot find method toSimpleString(java.util.Date) in class ...
这是我的数据class:
data class Journey(var title: String, var date: Date?, var destination: String)
这段代码有什么问题?
在 kotlin 中使用保留字 object,您真正要做的是声明一个实例。 java 中的等价物或多或少类似于:
class DataUtils {
static DataUtils INSTANCE;
public String toSimpleString()...
}
然后当你调用它时你会做一个 DateUtils.INSTANCE.toSimpleString()
您应该能够在 xml
中使用 DateUtils.INSTANCE.toSimpleString()
为了使 toSimpleString
可从静态上下文访问,您必须使用 @JvmStatic
标记该方法
object DateUtils {
@JvmStatic
fun toSimpleString(date: Date) : String {
val format = SimpleDateFormat("dd/MM/yyy")
return format.format(date)
}
}
使用扩展函数(doc)
@file:JvmName("DateUtils")//Use this to change your class name in java, by default is <the file name>Kt (DateUtilsKt in your case)
fun Date.toSimpleString() : String {
val format = SimpleDateFormat("dd/MM/yyy")
return format.format(this)
}
然后你可以直接在 xml 中使用它,就像你已经在做的那样:
android:text="@{DateUtils.toSimpleString(journey.date)}"
为什么不使用默认为静态的顶级函数?任何 class.
中都没有定义顶级函数
fun main(args: Array<String>){
println(toSimpleString(Date()))
}
fun toSimpleString(date: Date?) = with(date ?: Date()) {
SimpleDateFormat("dd/MM/yyy").format(this)
}
另外,请注意在您的示例中 Jouney 的日期是如何可以为空的,而您的 toSimpleString
只接受不可为空的日期!
我更改了它,这样它将 return 当前日期的字符串,以防传递 null。
更简单的方法 是在模型 class 中制作一个 getDateString
。
android:text="@{journey.dateString)}"
class Journey {
lateinit var date: Date
fun getDateString(){
return DataUtils.toSimpleString(date)
}
}
我喜欢这种方式,因为在这种情况下我不需要导入任何 class。
我想通过数据绑定在视图中使用我的数据 class 的日期值。 如果我在 Date 字段上使用 toString() 方法,它就可以工作。但我想自定义日期值。 所以我用 Method 创建了 Utils 对象。这是 Util 对象
object DateUtils {
fun toSimpleString(date: Date) : String {
val format = SimpleDateFormat("dd/MM/yyy")
return format.format(date)
}
}
但是如果我想在xml中像这样使用这个方法
<data>
<import type="de.mjkd.journeylogger.Utils.DateUtils"/>
<variable
name="journey"
type="de.mjkd.journeylogger.data.Journey"/>
</data>
...
android:text="@{DateUtils.toSimpleString(journey.date)}"
我收到一个错误 cannot find method toSimpleString(java.util.Date) in class ...
这是我的数据class:
data class Journey(var title: String, var date: Date?, var destination: String)
这段代码有什么问题?
在 kotlin 中使用保留字 object,您真正要做的是声明一个实例。 java 中的等价物或多或少类似于:
class DataUtils {
static DataUtils INSTANCE;
public String toSimpleString()...
}
然后当你调用它时你会做一个 DateUtils.INSTANCE.toSimpleString()
您应该能够在 xml
中使用DateUtils.INSTANCE.toSimpleString()
为了使 toSimpleString
可从静态上下文访问,您必须使用 @JvmStatic
object DateUtils {
@JvmStatic
fun toSimpleString(date: Date) : String {
val format = SimpleDateFormat("dd/MM/yyy")
return format.format(date)
}
}
使用扩展函数(doc)
@file:JvmName("DateUtils")//Use this to change your class name in java, by default is <the file name>Kt (DateUtilsKt in your case)
fun Date.toSimpleString() : String {
val format = SimpleDateFormat("dd/MM/yyy")
return format.format(this)
}
然后你可以直接在 xml 中使用它,就像你已经在做的那样:
android:text="@{DateUtils.toSimpleString(journey.date)}"
为什么不使用默认为静态的顶级函数?任何 class.
中都没有定义顶级函数fun main(args: Array<String>){
println(toSimpleString(Date()))
}
fun toSimpleString(date: Date?) = with(date ?: Date()) {
SimpleDateFormat("dd/MM/yyy").format(this)
}
另外,请注意在您的示例中 Jouney 的日期是如何可以为空的,而您的 toSimpleString
只接受不可为空的日期!
我更改了它,这样它将 return 当前日期的字符串,以防传递 null。
更简单的方法 是在模型 class 中制作一个 getDateString
。
android:text="@{journey.dateString)}"
class Journey {
lateinit var date: Date
fun getDateString(){
return DataUtils.toSimpleString(date)
}
}
我喜欢这种方式,因为在这种情况下我不需要导入任何 class。