001    /*--------------------------------------------------------------------------+
002    $Id: DateUtils.java 29593 2010-08-09 16:01:20Z hummelb $
003    |                                                                          |
004    | Copyright 2005-2010 Technische Universitaet Muenchen                     |
005    |                                                                          |
006    | Licensed under the Apache License, Version 2.0 (the "License");          |
007    | you may not use this file except in compliance with the License.         |
008    | You may obtain a copy of the License at                                  |
009    |                                                                          |
010    |    http://www.apache.org/licenses/LICENSE-2.0                            |
011    |                                                                          |
012    | Unless required by applicable law or agreed to in writing, software      |
013    | distributed under the License is distributed on an "AS IS" BASIS,        |
014    | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
015    | See the License for the specific language governing permissions and      |
016    | limitations under the License.                                           |
017    +--------------------------------------------------------------------------*/
018    package edu.tum.cs.commons.date;
019    
020    import java.util.Collection;
021    import java.util.Collections;
022    import java.util.Date;
023    
024    /**
025     * Utility methods for working on date objects.
026     * 
027     * @author juergens
028     * @author $Author: hummelb $
029     * @version $Rev: 29593 $
030     * @levd.rating GREEN Hash: A327B6CB6CF8E826F60F8D16EBA23DEC
031     */
032    public class DateUtils {
033    
034            /** Returns the latest date in a collection of dates */
035            public static Date getLatest(Collection<Date> dates) {
036                    if (dates.isEmpty()) {
037                            return null;
038                    }
039                    return Collections.max(dates);
040            }
041    
042            /** Returns the earliest date in a collection of dates */
043            public static Date getEarliest(Collection<Date> dates) {
044                    if (dates.isEmpty()) {
045                            return null;
046                    }
047                    return Collections.min(dates);
048            }
049    
050            /** Returns the earlier of two dates, or null, if one of the dates is null */
051            public static Date min(Date d1, Date d2) {
052                    if (d1 == null || d2 == null) {
053                            return null;
054                    }
055    
056                    if (d1.compareTo(d2) < 0) {
057                            return d1;
058                    }
059                    return d2;
060            }
061    
062            /** Returns the later of two dates or null, if one of the dates is null. */
063            public static Date max(Date d1, Date d2) {
064                    if (d1 == null || d2 == null) {
065                            return null;
066                    }
067    
068                    if (d2.compareTo(d1) > 0) {
069                            return d2;
070                    }
071                    return d1;
072            }
073    
074    }