Java Data Type How to - Read ZonedDateTime from ResultSet








Question

We would like to know how to read ZonedDateTime from ResultSet.

Answer

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
/*w ww  . j  a v  a 2s  . c om*/
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;
  }

}