java 日期范围比较好,Java:最小和最大日期范围

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.

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值