javaspringjdbctemplatemysql-connectorjava.util.date

Spring jdbctemplate exception java.time.LocalDateTime cannot be cast to java.util.Date


I have a datetime type column in mysql db and file fetching column using jdbctemplate and type casting it to java.util.Date throws error. Here is my code :

I have defined the following SimpleDateFormat

private static final SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

and use it like this

String query = "select start_time as starttime from job_tbl";

List<Map<String, Object>> myList = jdbcTemplate.queryForList(query);

for (Map<String, Object> map: myList) {
    String dateTime = dateformat.format((java.util.Date) map.get("starttime");
}

The code throws this error:

java.lang.ClassCastException:
java.time.LocalDateTime cannot be cast to java.util.Date

The same functionality was working fine in my old other projects with jdbcTemplate easily type casted to java.util.Date but in this new project, it returns date of format java.time.LocalDateTime, is there any issue with mysql-connector or any other issue??


Solution

  • java.time

    The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.

    From the error, it is clear that you need to cast map.get("starttime") to java.time.LocalDateTime instead of java.util.Date.

    DateTimeFormatter dft = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss", Locale.ENGLISH);
    String dateTime = dft.format((java.time.LocalDateTime) map.get("starttime"));   
    

    Learn more about the modern Date-Time API* from Trail: Date Time.


    * For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.