Compare only the time portion of two dates, ignoring the date part

前端 未结 8 814
谎友^
谎友^ 2021-01-07 16:34

What\'s a good method to, given two Date objects, compare the difference between their time portion only, completely ignoring Year, Month and Day?

It\'s quite the op

相关标签:
8条回答
  • 2021-01-07 16:54

    Take a look at the Calendar class. It has support for extracting hours, minutes, and seconds from given Date's.

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(yourDateObject);
    int hour = calendar.get(Calendar.HOUR);
    int minute = calendar.get(Calendar.MINUTE);
    int second = calendar.get(Calendar.SECOND);
    
    0 讨论(0)
  • 2021-01-07 16:59

    If you want to compare the underlying binary (long int) values of the dates, you can do this:

    public int compareTimes(Date d1, Date d2)
    {
        int     t1;
        int     t2;
    
        t1 = (int) (d1.getTime() % (24*60*60*1000L));
        t2 = (int) (d2.getTime() % (24*60*60*1000L));
        return (t1 - t2);
    }
    

    Addendum 1

    This technique has the advantage of speed, because it uses the underlying long value of the Date objects directly, instead of converting between ticks and calendar components (which is rather expensive and slow). It's also a lot simpler than messing with Calendar objects.

    Addendum 2

    The code above returns the time difference as an int, which will be correct for any pair of times, since it ignores the year/month/day portions of the dates entirely, and the difference between any two times is no more than 86,400,000 ms (= 1000 ms/sec × 60 sec/min × 60 min/hr × 24 hr/day).

    0 讨论(0)
  • 2021-01-07 17:01

    Dont know how good practiced or efficent is this one but i've written a simple function using the Date object that serves my purposes. It returns a true or false is the first date value bigger than the second. The inputs d1 && d2 are Date objects.

    function CompareTimes(d1, d2) {
                 if (d1.getHours() < d2.getHours()) {
                     return false;
                 }
                 if (d1.getHours() > d2.getHours()) {
                     return true;
    
                 } else {
                     return (d1.getMinutes() > d2.getMinutes());
                 }
    };
    
    0 讨论(0)
  • 2021-01-07 17:02

    You may consider using Joda time's DateTimeComparator.getTimeOnlyInstance() for a comparator that will compare two Joda dates based only upon the times.

    For example:

    DateTimeComparator comparator = DateTimeComparator.getTimeOnlyInstance();
    comparator.compare(date1, date2);
    

    See http://joda-time.sourceforge.net/api-release/org/joda/time/DateTimeComparator.html#getTimeOnlyInstance()

    0 讨论(0)
  • 2021-01-07 17:11

    In case you can't use JODA,

    create 2 calendar objects, set year, month and day to 0.

    Compare them....

    0 讨论(0)
  • 2021-01-07 17:12

    if you just want to compare the time part of the dates then this would give you the right result -

    public long compareTimes(Date d1, Date d2)
        {
            long     t1;
            long     t2;
            t1 = d1.getHours()*60*60+d1.getMinutes()*60+d1.getSeconds();
            t2 = d2.getHours()*60*60+d2.getMinutes()*60+d2.getSeconds();
            long diff = t1-t2;
            System.out.println("t1 - "+t1+", t2 - "+t2+",  diff - "+diff);
    
            return diff;
        }
    
    0 讨论(0)
提交回复
热议问题