在Kotlin中为API级别创建小于或等于16的Date对象 [英] Creating Date objects in Kotlin for API level less than or equal to 16

查看:102
本文介绍了在Kotlin中为API级别创建小于或等于16的Date对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这听起来可能很有趣而且很基本,但是我正在努力通过将纪元传递给秒数来在Kotlin中创建DateTime类型的对象。

It may sound very funny and basic, but I am struggling with creating a DateTime kind of object in Kotlin by passing the number of seconds from epoch to it.

我得到的示例都是需要API级别26的库,或者只是谈论从DD-MM-YYYY字符串到其他字符串的格式转换。

Either the examples that I am getting are of the libraries that require API level 26, or just talk about format conversion from string of DD-MM-YYYY to some other.

到目前为止,我已经尝试过 LocalDateTime, Timestamp, Date,但是Kotlin要么拒绝识别它们,要么不同意函数参数,或者无法解决名称空间冲突。或示例是Java,在Kotlin中不起作用或在Kotlin中没有Java。

I have tried "LocalDateTime", "Timestamp", "Date" so far, but Kotlin either refuses to recognise them or the doesn't agree to the function parameters, or is unable to resolve namespace conflict. Or the examples are of Java, which don't work in Kotlin or don't have equivalent in Kotlin.

使用Date(),我收到错误消息无法访问' ':它在Date()中是私有的

With Date() I am getting error "Cannot access '': It is private in Date()"

使用LocalDate,LocalDateTime,我遇到了API级别错误

With LocalDate, LocalDateTime, I am getting the API level error

使用时间戳记,我收到错误消息使用提供的参数无法调用以下函数。我将Long作为文档中提到的参数传递。

With Timestamp I am getting error "None of the following functions can be called with the arguments supplied". I am passing Long as the parameter as mentioned in documentation. But it is expecting "Parcel" or "Date".

有人可以帮忙吗?

推荐答案

我将日历用于Kotlin

I use Calendar for Kotlin

val date = Calendar.getInstance()
date.timeInMillis = 5000000
txtDate.text = date.time.toString()

这篇关于在Kotlin中为API级别创建小于或等于16的Date对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆