比较Kotlin类型和Java类

时间:2018-10-24 11:40:13

标签: class types kotlin retrofit2

除了JodaDateTime,我正在尝试像此问题一样创建请求转换器 How to pass custom enum in @Query via Retrofit?

但是,我很难将传入的类型与Joda Date Time类进行比较。

class JodaDateConverterFactory : Converter.Factory() {

    override fun stringConverter(
        type: Type?, annotations: Array<out Annotation>?,
        retrofit: Retrofit?
    ): Converter<*, String>? {
        if (type is Class<*> && type==DateTime::class) {
          return Converter<Any?, String> { value -> (value as DateTime).toString("yyyy-MM-dd") }
        }
        return null
    }
}

type == DateTime :: class返回false。

我可以看到该类型包含一个表示Joda日期时间的字符串,但是我显然在比较它们。

比较kotlin类型和Java类的正确方法是什么?

1 个答案:

答案 0 :(得分:2)

要获取Java类型,请使用语法

DateTime::class.java