0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

JavaのLocalDateTimeをgoogle.type.datetimeに変換する

Last updated at Posted at 2023-02-26

概要

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();
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?