当使用代码类似于
ResultSet.getTimestamp("start_time").toLocalDateTime();
在start_time为0秒时,返回的时间会丢失精度
正常情况下:2021-09-03 10:10:08,时间可以正常转换,转换结果是2021-09-03 10:10:08
极端情况下:2021-09-03 00:00:00,时间转换会丢失秒,纳秒,毫秒,实际结果是2021-09-03 00:00
上代码
public LocalDateTime toLocalDateTime() {
return LocalDateTime.of(getYear() + 1900,//步入of()
getMonth() + 1,
getDate(),
getHours(),
getMinutes(),
getSeconds(),
getNanos());
}
public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond) {
LocalDate date = LocalDate.of(year, month, dayOfMonth);
LocalTime time = LocalTime.of(hour, minute, second, nanoOfSecond);//步入of()
return new LocalDateTime(date, time);
}
public static LocalTime of(int hour, int minute, int second, int nanoOfSecond) {
HOUR_OF_DAY.checkValidValue(hour);
MINUTE_OF_HOUR.checkValidValue(minute);
SECOND_OF_MINUTE.checkValidValue(second);
NANO_OF_SECOND.checkValidValue(nanoOfSecond);
return create(hour, minute, second, nanoOfSecond);//步入create()
}
private static LocalTime create(int hour, int minute, int second, int nanoOfSecond) {
/*
* 发现在这里直接返回了小时
*/
if ((minute | second | nanoOfSecond) == 0) {
return HOURS[hour];
}
return new LocalTime(hour, minute, second, nanoOfSecond);
}
遇到这种情况需要主动处理
原因是,toString这一行做了处理,导致没办法显示零秒后面的精度,实际的对象中仍然存在