【问题标题】:Incompatible types: com.google.api.client.util.DateTime cannot be converted to org.joda.time.DateTime不兼容的类型:com.google.api.client.util.DateTime 无法转换为 org.joda.time.DateTime
【发布时间】:2016-09-21 12:13:45
【问题描述】:

我想将 Google DateTime 转换为 org.joda.time.DateTime

这是谷歌的实现代码。

http://grepcode.com/file/repo1.maven.org/maven2/com.google.http-client/google-http-client/1.15.0-rc/com/google/api/client/util/DateTime.java

我该如何实现?

【问题讨论】:

  • 通过java.util.Date ?
  • this answer
  • @Stewart 你能给我看一下代码示例吗?
  • 好的,所以你甚至不需要java.util.Date,你可以从纪元开始使用millis

标签: java datetime


【解决方案1】:

返回日期/时间值,表示为自 Unix 纪元以来的毫秒数。 如果指定了时区,则该值将标准化为 UTC,因此要格式化此日期/时间值,必须应用时区偏移。


公共日期时间(长瞬间)

使用默认时区中的 ISOChronology 构造一个实例,设置为从 1970-01-01T00:00:00Z 开始的毫秒数。


// getValue() return millis since epoch
long millis = googleDate.getValue();

// Which is accepted by Joda DateTime's constructor
DateTime jodaDate = new DateTime(millis);

【讨论】:

  • 我测试了它,但我得到了空值。可能问题出在其他地方。
  • getValue() 返回一个原语long。不能是null
【解决方案2】:

可能为时已晚,但我刚刚解决了一个非常相似的问题,如下所示

//Let's suppose date is our google DateTime
org.joda.time.DateTime jodaDate = date.getDateTime(date.getValue);

这会根据 google DateTimes 的 long 值创建一个新的 joda DateTime

【讨论】:

    猜你喜欢
    • 2016-02-23
    • 1970-01-01
    • 2017-10-03
    • 1970-01-01
    • 2021-12-20
    • 2016-10-17
    • 1970-01-01
    • 2017-03-29
    相关资源
    最近更新 更多