Kotlin: Java Util Date to String for Databindings

2019-04-13 05:46发布

I want to use the Date value of my Data class in view via Databinding. If I use the toString() method on the Date field it works. But I want to customize the Date value. So I created the Utils object with Method. This is the Util object

object DateUtils {

     fun toSimpleString(date: Date) : String {
        val format = SimpleDateFormat("dd/MM/yyy")
        return format.format(date)
    }
}

But if I want to use this method in the xml like this

<data>
    <import type="de.mjkd.journeylogger.Utils.DateUtils"/>

    <variable
        name="journey"
        type="de.mjkd.journeylogger.data.Journey"/>
</data>
...
    android:text="@{DateUtils.toSimpleString(journey.date)}"

I get an error cannot find method toSimpleString(java.util.Date) in class ...

This is my Dataclass:

data class Journey(var title: String, var date: Date?, var destination: String)

Whats wrong with this code?

4条回答
在下西门庆
2楼-- · 2019-04-13 06:26

The function toSimpleString must be static, so:

object DateUtils {
    companion object {
         fun toSimpleString(date: Date) : String {
            val format = SimpleDateFormat("dd/MM/yyy")
            return format.format(date)
        }
    }
}
查看更多
相关推荐>>
3楼-- · 2019-04-13 06:31

Using the reserved word object in kotlin, that you really doing is declare a single instance. the equivalent in java is something more or less like:

class DataUtils {
    static DataUtils INSTANCE;
    public String toSimpleString()...
}

then when you call it you do a DateUtils.INSTANCE.toSimpleString()

You should capable to use DateUtils.INSTANCE.toSimpleString() in your xml


In order to make toSimpleString accessible from static context, you have to flag the method with@JvmStatic

object DateUtils {
    @JvmStatic
    fun toSimpleString(date: Date) : String {
        val format = SimpleDateFormat("dd/MM/yyy")
        return format.format(date)
    }
}

Using extension function(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)
}

Then you can use it directly in xml as you are already doing:

android:text="@{DateUtils.toSimpleString(journey.date)}"
查看更多
倾城 Initia
4楼-- · 2019-04-13 06:42

Why don't you just use a top-level function which is static by default? A top-level function is not defined in any class.

fun main(args: Array<String>){
    println(toSimpleString(Date())) 
}

fun toSimpleString(date: Date?) = with(date ?: Date()) {
    SimpleDateFormat("dd/MM/yyy").format(this)
}

Also, notice how Jouney's date is nullable in your example and your toSimpleString only accepts a non-nullable Date!

I changed it, so that it will return the string of the current date in case null is passed.

查看更多
仙女界的扛把子
5楼-- · 2019-04-13 06:46

More easy way would be to make a getDateString in model class.

android:text="@{journey.dateString)}"

class Journey {
    lateinit var date: Date
    fun getDateString(){
        return DataUtils.toSimpleString(date)
    }
}

I like this way because I don't need to import any class in this case.

查看更多
登录 后发表回答