Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Min and Max Date range

Tags:

java

date

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;
    }
like image 720
Christopher Perry Avatar asked Aug 30 '10 05:08

Christopher Perry


People also ask

How to find Min and max date in Java?

Finding Min or Max Date. To get max or min date from a stream of dates, you can use Comparator. comparing( LocalDate::toEpochDay ) Comparator. The toEpochDay() function returns the count of days since epoch i.e. 1970-01-01.

How to get the max date in Java?

LocalDateTime MAX defines the maximum supported LocalDateTime, '+999999999-12-31T23:59:59.999999999'. This is the local date-time just before midnight at the end of the maximum date.

How do I get the latest date in Java?

Get Current Date & Time: java.time.Clock The Clock.systemUTC().instant() method returns the current date and time both.

What is minimum date value?

The value of this constant is equivalent to 00:00:00.0000000 UTC, January 1, 0001, in the Gregorian calendar. MinValue defines the date and time that is assigned to an uninitialized DateTime variable. The following example illustrates this.


2 Answers

Why not use

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

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.

like image 82
emory Avatar answered Nov 06 '22 15:11

emory


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.

like image 20
Sean Patrick Floyd Avatar answered Nov 06 '22 14:11

Sean Patrick Floyd