Java tutorial
//package com.java2s; /* * Copyright 2014 Kevin Quan (kevin.quan@gmail.com) * * 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; public class Main { /** * Compares only the month, day, and year of the provided calendars * @param date1 The calendar to compare against * @param date2 The calendar to compare * @return -1 if the first calendar is earlier, 1 if the second calendar is earlier, 0 otherwise */ public static int compareDates(Calendar date1, Calendar date2) { if (date1.get(Calendar.YEAR) < date2.get(Calendar.YEAR)) { return -1; } else if (date1.get(Calendar.YEAR) > date2.get(Calendar.YEAR)) { return 1; } // Years are equal if (date1.get(Calendar.MONTH) < date2.get(Calendar.MONTH)) { return -1; } else if (date1.get(Calendar.MONTH) > date2.get(Calendar.MONTH)) { return 1; } // Years and months are equal if (date1.get(Calendar.DAY_OF_MONTH) < date2.get(Calendar.DAY_OF_MONTH)) { return -1; } else if (date1.get(Calendar.DAY_OF_MONTH) > date2.get(Calendar.DAY_OF_MONTH)) { return 1; } return 0; } }