将分区时间戳转换为com.google.protobuf.Timestamp

huangapple go评论67阅读模式
英文:

Convert zoned timestamp to com.google.protobuf.Timestamp

问题

在我正在使用Swagger创建的自定义API中,我需要输入一个时间戳。

在我的YAML文件中,我已将输入参数的格式定义为date-time,如Swagger网页上所述日期时间 - 如RFC 3339第5.6节定义的日期时间表示法,例如2017-07-21T17:32:28Z

我想将生成的带时区的时间戳转换为com.google.protobuf.Timestamp,但我不知道如何操作,需要帮助。我正在使用Kotlin。

到目前为止,我尝试了一些从StackOverflow中提取的Java示例(转换为Kotlin后),例如:

System.out.println(SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX")
        .format(Date()))

val withoutTimezone = zoneDateTime.toLocalDateTime()
val timestamp = Timestamp.valueOf(withoutTimezone)

由于我需要将输入参数格式转换为com.google.protobuf.Timestamp,因此上述示例均不起作用。

有关如何转换时间戳的任何帮助将不胜感激。

英文:

In my custom API that I am creating using Swagger, I need to input a timestamp.

In my YAML file I have defined the format of my input parameter as date-time as mentioned on the Swagger webpage date-time – the date-time notation as defined by RFC 3339, section 5.6, for example, 2017-07-21T17:32:28Z

I want to convert the resulting zoned timestamp to com.google.protobuf.Timestamp but I don´t know how to do that and need help. I am using Kotlin.

So far I´ve tried implementing some of the Java examples from StackOverflow (after converting to Kotlin), for instance:

System.out.println(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX")
        .format(new Date()));

and

LocalDateTime withoutTimezone = zoneDateTime.toLocalDateTime();
Timestamp timestamp = Timestamp.valueOf(withoutTimezone));

None of the examples seem to work since I need input parameter in com.google.protobuf.Timestamp format, which the above examples do not result in.

Any help would be appreciated regarding how to convert timestamp.

答案1

得分: 1

我还没有进行过测试但从阅读`com.google.protobuf.Timestamp`文档来看这似乎是一个选项

String exampleInput = "2020-08-27T20:13:10+02:00";
Instant javaTimeInstant = OffsetDateTime.parse(exampleInput).toInstant();
com.google.protobuf.Timestamp ts = com.google.protobuf.Timestamp.newBuilder()
       .setSeconds(javaTimeInstant.getEpochSecond())
       .setNanos(javaTimeInstant.getNano())
       .build();

我正在使用Java你可能可以自己将其手动翻译成Kotlin

**文档链接:** [`com.google.protobuf.Timestamp`](https://developers.google.com/protocol-buffers/docs/reference/java/com/google/protobuf/Timestamp)
英文:

I haven’t tested, but from reading the com.google.protobuf.Timestamp documentation this seems to be an option:

    String exampleInput = "2020-08-27T20:13:10+02:00";
    Instant javaTimeInstant = OffsetDateTime.parse(exampleInput).toInstant();
    com.google.protobuf.Timestamp ts = com.google.protobuf.Timestamp.newBuilder()
           .setSeconds(javaTimeInstant.getEpochSecond())
           .setNanos(javaTimeInstant.getNano())
           .build();

I am using Java. You can probably hand translate to Kotlin yourself?

Documentation link: com.google.protobuf.Timestamp

huangapple
  • 本文由 发表于 2020年8月27日 22:08:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63617857.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定