Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Copyright 2001-2009 Stephen Colebourne
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  *  Unless required by applicable law or agreed to in writing, software
  *  distributed under the License is distributed on an "AS IS" BASIS,
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
 package org.joda.time;
 
 
DateTimeComparator provides comparators to compare one date with another.

Dates may be specified using any object recognised by the ConverterManager class.

The default objects recognised by the comparator are:

  • ReadableInstant
  • String
  • Calendar
  • Date
  • Long (milliseconds)
  • null (now)

DateTimeComparator is thread-safe and immutable.

Author(s):
Guy Allard
Stephen Colebourne
Brian S O'Neill
Since:
1.0
 
 public class DateTimeComparator implements Comparator<Object>, Serializable {

    
Serialization lock
 
     private static final long serialVersionUID = -6097339773320178364L;

    
Singleton instance
 
     private static final DateTimeComparator ALL_INSTANCE = new DateTimeComparator(nullnull);
    
Singleton instance
 
     private static final DateTimeComparator DATE_INSTANCE = new DateTimeComparator(DateTimeFieldType.dayOfYear(), null);
    
Singleton instance
 
     private static final DateTimeComparator TIME_INSTANCE = new DateTimeComparator(null, DateTimeFieldType.dayOfYear());

    
The lower limit of fields to compare, null if no limit
 
     private final DateTimeFieldType iLowerLimit;
    
The upper limit of fields to compare, null if no limit
 
     private final DateTimeFieldType iUpperLimit;
 
     //-----------------------------------------------------------------------
     
Returns a DateTimeComparator the compares the entire date time value.

Returns:
a comparator over all fields
 
     public static DateTimeComparator getInstance() {
         return ;
     }

    
Returns a DateTimeComparator with a lower limit only. Fields of a magnitude less than the lower limit are excluded from comparisons.

Parameters:
lowerLimit inclusive lower limit for fields to be compared, null means no limit
Returns:
a comparator over all fields above the lower limit
 
     public static DateTimeComparator getInstance(DateTimeFieldType lowerLimit) {
         return getInstance(lowerLimitnull);
     }

    
Returns a DateTimeComparator with a lower and upper limit. Fields of a magnitude less than the lower limit are excluded from comparisons. Fields of a magnitude greater than or equal to the upper limit are also excluded from comparisons. Either limit may be specified as null, which indicates an unbounded limit.

Parameters:
lowerLimit inclusive lower limit for fields to be compared, null means no limit
upperLimit exclusive upper limit for fields to be compared, null means no limit
Returns:
a comparator over all fields between the limits
 
     public static DateTimeComparator getInstance(DateTimeFieldType lowerLimitDateTimeFieldType upperLimit) {
         if (lowerLimit == null && upperLimit == null) {
             return ;
        }
        if (lowerLimit == DateTimeFieldType.dayOfYear() && upperLimit == null) {
            return ;
        }
        if (lowerLimit == null && upperLimit == DateTimeFieldType.dayOfYear()) {
            return ;
        }
        return new DateTimeComparator(lowerLimitupperLimit);
    }

    
Returns a comparator that only considers date fields. Time of day is ignored.

Returns:
a comparator over all date fields
    public static DateTimeComparator getDateOnlyInstance() {
        return ;
    }

    
Returns a comparator that only considers time fields. Date is ignored.

Returns:
a comparator over all time fields
    public static DateTimeComparator getTimeOnlyInstance() {
        return ;
    }

    
Restricted constructor.

Parameters:
lowerLimit the lower field limit, null means no limit
upperLimit the upper field limit, null means no limit
    protected DateTimeComparator(DateTimeFieldType lowerLimitDateTimeFieldType upperLimit) {
        super();
         = lowerLimit;
         = upperLimit;
    }
    //-----------------------------------------------------------------------
    
Gets the field type that represents the lower limit of comparison.

Returns:
the field type, null if no upper limit
    public DateTimeFieldType getLowerLimit() {
        return ;
    }

    
Gets the field type that represents the upper limit of comparison.

Returns:
the field type, null if no upper limit
    public DateTimeFieldType getUpperLimit() {
        return ;
    }

    
Compare two objects against only the range of date time fields as specified in the constructor.

Parameters:
lhsObj the first object, logically on the left of a < comparison, null means now
rhsObj the second object, logically on the right of a < comparison, null means now
Returns:
zero if order does not matter, negative value if lhsObj < rhsObj, positive value otherwise.
Throws:
java.lang.IllegalArgumentException if either argument is not supported
    public int compare(Object lhsObjObject rhsObj) {
        InstantConverter conv = ConverterManager.getInstance().getInstantConverter(lhsObj);
        Chronology lhsChrono = conv.getChronology(lhsObj, (Chronologynull);
        long lhsMillis = conv.getInstantMillis(lhsObjlhsChrono);
        
        conv = ConverterManager.getInstance().getInstantConverter(rhsObj);
        Chronology rhsChrono = conv.getChronology(rhsObj, (Chronologynull);
        long rhsMillis = conv.getInstantMillis(rhsObjrhsChrono);
        if ( != null) {
            lhsMillis = .getField(lhsChrono).roundFloor(lhsMillis);
            rhsMillis = .getField(rhsChrono).roundFloor(rhsMillis);
        }
        if ( != null) {
            lhsMillis = .getField(lhsChrono).remainder(lhsMillis);
            rhsMillis = .getField(rhsChrono).remainder(rhsMillis);
        }
        if (lhsMillis < rhsMillis) {
            return -1;
        } else if (lhsMillis > rhsMillis) {
            return 1;
        } else {
            return 0;
        }
    }
    //-----------------------------------------------------------------------
    
Support serialization singletons.

Returns:
the resolved singleton instance
    private Object readResolve() {
        return getInstance();
    }

    
Compares this comparator to another.

Parameters:
object the object to compare to
Returns:
true if equal
    public boolean equals(Object object) {
        if (object instanceof DateTimeComparator) {
            DateTimeComparator other = (DateTimeComparatorobject;
            return ( == other.getLowerLimit() ||
                    ( != null && .equals(other.getLowerLimit()))) &&
                   ( == other.getUpperLimit() ||
                    ( != null && .equals(other.getUpperLimit())));
        }
        return false;
    }

    
Gets a suitable hashcode.

Returns:
the hashcode
    public int hashCode() {
        return ( == null ? 0 : .hashCode()) +
               (123 * ( == null ? 0 : .hashCode()));
    }

    
Gets a debugging string.

Returns:
a debugging string
    public String toString() {
        if ( == ) {
            return "DateTimeComparator["
                + ( == null ? "" : .getName())
                + "]";
        } else {
            return "DateTimeComparator["
                + ( == null ? "" : .getName())
                + "-"
                + ( == null ? "" : .getName())
                + "]";
        }
    }
New to GrepCode? Check out our FAQ X