在用GRPC框架时,传输时间类型需要转换成google的Timestamp类型,以下是timestamp和localdatetime互转的工具类
Timestamp转LocalDateTime
public static Timestamp toPb(LocalDateTime dateTime) {
Instant instant = dateTime.atZone(ZoneId.systemDefault()).toInstant();
return Timestamp.newBuilder().setSeconds(instant.getEpochSecond()).setNanos(instant.getNano()).build();
}
LocalDateTime转Timestamp
public static LocalDateTime fromPb(Timestamp timestamp) {
return Instant.ofEpochSecond(timestamp.getSeconds(), timestamp.getNanos())
.atZone(TimeZone.getDefault().toZoneId()).toLocalDateTime();
}