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.
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