简体   繁体   English

Java:最小和最大日期范围

[英]Java: Min and Max Date range

I'm grabbing some data from a database that has a stored date value, and I'm letting the user pick date ranges they would like to view data for. 我从具有存储日期值的数据库中获取一些数据,并且我让用户选择他们想要查看数据的日期范围。 All my code for getting these date ranges works except for the method to get the date range covering all time, which would be a start value of the earliest possible data Java handles, to the end value of the max possible date. 我获取这些日期范围的所有代码都有效,除了获取所有时间的日期范围的方法,这是最早可能的数据Java句柄的起始值,以及最大可能日期的结束值。

Is there something wrong with my code, because I can't see a problem: 我的代码有问题,因为我看不出问题:

public static DateRange getAllTime() {
        /**
         * Get earliest possible
         */
        Calendar c = Calendar.getInstance();
        c.set(
                c.getActualMinimum(Calendar.YEAR), 
                c.getActualMinimum(Calendar.MONTH), 
                c.getActualMinimum(Calendar.DAY_OF_MONTH), 
                c.getActualMinimum(Calendar.HOUR), 
                c.getActualMinimum(Calendar.MINUTE), 
                c.getActualMinimum(Calendar.SECOND)
            );

        c.set(Calendar.MILLISECOND, c.getActualMinimum(Calendar.MILLISECOND));
        Date start = c.getTime();

        /**
         * Get latest possible date
         */
        c.set(
                c.getActualMaximum(Calendar.YEAR), 
                c.getActualMaximum(Calendar.MONTH), 
                c.getActualMaximum(Calendar.DAY_OF_MONTH), 
                c.getActualMaximum(Calendar.HOUR), 
                c.getActualMaximum(Calendar.MINUTE), 
                c.getActualMaximum(Calendar.SECOND)
            );

        c.set(Calendar.MILLISECOND, c.getActualMaximum(Calendar.MILLISECOND));
        Date end = c.getTime();

        DateRange range = new DateRange();
        range.Start = start;
        range.End = end;

        return range;
    }

Why not use 为什么不用

  1. new Date(Long.MIN_VALUE) (in YEAR 292269055 BC) 新日期(Long.MIN_VALUE)(公元前292269055年)
  2. new Date(Long.MAX_VALUE) (in YEAR 292278994 AD)? 新日期(Long.MAX_VALUE)(公元292278994年)?

Since froginvasion challenged the answer, I thought I'd double check 由于青蛙入侵挑战了答案,我想我会仔细检查

    long day=1000*60*60*24;
    System.out.println(new Date(Long.MAX_VALUE-day));
    System.out.println(new Date(Long.MAX_VALUE));
    System.out.println(new Date(0));
    System.out.println(new Date(-day));
    System.out.println(new Date(Long.MIN_VALUE));
    System.out.println(new Date(Long.MIN_VALUE+day));

gave me 给我

Sat Aug 16 07:12:55 GMT 292278994
Sun Aug 17 07:12:55 GMT 292278994
Thu Jan 01 00:00:00 GMT 1970
Wed Dec 31 00:00:00 GMT 1969
Sun Dec 02 16:47:04 GMT 292269055
Mon Dec 03 16:47:04 GMT 292269055

I think it is right. 我认为这是对的。 I assume the AD/BC are just being suppressed. 我假设AD / BC正被压制。 The suggestion to use new Date(0) as the minimum is clearly wrong because new Date(-day) is clearly smaller. 使用新Date(0)作为最小值的建议显然是错误的,因为新的Date(-day)明显更小。

Why make life so complicated? 为什么让生活如此复杂? If you don't have a start date, don't query for a start date. 如果您没有开始日期,请不要查询开始日期。 If you don't have an end date, don't query for an end date. 如果您没有结束日期,请不要查询结束日期。 And if you have neither, don't query for dates at all. 如果您没有,请不要查询日期。

That code works me, maybe you're not expecting the values it returns? 那段代码对我有用,也许你不期望它返回的值?

Start: Sat Jan 01 00:00:00 PST 1 End: Wed Apr 17 21:34:08 PST 292269054 开始时间:周一1月01日00:00:00太平洋标准时间1结束:周四4月17日21:34:08太平洋标准时间292269054

(It would be easier to help if you included the stack trace) (如果包含堆栈跟踪,将更容易提供帮助)

I suspect may get an overflow by setting the year and then setting maximum values for all the other fields separately. 我怀疑可能通过设置年份然后分别为所有其他字段设置最大值来获得溢出。 That would make your end time somewhere around your start time and cause all records to be rejected. 这会使你的结束时间在你的开始时间附近并导致所有记录被拒绝。 You might try just printing out the calendar times to see what's happening. 您可以尝试打印日历时间以查看正在发生的事情。

As seanizer points out, you're really making this more complicated than it should be - the correct way to deal with this is to leave the date clause off entirely in the query. 正如seanizer指出的那样,你真的让它变得比它应该更复杂 - 处理这个问题的正确方法是在查询中完全保留date子句。 That may be difficult sometimes because the sql statement isn't generated dynamically. 这有时可能很困难,因为sql语句不是动态生成的。 But note that even if you can't modify the sql at run time, the condition (in Oracle) 但请注意,即使你无法在运行时修改sql,条件(在Oracle中)

start_date >= nvl(?, start_date)

will always be satisfied if the supplied value is null and start_date is populated. 如果提供的值为null并且填充了start_date,则将始终满足。

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

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