Java:最小和最大日期范围 [英] Java: Min and Max Date range

查看:684
本文介绍了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.

这篇关于Java:最小和最大日期范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆