How to initialize Google protocol buffers Timestamp in Java? -


google protocol buffers (3.0.0-beta2) offers well-known type timestamp.

the documentation describes initialization in java using system.currenttimemillis() following:

long millis = system.currenttimemillis(); timestamp timestamp = timestamp.newbuilder().setseconds(millis / 1000)     .setnanos((int) ((millis % 1000) * 1000000)).build(); 

is there alternative way in recent java 8?

starting java 8, there new date/time-api makes more appealing reader using java.time.instant

instant time = instant.now(); timestamp timestamp = timestamp.newbuilder().setseconds(time.getepochsecond())     .setnanos(time.getnano()).build(); 

the result should same concerning precision.


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -