Java Data Type How to - Insert and save LocalDateTime to ResultSet








Question

We would like to know how to insert and save LocalDateTime to ResultSet.

Answer

import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
//  www  .  j  a v  a  2  s. c om
public class Main {

  public static void main(String[] args) {

  }

  public static LocalDateTime getLocalDateTimeFromDB(ResultSet rs, String column)
      throws SQLException {
    String date = rs.getString(column);
    if (date != null) {
      return LocalDateTime.parse(date, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
    } else {
      return null;
    }
  }

  public static String getDBValueFromLocalDateTime(LocalDateTime start) {
    if (start != null) {
      return start.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
    } else {
      return null;
    }
  }
}