Java:最小和最大日期范围
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3598091/
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
Java: Min and Max Date range
提问by Christopher Perry
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;
}
采纳答案by Sean Patrick Floyd
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.
为什么要把生活弄得这么复杂?如果您没有开始日期,请不要查询开始日期。如果您没有结束日期,请不要查询结束日期。如果两者都没有,则根本不要查询日期。
回答by yfrangi
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 月 1 日星期六 00:00:00 太平洋标准时间 1 结束:太平洋标准时间 292269054 星期三 4 月 17 日 21:34:08
(It would be easier to help if you included the stack trace)
(如果包含堆栈跟踪会更容易提供帮助)
回答by emory
Why not use
为什么不使用
- new Date(Long.MIN_VALUE) (in YEAR 292269055 BC)
- new Date(Long.MAX_VALUE) (in YEAR 292278994 AD)?
- 新日期(Long.MIN_VALUE)(公元前 292269055 年)
- 新日期(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. The suggestion to use new Date(0) as the minimum is clearly wrong because new Date(-day) is clearly smaller.
我认为是对的。我认为 AD/BC 只是被压制了。使用 new Date(0) 作为最小值的建议显然是错误的,因为 new Date(-day) 显然更小。
回答by user434722
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. That may be difficult sometimes because the sql statement isn't generated dynamically. But note that even if you can't modify the sql at run time, the condition (in Oracle)
正如 seanizer 指出的那样,您确实使这变得比应有的更复杂 - 处理此问题的正确方法是将日期子句完全保留在查询中。有时这可能很困难,因为 sql 语句不是动态生成的。但是要注意,即使你不能在运行时修改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,则将始终满足。