简体   繁体   English

在java中获得最大日期值的最佳方法?

[英]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?获得正在使用的任何 Date 实现的最大值的最佳方法是什么?

Try尝试

new Date(Long.MAX_VALUE)

which should give you the longest possible date value in Java.这应该为您提供 Java 中可能的最长日期值。

Encapsulate the functionality you want in your own class, using Long.MAX_VALUE will most likely cause you problems.在你自己的类中封装你想要的功能,使用 Long.MAX_VALUE 很可能会给你带来问题。

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. +1 对 Long.MAX_VALUE 建议。 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:但是,不要从需要日期的某个大常量值构建日期,而是使用全局可见的单例来保存代表您的特殊值的 Date 实例:

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();然后您可以使用简单的身份比较(mydate == DateUtils.NO_EXPIRE)来测试特定日期是否属于您的特殊情况,而不是 obj.equals(); (ie. mydate.equals ( DateUtils.NO_EXPIRE ); ) (即 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.请注意,如果时区不是 UTC,您将从“时间结束”获得偏移量,这不会是最大值。 These are especially useful for inserting into Database fields and not having to have NULL dates.这些对于插入到数据库字段中并且不必具有 NULL 日期特别有用。

have you considered adopting the use of Joda Time ?您是否考虑过使用Joda Time

It's slated to be included in java 7 as the basis for JSR-310它计划包含在 Java 7 中作为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.您可能感兴趣的功能是ZeroIsMaxDateTimeField ,它基本上将零字段交换为日期时间内该字段的最大值。

从 Java SE 8 开始,您可以使用:

LocalDate.MAX

One problem I see is that for sorting on expiration date, using a null isn't easily sortable.我看到的一个问题是,对于按到期日期排序,使用 null 不容易排序。 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!我想另一种处理“无到期”的方法是简单地说某些东西在未来 100 年到期……除非您的数据库正在处理长期合同!

I like Instant.MAX because it is more likely to be supported in the future than Long.MAX_VALUE.我喜欢 Instant.MAX,因为它在未来比 Long.MAX_VALUE 更有可能得到支持。

Note that as of today, though, Instant.MAX.toEpochMilli() throws an overflow error.请注意,截至今天, Instant.MAX.toEpochMilli() 会引发溢出错误。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM