How to convert Google proto timestamp to Java LocalDate?

Thirunavukkarasu picture Thirunavukkarasu · Oct 4, 2018 · Viewed 11.5k times · Source

We need to convert Google Proto buffer time stamp to a normal date. In that circumstance is there any way to convert Google Proto buffer timestamp to a Java LocalDate directly?

Answer

Basil Bourque picture Basil Bourque · Oct 4, 2018

tl;dr

As a moment in UTC, convert to java.time.Instant. Then apply a time zone to get a ZonedDateTime. Extract the date-only portion as a LocalDate.

One-liner:

Instant
.ofEpochSecond( ts.getSeconds() , ts.getNanos() ) 
.atZone( ZoneId.of( "America/Montreal" ) ) 
.toLocalDate() 

Convert

First step is to convert the Timestamp object’s count of seconds and fractional second (nanoseconds) to the java.time classes. Specifically, java.time.Instant. Just like Timestamp, an Instant represents a moment in UTC with a resolution of nanoseconds.

Instant instant =  Instant.ofEpochSecond( ts.getSeconds() , ts.getNanos() ) ;

Determining a date requires a time zone. For any given moment, the date varies around the globe by zone.

Apply a ZoneId to our Instant to get a ZonedDateTime. Same moment, same point on the timeline, different wall-clock time.

ZoneId z = ZoneId( "Pacific/Auckland" ) ; 
ZonedDateTime zdt = instant.atZone( z ) ;

Extract the date-only portion as a LocalDate. A LocalDate has no time-of-day and no time zone.

LocalDate ld = zdt.toLocalDate() ;

Caution: Do not use LocalDateTime class for this purpose, as unfortunately shown in another Answer. That class purposely lacks any concept of time zone or offset-from-UTC. As such it cannot represent a moment, is not a point on the timeline. See class documentation.

Convert

Best to entirely avoid the terribly troublesome legacy date-time classes including Date, Calendar, SimpleDateFormat. But if you must interoperate with old code not yet updated to java.time you can convert back-and-forth. Call new conversion methods added to the old classes.

GregorianCalendar gc = GregorianCalendar.from( zdt ) ;

To represent a date-only value as a GregorianCalendar we must specify a time-of-day and a time zone. You’ll likely want to use the first moment of the day as the time-of-day component. Never assume the first moment is 00:00:00. Anomalies such as Daylight Saving Time mean the first moment might be another time such as 01:00:00. Let java.time determine first moment.

ZonedDateTime firstMomentOfDay = ld.atZone( z ) ;
GregorianCalendar gc = GregorianCalendar.from( firstMomentOfDay ) ;

Table of date-time types in Java (both legacy and modern) and in standard SQL


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.