Best way to get maximum Date value in java?

前端 未结 9 1286
眼角桃花
眼角桃花 2020-12-01 13:55

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). Inst

9条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-01 14:07

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

提交回复
热议问题