'0000-00-00 000000' can not be represented as java.sql.Timestamp error
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
In Java applications, particularly those that connect to relational databases, one might encounter the error '0000-00-00 00:00:00' cannot be represented as java.sql.Timestamp. Let's dive into why this happens, how this error arises, and what can be done to address it.
Understanding java.sql.Timestamp
Java's java.sql.Timestamp is a subclass of java.util.Date, designed to handle both date and exact time to nanoseconds. It is commonly used to represent dates and times in database interactions.
A typical Timestamp object represents a point in time, expressed in milliseconds since January 1, 1970, 00:00:00 GMT (the Unix epoch). This design implies it expects valid date-time values, fully compatible with the Gregorian calendar.
The Cause of the Error
The date '0000-00-00 00:00:00' is often a placeholder representing a missing or uninitialized date in databases, particularly seen in MySQL. Unlike other "invalid" dates (e.g., February 30), which are rejected with an error, MySQL typically accepts '0000-00-00 00:00:00' unless strict mode settings dictate otherwise.
Here's why this becomes problematic with java.sql.Timestamp:
- The
java.sql.Timestampclass strictly adheres to dates being in the Gregorian calendar. - The concept of "year zero" does not exist in the standard Gregorian calendar, making it an invalid date representation in Java.
Situations where this Error Occurs
This error commonly emerges when:
- An attempt is made to read or write
'0000-00-00 00:00:00'from a database into ajava.sql.Timestampobject. - Date fields are left uninitialized or set to a default improper value in the database.
Example Scenario
Imagine a Java application using JDBC to connect to a MySQL database. The MySQL table 'orders' contains a datetime column created_at with some records storing '0000-00-00 00:00:00' as the value. When the Java application attempts to fetch these records:
- Update existing records to replace
'0000-00-00 00:00:00'withNULLor a valid default date-time. - Enable MySQL's strict mode to prevent such invalid defaults during insertions.
- When fetching data, perform a check on the string value before conversion.
- Utilize a wrapper or utility to safely handle invalid dates.
- Configure the JDBC driver to allow zero dates. This can be done by specifying
zeroDateTimeBehavior=convertToNullin the connection string, which treats zero dates asNULL.
Related reading
- 1030 Got error 28 from storage engine
- 1052 Column 'id' in field list is ambiguous
- 1071 - Specified key was too long; max key length is 1000 bytes
- 1114 HY000 The table is full
- 1.3.7.RELEASE - 1.4.1.RELEASE java.lang.NoSuchMethodError org.springframework.boot.builder.SpringApplicationBuilder.showBanner
- 8080 port already taken issue when trying to redeploy project from Spring Tool Suite IDE
- 0/1 nodes are available 1 nodes didn't have free ports for the requested pod ports when start the promethus exporter
- 100% cpu usage by all kafka brokers

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.