首页javadate_timeJava Data Type - 如何从ResultSet中读取ZonedDateTime

Java Data Type - 如何从ResultSet中读取ZonedDateTime

我们想知道如何从ResultSet中读取ZonedDateTime。
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

public class Main {

  public static void main(String[] args) {

  }

  public static ZonedDateTime fromTimestamp(ResultSet rs, String column)
      throws SQLException {
    Timestamp timestamp = rs.getTimestamp(column);
    return getDateTime(timestamp);
  }

  public static ZonedDateTime getDateTime(Timestamp timestamp) {
    return timestamp != null ? ZonedDateTime.ofInstant(
        Instant.ofEpochMilli(timestamp.getTime()), ZoneOffset.UTC) : null;
  }

}