概要
Protocol Buffersで日付型として「google.type.datetime」を使用した際、
生成したオブジェクトにJava側で値を詰めるときに困ったのでやったことを備忘として記載
※もっといいやり方あったらコメントいただけると助かります
サンプル
- .protoの記載
syntax = "proto3";
package sample;
import "google/type/datetime.proto";
message response {
google.type.DateTime last_updated = 1;
}
- Java側の記載
下記で一度変換した上でオブジェクトに詰めるようにした。
import com.google.type.DateTime
import java.time.LocalDateTime
public DateTime toDateTime(LocalDateTime localDateTime) {
return DateTime.newBuilder()
.setYear(localDateTime.getYear())
.setMonth(localDateTime.getMonthValue())
.setDay(localDateTime.getDayOfMonth())
.setHours(localDateTime.getHour())
.setMinutes(localDateTime.getMinute)
.setSeconds(localDateTime.getSecond)
.build();
}