0

I have the following date value, that want to keep into MySQL database. Table field is DATETIME datatype that does not allow keep such value. I get the following error message when try to insert a record with:

Data truncation: Incorrect datetime value: '19999-12-21 01:00:00'

Tue Dec 21 01:00:00 EET 19999
19999-12-21 01:00:00
Dumitru Gutu
  • 579
  • 1
  • 7
  • 19

2 Answers2

4

There are reasons why one might want a year beyond 9999. For instance, if you are calculating astronomical events, you might want to know the next 1,000 times that Halley's comet will return to earth. Or, if you are fascinated by arcane religious calendars, you might be amused by the cycles of Jewish holidays through the seasons, or how often Ramadan starts on Christmas.

Unfortunately for this purpose, most databases where designed with business needs in mind, not astronomy or archeology or other domains. If you really need dates beyond the range, I would suggest setting up your own calendar system. One method is to create a bigint column and count the number of days since some canonical date, such as '0001-01-01'. Another is to store the dates as strings in a 'YYYYY-MM-DD' format.

Or, perhaps you just have a typo and 1999 is sufficient.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • Eloquent answer although you'd simply use seconds for such events. Years, days, etc. are not constant: yesterday's leap second epitomises that. – Bathsheba Jul 01 '15 at 12:32
  • @Bathsheba . . . Those interested in arcane calendar issues would probably be interested in John Skeet's top post: http://stackoverflow.com/questions/6841333/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result/6841479#6841479. And that's on a much shorter time frame. – Gordon Linoff Jul 01 '15 at 12:34
2

This is not a limit in Java but a limit in MySQL. The MySQL date types do not go that far into the future (https://dev.mysql.com/doc/refman/5.1/en/datetime.html)

Either switch databases or do it yourself by storing an appropriate counter you convert yourself.

If you switch databases, PostgreSQL 9 goes this far and then some (294276 AD). See http://www.postgresql.org/docs/9.1/static/datatype-datetime.html for details.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
  • 1
    Note that you may also just compare the date to a known date representing the largest date the database can hold, and if larger than that use the known date instead, – Thorbjørn Ravn Andersen Jul 01 '15 at 12:33