tags:

views:

164

answers:

5

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;
    }
+3  A: 

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.

seanizer
Haha yeah, that's what I ended up doing. This was a special case, and since every other case had some start/end I just used one method. Ended up writing a method to cover this case and wrote a query that didn't check the date.That said, it bugs me to not know and I'd really like to know why this wasn't working.
Scienceprodigy
A: 

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

(It would be easier to help if you included the stack trace)

yfrangi
A: 

Why not use

  1. new Date(Long.MIN_VALUE) (in YEAR 292269055 BC)
  2. new Date(Long.MAX_VALUE) (in YEAR 292278994 AD)?
emory
A: 

Calendar c = Calendar.getInstance();

I am new to Java. Can you please explain why you are declaring 'c' an object of type Calendar and then calling getInstance() method as if it was a static or class method rather than an instance method? Also how come you are not using 'new' keyword to create a new object? I'm so confused.

`getInstance()` is indeed a static method in class `Calendar`. It returns a `Calendar` object. Look it up in the API documentation. Note that StackOverflow is not a discussion forum; you're not answering the question asked by Scienceprodigy.
Jesper
A: 

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)

start_date >= nvl(?, start_date)

will always be satisfied if the supplied value is null and start_date is populated.