2021-10-10T10:10:10 含有T的字符串转LocalDateTime
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
LocalDateTime dateTimeParked = LocalDateTime.parse(rs.getObject(columnIndex).toString(), formatter);
来源:
@Component
@MappedTypes(LocalDateTime.class)
@MappedJdbcTypes(value = JdbcType.DATE, includeNullJdbcType = true)
public class LocalDateTimeTypeHandler extends BaseTypeHandler<LocalDateTime> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, LocalDateTime parameter, JdbcType jdbcType)
throws SQLException {
ps.setObject(i, parameter);
}
@Override
public LocalDateTime getNullableResult(ResultSet rs, String columnName) throws SQLException {
return DateUtil.toLocalDateTime(rs.getObject(columnName));
}
@Override
public LocalDateTime getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
// return Convert.toLocalDateTime(rs.getObject(columnIndex));
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
LocalDateTime dateTimeParked = null;
try {
dateTimeParked = LocalDateTime.parse(rs.getObject(columnIndex).toString(), formatter);
}catch (Exception ex){
dateTimeParked = ((Timestamp)rs.getObject(columnIndex)).toLocalDateTime();
}
return dateTimeParked;
}
@Override
public LocalDateTime getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
//return Convert.toLocalDateTime(cs.getObject(columnIndex));
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
LocalDateTime dateTimeParked = null;
try{
dateTimeParked =LocalDateTime.parse(cs.getObject(columnIndex).toString(), formatter);
}catch (Exception ex){
dateTimeParked = ((Timestamp)cs.getObject(columnIndex)).toLocalDateTime();
}
return dateTimeParked;
}
}
try catch 可以用以下工具类替换:
public static LocalDateTime toLocalDateTime(Object obj){
LocalDateTime localDateTime = null;
if(obj instanceof java.sql.Timestamp){
localDateTime = ((Timestamp)obj).toLocalDateTime();
}
if(obj instanceof java.time.LocalDateTime){
localDateTime = (LocalDateTime)obj;
}
return localDateTime;
}
含有T的字符串转LocalDateTime