Kvasir - 2 years ago 232
Java Question

Get max and min date of java.sql.date

I see everywhere that for get the minimum and the maximum of a date i have to use :

``````Date dateModificationMin = new Date( Long.MIN_VALUE );
Date dateModificationMax = new Date( Long.MAX_VALUE );
``````

But i do it on my java.sql.date it's return :
055-12-02 for the min date
994-08-17 for the max date

How can i do for get the maximum and minimum of a java.sql.date?

If you do the code to the java.util.Date, you will gain

``````dateModificationMin = Sun Dec 02 17:47:04 CET 292269055
dateModificationMax = Sun Aug 17 08:12:55 CET 292278994
``````

i guess, the date with negative long timestamp works wrong, so the minimum date is

``````new Date(0)==  Thu Jan 01 01:00:00 CET 1970
``````

Since java.sql.date extends the java.util.Date, the problem there is probably same

with

``````        long timestamp = -10;

for (int i = 0; i < 20; i++) {

System.out.println("timestamp = " + timestamp + " date = " + (new Date(timestamp)));
timestamp*=10;
}
``````

you get :

``````timestamp = -10 date = Thu Jan 01 00:59:59 CET 1970
timestamp = -100 date = Thu Jan 01 00:59:59 CET 1970
timestamp = -1000 date = Thu Jan 01 00:59:59 CET 1970
timestamp = -10000 date = Thu Jan 01 00:59:50 CET 1970
timestamp = -100000 date = Thu Jan 01 00:58:20 CET 1970
timestamp = -1000000 date = Thu Jan 01 00:43:20 CET 1970
timestamp = -10000000 date = Wed Dec 31 22:13:20 CET 1969
timestamp = -100000000 date = Tue Dec 30 21:13:20 CET 1969
timestamp = -1000000000 date = Sat Dec 20 11:13:20 CET 1969
timestamp = -10000000000 date = Sun Sep 07 07:13:20 CET 1969
timestamp = -100000000000 date = Mon Oct 31 15:13:20 CET 1966
timestamp = -1000000000000 date = Sun Apr 24 23:13:20 CET 1938
timestamp = -10000000000000 date = Mon Feb 10 07:13:20 CET 1653
timestamp = -100000000000000 date = Thu Feb 26 15:13:20 CET 1200
timestamp = -1000000000000000 date = Tue Nov 16 23:13:20 CET 29720
timestamp = -10000000000000000 date = Sun Feb 01 07:13:20 CET 314912
timestamp = -100000000000000000 date = Mon Mar 08 15:13:20 CET 3166840
timestamp = -1000000000000000000 date = Sun Feb 24 23:13:20 CET 31686119
``````

see how dates go crasy after -1 * 10^15 also with java.sql.Date :

``````timestamp = -10000000000000 date = 1653-02-10
timestamp = -100000000000000 date = 1200-02-26
timestamp = -1000000000000000 date = 720-11-16
timestamp = -10000000000000000 date = 912-02-01
timestamp = -100000000000000000 date = 840-03-08
timestamp = -1000000000000000000 date = 119-02-24
timestamp = 8446744073709551616 date = 634-03-09
timestamp = -7766279631452241920 date = 582-01-06
``````

so wrong here !! :D

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download