The Code A can convert a long value to date value, just like 2018.01.10

  1. 我希望得到日期+时间值,比如2018.01.10 23:11,我该如何处理Kotlin

  2. 我希望将当前时间转换为长时间值,我如何处理Kotlin

谢谢!

Code A

fun Long.toDateString(dateFormat: Int =  DateFormat.MEDIUM): String {
    val df = DateFormat.getDateInstance(dateFormat, Locale.getDefault())
    return df.format(this)
}

推荐答案

试试这个,我用的是SimpleDataFormat.

fun convertLongToTime(time: Long): String {
    val date = Date(time)
    val format = SimpleDateFormat("yyyy.MM.dd HH:mm")
    return format.format(date)
}

fun currentTimeToLong(): Long {
    return System.currentTimeMillis()
}

fun convertDateToLong(date: String): Long {
    val df = SimpleDateFormat("yyyy.MM.dd HH:mm")
    return df.parse(date).time
}

And to convert java file to kotlin file with Android Studio, choosing Code->Convert java file to kotlin file.

Kotlin相关问答推荐

UByte范围. Min_UTE.. UByte.MAX_UTE不符合预期

如何在Jetpack Compose中从领域查询中读取数据?

Kotlin中函数引用的否定

可选的.在kotlin中不使用泛型参数

为什么Kotlin协程程序即使有延迟也能输出?

Ktor 在 Heroku 上的 CORS 问题,但在本地没有

有没有什么方法或算法可以在没有存储的情况下生成唯一的随机数?

Kotlin 中的 maxOf() 和 max() 方法有什么区别?

可组合项在返回后返回时组合导航句柄

如何在 kotlin 中创建自定义迭代器并添加到现有类?

Eclipse:无法安装 Kotlin 插件

使用 Discord4j 交叉发布 Discord 消息

找不到引用的类 kotlin.internal.annotations.AvoidUninitializedObjectCopyingCheck

如何使 TextInputEditText 只读?

Kotlin中具有多个参数的绑定适配器

Kotlin替换字符串中的多个单词

安装 Kotlin-Jupyter:e: java.lang.NoClassDefFoundError: 无法初始化类 org.jetbrains.kotlin.com.intellij.pom.java.LanguageLevel

使用 rxbinding 时我应该取消订阅吗?

从 Kotlin 访问 Integer.class

Kotlin 中的填充字符串