Java Data解决报错过程记录

[attendancewebservice] [24-08-15 19:01:03.199] [b3960aea15204b76b7c838189c28d45d] [10.129.1.238] DEBUG [Thread-10] [ne.jdbc.spi.SqlExceptionHelper.logExceptions 139] could not execute query [select * from id_customers where userid = ?]
java.sql.SQLException: HOUR_OF_DAY: 0 -> 1
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql-connector-java-8.0.27.jar:8.0.27]
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-8.0.27.jar:8.0.27]
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89) ~[mysql-connector-java-8.0.27.jar:8.0.27]
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63) ~[mysql-connector-java-8.0.27.jar:8.0.27]
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73) ~[mysql-connector-java-8.0.27.jar:8.0.27]
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:85) ~[mysql-connector-java-8.0.27.jar:8.0.27]
    at com.mysql.cj.jdbc.result.ResultSetImpl.getTimestamp(ResultSetImpl.java:939) ~[mysql-connector-java-8.0.27.jar:8.0.27]
    at com.mysql.cj.jdbc.result.ResultSetImpl.getTimestamp(ResultSetImpl.java:977) ~[mysql-connector-java-8.0.27.jar:8.0.27]
    at com.alibaba.druid.filter.FilterChainImpl.resultSet_getTimestamp(FilterChainImpl.java:1258) ~[druid-1.2.3.jar:1.2.3]
    at com.alibaba.druid.filter.FilterAdapter.resultSet_getTimestamp(FilterAdapter.java:1829) ~[druid-1.2.3.jar:1.2.3]
    at com.alibaba.druid.filter.FilterChainImpl.resultSet_getTimestamp(FilterChainImpl.java:1254) ~[druid-1.2.3.jar:1.2.3]
    at com.alibaba.druid.filter.FilterAdapter.resultSet_getTimestamp(FilterAdapter.java:1829) ~[druid-1.2.3.jar:1.2.3]
    at com.alibaba.druid.filter.FilterChainImpl.resultSet_getTimestamp(FilterChainImpl.java:1254) ~[druid-1.2.3.jar:1.2.3]
    at com.alibaba.druid.proxy.jdbc.ResultSetProxyImpl.getTimestamp(ResultSetProxyImpl.java:740) ~[druid-1.2.3.jar:1.1.10]
    at com.alibaba.druid.pool.DruidPooledResultSet.getTimestamp(DruidPooledResultSet.java:365) ~[druid-1.2.3.jar:1.2.3]
    at org.hibernate.type.descriptor.sql.TimestampTypeDescriptor$2.doExtract(TimestampTypeDescriptor.java:75) ~[hibernate-core-4.3.1.Final.jar:4.3.1.Final]
    at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:64)

解决过程:

1. 修改tomcat的数据库连接url配置,增加&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai 【验证无效】

2. 程序修改Data类型为LocalData 【未验证】

3. 时区验证解决过程 【确实是此问题】

代码验证如下:

1     public static void main(String[] args) throws ParseException {
2         DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
3         sdf.setLenient(false);
4         //sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));
5         sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
6         Date dt = sdf.parse("1991-04-14 00:00:00");
7         System.out.println(sdf.format(dt));
8     }

发现采用美国时区时,程序正常,采用中国时区时,抛异常,问题原因是“1991-04-14 00:00:00”在中国不存在

 上述曾实行夏令时的时间,中国时区均不存在,加1小时即可解决

posted @ 2024-08-15 20:17  快乐的小小码农  阅读(5)  评论(0编辑  收藏  举报