【问题标题】:how to create a Instant class variable in kotlin with my own timestamp如何使用我自己的时间戳在 kotlin 中创建 Instant 类变量
【发布时间】:2021-12-28 17:25:27
【问题描述】:

直到现在我还在使用

val date = Instant.now(Clock.system(ZoneId.of("UTC")))

生成即时时间戳。 现在我需要用我想要指定的日期替换它,例如“2021-05-03T00:00:00.000Z”。当我将它作为字符串插入函数时,这个想法给了我错误“类型不匹配。必需:即时!找到:字符串”。我无法更改该功能,因为我无法访问它。所以我需要以某种方式把这个日期变成“即时!”类。

这就是我无法更改的功能的样子

    public TimeTZ(Instant timestamp, Boolean isLocal) {
        this.timestamp = timestamp;
        this.isLocal = isLocal;
    }

【问题讨论】:

    标签: date kotlin java-time java.time.instant


    【解决方案1】:
    val date = Instant.parse("2021-05-03T00:00:00.000Z")
    

    将字符串转换为Instant(或其他类型的值)称为解析。所以使用Instantparse方法。

    【讨论】:

    • 谢谢,用那个方法查不到。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-09
    • 2012-09-15
    • 2013-06-08
    • 2021-04-13
    • 2021-04-21
    • 2020-02-29
    • 2011-02-23
    相关资源
    最近更新 更多