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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1743985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:09:47  来源:igfitidea点击:

Best way to get maximum Date value in java?

javadatetime

提问by jthg

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 实现的最大值的最佳方法是什么?

采纳答案by cjstehno

Try

尝试

new Date(Long.MAX_VALUE)

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

这应该为您提供 Java 中可能的最长日期值。

回答by Tom

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

    ...
}

回答by Trevor Harrison

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

+1 对 Long.MAX_VALUE 的建议。如果您按日期字段对内容进行排序,这似乎会对您有所帮助。

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(); (ie. mydate.equals ( DateUtils.NO_EXPIRE ); )

然后您可以使用简单的身份比较(mydate == DateUtils.NO_EXPIRE)来测试特定日期是否属于您的特殊情况,而不是 obj.equals(); (即 mydate.equals ( DateUtils.NO_EXPIRE ); )

回答by crowne

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 ZeroIsMaxDateTimeFieldwhich basically swaps zero fields for the maximum value for that field within the date-time.

您可能感兴趣的功能是ZeroIsMaxDateTimeField,它基本上将零字段交换为日期时间内该字段的最大值。

回答by crowne

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.

请注意,如果时区不是 UTC,您将从“时间结束”获得偏移量,这不会是最大值。这些对于插入到数据库字段中并且不必具有 NULL 日期特别有用。

回答by Toybuilder

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.

我看到的一个问题是,对于按到期日期排序,使用 null 不容易排序。因此,可能需要用实际值替换(即使它是未来很长一段时间内的任意哨兵值)。

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 年到期……除非您的数据库正在处理长期合同!

回答by Rakesh SR

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

回答by tmucha

From Java SE 8 you could use:

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

LocalDate.MAX

回答by ccleve

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() 会引发溢出错误。