Bug #112220 Create TimeStamp instance from DateTime in db for specific datetime
Submitted: 30 Aug 2023 10:27 Modified: 15 Jun 2024 13:05
Reporter: huanjie liu Email Updates:
Status: No Feedback Impact on me:
None 
Category:Connector / J Severity:S3 (Non-critical)
Version:8.0.33 OS:Any
Assigned to: Assigned Account CPU Architecture:Any
Tags: datetime, timestamp

[30 Aug 2023 10:27] huanjie liu
Description:
table with a datetime column, and set value between 1900-01-01 08:00:00
and 1900-01-01 08:05:00;

map this column into a java.sql.TimeStamp java type, bollow exception throws:

Caused by: java.lang.IllegalArgumentException: MINUTE: 5 -> 10
    at java.util.GregorianCalendar.computeTime(GregorianCalendar.java:2825)
    at java.util.Calendar.updateTime(Calendar.java:3395)
    at java.util.Calendar.getTimeInMillis(Calendar.java:1782)
    at com.mysql.cj.result.SqlTimestampValueFactory.localCreateFromDatetime(SqlTimestampValueFactory.java:191)

How to repeat:
// create table sql:
CREATE TABLE `person` (
  `id` bigint NOT NULL AUTO_INCREMENT COMMENT 'id',
  `name` varchar(50) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT 'name',
  `age` int DEFAULT NULL COMMENT 'age',
  `birth` datetime DEFAULT NULL COMMENT 'birth',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='for TEST';

// insert data
insert into person (id, name, age, birth) values (1, 'tom', 18, '1900-01-01 08:05:00');

// select in java with connector/j
public class Mysql8BugTestTest {

    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (Exception e) {
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (Exception t) {
                throw new RuntimeException(t);
            }
        }
    }

    public static final String MYSQL_URL_FORMAT = "jdbc:mysql://%s/%s?useSSL=false&characterEncoding=UTF-8";

    @Test
    public void mysql8SocketTimeoutTest() throws SQLException {

        try(Connection connection = getConnection()) {
            try(Statement statement = connection.createStatement()) {
                ResultSet resultSet = statement.executeQuery("select * from person where id =1;");
                while(resultSet.next()) {
                    Timestamp birth = resultSet.getTimestamp("birth");
                }
                // Ignore for Processing of ResultSet
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Connection getConnection() throws SQLException {
        return DriverManager.getConnection(String.format(Constants.MYSQL_URL_FORMAT, "ip:port", "dbname"), "uid", "pwd");
    }

}

Suggested fix:
we replay the same select with connector/j 5.1.48 and it worked well, so figured out difference between connector/j 5.1.48 and 8.0.33;

in 8.0.33 src/main/core-impl/java/com/mysql/cj/result/SqlTimestampValueFactory.java

c = Calendar.getInstance(this.defaultTimeZone, Locale.US);
c.setLenient(false);

if remove 
c.setLenient(false); 
it will be fine.
[15 May 2024 13:05] MySQL Verification Team
Hello Huanjie Liu,

Thank you for the bug report.
Please upgrade to latest version and report us back if issue persist even in latest version along with test case. Thank you.

Regards,
Ashwini Patil
[16 Jun 2024 1:00] Bugs System
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".