English 中文(简体)
Best way to get maximum Date value in java?
原标题:

I m writing a bit of logic that requires treating null dates as meaning forever in the future (the date in question is an expiration date, which may or may not exist). Instead of putting in special cases for a null date throughout the code, I want to just convert null into the maximum possible Date. I don t see any obvious ways to get such a value without hard coding it. What s the best way to get the maximum value of whatever Date implementation is being used?

最佳回答

Try

new Date(Long.MAX_VALUE)

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

问题回答

Encapsulate the functionality you want in your own class, using Long.MAX_VALUE will most likely cause you 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);
        }
    }

    ...
}

+1 to the Long.MAX_VALUE suggestions. It seems that this would help you if you sort stuff by your date field.

However, instead of constructing a date from some the large constant value where ever you need the date, use a globally visible singleton to hold 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 simple identity comparison (mydate == DateUtils.NO_EXPIRE) to test if a particular date is of your special case instead of obj.equals(); (ie. mydate.equals ( DateUtils.NO_EXPIRE ); )

Here is what I do:

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();
}

Note that if the TimeZone is NOT UTC you will get offsets from the "end of time", which won t be maximal values. These are especially useful for inserting into Database fields and not having to have NULL dates.

have you considered adopting the use of Joda Time?

It s slated to be included in java 7 as the basis for JSR-310

The feature that may interest you is ZeroIsMaxDateTimeField which basically swaps zero fields for the maximum value for that field within the date-time.

From Java SE 8 you could use:

LocalDate.MAX

One problem I see is that for sorting on expiration date, using a null isn t easily sortable. So replacing with an actual value (even if it s an arbitrary sentry value well into the future) may be needed.

I suppose another way of treating "no expiration" is simply to say something expires 100 years in the future... Unless your database is dealing with long-term contracts!

I like Instant.MAX because it is more likely to be supported in the future than Long.MAX_VALUE.

Note that as of today, though, Instant.MAX.toEpochMilli() throws an overflow error.

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

System.out.println(new Date(Long.MAX_VALUE).toString())
//Output:
//Sun Aug 17 12:42:55 IST 292278994




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签