0

I am trying to create a date that is a few years away from the current time. I am doing this like so:

new Date(Math.abs(System.currentTimeMillis() - 94670778000L));

However for some reason the year portion of the date becomes some random unknown number, causing the jvm to throw a MysqlDataTruncation exception. This is the full message:

com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: '55106056-02-25 05:40:34.461' for column 'lastSeenOnline' at row 1

According to serhii's post here: Data truncation: Incorrect datetime value: '' i should change the @Temporal annotation from TemporalType.TIMESTAMP to TemporalType.TIME. This does help but only because the date portion is then omitted, so this isn't the solution i'm looking for.

Can anyone tell me what i'm doing wrong? I am only initializing a new date via the millisecond constructor and it should work but it doesn't for some reason.

Maurice
  • 6,698
  • 9
  • 47
  • 104
  • Well MySQL is right about this `55106056-02-25 05:40:34.461` format to be incorrect.. No tested but `System.currentTimeMillis() - 94670778000L` feels like a int overflow is happing that would explain the wierd `55106056` value for the year. – Raymond Nijland Feb 15 '19 at 14:44
  • what do you mean with int overflow? The date argument is of `long` type – Maurice Feb 15 '19 at 14:49
  • 1
    "what do you mean with int overflow? The date argument is of long type" Yes the java type is long there yes.. But MySQL expects a int for year right? Maybe the conversion between long to int happens wrong in the database layer. – Raymond Nijland Feb 15 '19 at 14:53
  • this `new Date(Math.abs(System.currentTimeMillis()))` is the same as this `new Date()`. Both represent a Date of the current time and date. Only the first one does throw the error and the second one doesn't. So i don't think its a conversion issue – Maurice Feb 15 '19 at 14:58
  • CORRECTION: `new Date()` creates the same error.. – Maurice Feb 15 '19 at 15:00
  • "So i don't think its a conversion issue ...CORRECTION: new Date() creates the same error." i hate it when iám right or on the correct path.. You should ideally provide SQL table structures `SHOW CREATE TABLE ` and Java code.
    – Raymond Nijland Feb 15 '19 at 15:02
  • I am using Hibernate to auto-generate my database – Maurice Feb 15 '19 at 15:05

1 Answers1

0

Still not sure what is causing it, but removing the trouble causing field from the embeddable class and placing it in the class that is using the embeddable class solved the problem. So to anyone facing the same problem, don't use @Temporal in an embeddable class.

Maurice
  • 6,698
  • 9
  • 47
  • 104