Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert LocalDateTime to com.google.protobuf.Timestamp?

I have an instance of LocalDateTime, which I get from the repository layer, and I need to convert it to a Timestamp (Protocol Buffer) instance.

I have used to following approach for the conversion:

LocalDateTime localDateTime = LocalDateTime.now();//this can be any date

Instant instant = localDateTime.toInstant(ZoneOffset.UTC);

Timestamp timestamp = Timestamp.newBuilder()
                            .setSeconds(instant.getEpochSecond())
                            .setNanos(instant.getNano())
                            .build();

Is the ZoneOffset used here, to convert localDateTime to an instance of Instant, correct?

I have used the UTC offset because the comment on the "seconds" attribute in the Timestamp class says the following:

Represents seconds of UTC time since Unix epoch 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z inclusive

Have I used the correct ZoneOffset and is my conversion approach correct?

like image 688
uneq95 Avatar asked Jun 07 '18 05:06

uneq95


People also ask

How to convert LocalDateTime into Timestamp in Java?

In Java, we can use Timestamp. valueOf(LocalDateTime) to convert a LocalDateTime into a Timestamp .

How to convert LocalDateTime to Timestamp in Java 8?

Use Timestamp. valueOf(LocalDateTime time) method to transform the LocalDateTime into Timestamp.

How to convert localdatetime to timestamp in Java 8?

Java 8 – Convert LocalDateTime to Timestamp By mkyong| Last updated: June 14, 2019 Viewed: 88,912 (+865 pv/w) Tags:date conversion| java 8| java.time In Java, we can use Timestamp.valueOf(LocalDateTime)to convert a LocalDateTimeinto a Timestamp.

How to get the timestamp of a date without timezone?

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.

What is a timestamp?

A Timestamp represents a point in time independent of any time zone or calendar, represented as seconds and fractions of seconds at nanosecond resolution in UTC Epoch time. It is encoded using the Proleptic Gregorian Calendar which extends the Gregorian calendar backwards to year one.

How do I convert a date to a timestamp in SQL?

Convert LocalDateTime to Timestamp You can use Timestamp’s valueOf () method to convert LocalDateTime to Timestamp. Converting LocalDateTime to Timestamp helps us to convert time into Timestamp that is compatible with SQL timestamp datatype.


2 Answers

In general, no, your approach is not correct. The reason is that a LocalDateTime does not have an associated timezone, so it is ambiguous by nature. To convert it to an actual timestamp (an absolute point in time, independent of timezones), you need to know what timezone it was measured in.

By calling localDateTime.toInstant(ZoneOffset.UTC), you are assuming that your localDateTime was actually measured in the UTC timezone. Instead, you should be using the timezone that the LocalDateTime is stored in. If you don't know, then your input data is inherently ambiguous and you'll need to fix that first.

Note that this has nothing to do with the fact that the Unix epoch is usually specified in UTC. We might as well say that the Unix epoch is 1970-01-01T08:00:00+08:00, and it would be the same instant in time.

The rest of it seems correct to me.

like image 112
Thomas Avatar answered Oct 10 '22 22:10

Thomas


Here's a routine that pulls together the comments from the question and actual answer to this question:

 protected Timestamp convertLocalDateTimeToGoogleTimestamp(LocalDateTime localDateTime) {
    Instant instant = localDateTime.toInstant(ZoneOffset.UTC);

    Timestamp result = Timestamp.newBuilder()
            .setSeconds(instant.getEpochSecond())
            .setNanos(instant.getNano())
            .build();

    return result;
  }
like image 2
Brad Parks Avatar answered Oct 10 '22 21:10

Brad Parks