Best way to get maximum date value in java?

I am writing a little logic that requires treating zero dates as values ​​forever in the future (the date in question is an expiration date that may or may not exist). Instead of introducing special cases for the zero date in the whole code, I want to just convert null to the highest possible date. I do not see any obvious ways to get such value without hard coding. What is the best way to get the maximum value of any date implementation?

+55
java datetime
Nov 16 '09 at 18:31
source share
8 answers

Try

new Date(Long.MAX_VALUE) 

which should give you the longest possible date value in Java.

+103
Nov 16 '09 at 18:33
source share

Encapsulate the desired functions in your class using Long.MAX_VALUE is likely to cause problems.

 class ExpirationDate { Date expires; boolean hasExpiration() { return expires == null; } Date getExpirationDate() { return expires; } boolean hasExpired(Date date) { if (expires == null) { return true; } else { return date.before(expires); } } ... } 
+19
Nov 16 '09 at 18:49
source share

+1 to Long.MAX_VALUE offers. It looks like this will help you if you sort the material by date field.

However, instead of building a date from some big constant value when you need a date, use a globally visible singleton to store a Date instance that represents your special value:

 class DateUtil { public static final Date NO_EXPIRE = new Date( Long.MAX_VALUE ); } 

Then you can use a simple identity comparison (mydate == DateUtils.NO_EXPIRE) to check if your specific date has a special case instead of obj.equals (); (i.e. mydate.equals (DateUtils.NO_EXPIRE);)

+6
Nov 16 '09 at 19:21
source share

That's what I'm doing:

 public static final TimeZone UTC; // 0001.01.01 12:00:00 AM +0000 public static final Date BEGINNING_OF_TIME; // new Date(Long.MAX_VALUE) in UTC time zone public static final Date END_OF_TIME; static { UTC = TimeZone.getTimeZone("UTC"); final Calendar c = new GregorianCalendar(UTC); c.set(1, 0, 1, 0, 0, 0); c.set(Calendar.MILLISECOND, 0); BEGINNING_OF_TIME = c.getTime(); c.setTime(new Date(Long.MAX_VALUE)); END_OF_TIME = c.getTime(); } 

Please note that if TimeZone is NOT UTC, you will get an offset from the "end of time" that will not be maximum. They are especially useful for inserting into database fields and the absence of null dates.

+4
Nov 16 '09 at 20:30
source share

Did you think you were using Joda Time ?

It will be included in java 7 as the basis for JSR-310

A function that might interest you is ZeroIsMaxDateTimeField, which basically swaps null fields for the maximum value for this field over the course of a date.

+2
Nov 16 '09 at 20:26
source share

One of the problems I see is that to sort by expiration date, using null is not just sorted. Therefore, it may be necessary to replace the actual value (even if it is an arbitrary time zone value in the future).

I suppose another “no expiration” treatment is to simply say that 100 years will expire in the future ... If your database is not connected with long-term contracts!

+1
Sep 22 '10 at 18:14
source share

With Java SE 8, you can use:

 LocalDate.MAX 
0
Jul 03 '19 at 12:18
source share

Perhaps one option is to use the maximum system date. You can get it using:

 System.out.println(new Date(Long.MAX_VALUE).toString()) //Output: //Sun Aug 17 12:42:55 IST 292278994 
-one
May 29 '15 at 8:02
source share



All Articles