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
Post a Comment