Java Date Compare compare(Date start, Date end)

Here you can find the source of compare(Date start, Date end)

Description

compare

License

Apache License

Declaration

public static int compare(Date start, Date end) 

Method Source Code

//package com.java2s;
/**/*w  ww  .j  a v  a 2  s.  com*/
 * Copyright 2008 - 2011 Simcore.org.
 * 
 * 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.
 */

import java.util.Calendar;
import java.util.Date;

public class Main {

    public static int compare(Date start, Date end) {
        Calendar c1 = Calendar.getInstance();
        c1.setTime(start);
        Calendar c2 = Calendar.getInstance();
        c2.setTime(end);
        int year1 = c1.get(Calendar.YEAR);
        int year2 = c2.get(Calendar.YEAR);
        if (year1 != year2) {
            return year1 - year2;
        }
        int month1 = c1.get(Calendar.MONTH);
        int month2 = c2.get(Calendar.MONTH);
        if (month1 != month2) {
            return month1 - month2;
        }
        int day1 = c1.get(Calendar.DAY_OF_MONTH);
        int day2 = c2.get(Calendar.DAY_OF_MONTH);
        return day1 - day2;
    }
}

Related

  1. compare(Date d1, Date d2)
  2. compare(Date d1, Date d2)
  3. compare(Date date, Date fromDate, Date toDate)
  4. compare(Date date1, Date date2, String format)
  5. Compare(Date dtA, Date dtB, boolean bUnkIsPast)
  6. compare(Date start, String end)
  7. compare(Date startDate, Date endDate, Date targetDate)
  8. compare(final Date lhs, final Date rhs)
  9. compare(final Date one, final Date another)