2025/3/17 09:23:10---调整字符串与时间转换

This commit is contained in:
2025-03-17 01:24:18 +00:00 committed by root
parent 12ce84903f
commit f5ef46abd3

View File

@ -7,36 +7,36 @@ import java.sql.SQLException;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import org.apache.ibatis.type.BaseTypeHandler; import org.apache.ibatis.type.TypeHandler;
import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.JdbcType;
public class StringToTimestampTypeHandler extends BaseTypeHandler<String> { public class StringToTimestampTypeHandler implements TypeHandler<String> {
private static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); private static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
@Override @Override
public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException { public void setParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
try { try {
ps.setLong(i, FORMAT.parse(parameter).getTime()); ps.setLong(i, FORMAT.parse(parameter).getTime());
} catch (ParseException e) { } catch (ParseException e) {
throw new SQLException("日期格式解析错误", e); throw new SQLException("日期格式解析错误", e);
} }
} }
@Override @Override
public String getNullableResult(ResultSet rs, String columnName) throws SQLException { public String getResult(ResultSet rs, String columnName) throws SQLException {
long timestamp = rs.getLong(columnName); long timestamp = rs.getLong(columnName);
return timestamp == 0 ? null : FORMAT.format(new java.util.Date(timestamp)); return timestamp == 0 ? null : FORMAT.format(new java.util.Date(timestamp));
} }
@Override @Override
public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException { public String getResult(ResultSet rs, int columnIndex) throws SQLException {
long timestamp = rs.getLong(columnIndex); long timestamp = rs.getLong(columnIndex);
return timestamp == 0 ? null : FORMAT.format(new java.util.Date(timestamp)); return timestamp == 0 ? null : FORMAT.format(new java.util.Date(timestamp));
} }
@Override @Override
public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { public String getResult(CallableStatement cs, int columnIndex) throws SQLException {
long timestamp = cs.getLong(columnIndex); long timestamp = cs.getLong(columnIndex);
return timestamp == 0 ? null : FORMAT.format(new java.util.Date(timestamp)); return timestamp == 0 ? null : FORMAT.format(new java.util.Date(timestamp));
} }
} }