0

I'm using Spring Boot, and HSQLDB file, When i use:

calendar.setTimeInMillis(-9223372036854775808L);
calendar.setTime(new Date(Long.MIN_VALUE));

and and store the model,

after i call rest client, i get invalid date, when i check it, it returns some positive value instead of negative one :| what should i use as min_Value.

i thought it may be SQL issue, i changed temporary variable type from timespan to Date, but didn't worked again, i no longer sure what is the case of this issue, and what nuber i should use, every one talk about min value, some one talked about around -8.......L too that work in JS, but it didn't worked here too :|

assylias
  • 321,522
  • 82
  • 660
  • 783
Hassan Faghihi
  • 1,888
  • 1
  • 37
  • 55
  • 2
    in SQL-92, year has to be between -9999 and 9999. Just pick a random date in the past as min value (1st Jan of year 1? 1st Jan -9999 ?). Note that depending on the exact column type and database, the valid range may be more restricted. You may also consider using NULL if you want to represent a non existing value. – assylias Nov 28 '16 at 12:31
  • @assylias SQL-92 year has to be between 1 and 9999. In Java, dates before 1970-01-01 have negative millisecond values. – fredt Nov 28 '16 at 12:58
  • @assylias it didn't worked... – Hassan Faghihi Nov 28 '16 at 13:45
  • @deadManN try using 0L for 1970-01-01 as minimum value – fredt Nov 28 '16 at 17:05
  • @fredt true, that work, i done it before i ask, but that's not the minimum value – Hassan Faghihi Nov 29 '16 at 07:45

1 Answers1

1

Avoid ancient date-time values

Many reasons to not use that minimum number as a date value. As commented, standard SQL does not permit such ancient dates. No database implementation I know of supports that value. And using a date-time for historical values is fraught with problems and issues, and ill-advised.

Use epoch as a flag value

If you are looking for an arbitrary value to use as a flag such as "no value intended" while avoiding the use of nulls, then I suggest using the Java and Unix epoch of first moment of 1970 in UTC. If you know your system will never store any date-time that far back as a valid value, this will work well. And 1970-01-01T00:00:00Z is easily recognized by many programmers, DBAs, and SysAdmins as the common epoch and therefore likely to be a special value.

java.time

Avoid using the Date and Calendar classes. These troublesome classes and their siblings are now legacy, supplanted by the java.time classes.

These classes include a constant for that epoch value: Instant.EPOCH

Similar Question: Minimum date in Java

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154