Compares whether two calendars refer to the same day. - Android java.util

Android examples for java.util:Day

Description

Compares whether two calendars refer to the same day.

Demo Code

/*/*from ww  w  . jav  a  2s.  c o m*/
 * 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.
 */
//package com.java2s;

import java.util.Calendar;

public class Main {
    /**
     * Compares whether two calendars refer to the same day.
     * @param date1 The first calendar
     * @param date2 The second calendar
     * @return True if both calendars refer to the same day.
     */
    public static boolean isSameDay(Calendar date1, Calendar date2) {
        return compareDates(date1, date2) == 0;
    }

    /**
     * Compares whether two times in milliseconds refer to the same day
     * @param dayMillis1 The first time in milliseconds
     * @param dayMillis2 The second time in milliseconds
     * @return True if both times refer to the same day
     */
    public static boolean isSameDay(long dayMillis1, long dayMillis2) {
        Calendar date1 = getCalendar(dayMillis1);
        Calendar date2 = getCalendar(dayMillis2);
        return compareDates(date1, date2) == 0;
    }

    /**
     * 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;
    }

    /**
     * Retrieve a calendar object set to the specified millisecond time
     * @param timeInMillis the time to set on the calendar
     * @return the calendar object with the specified time.
     */
    public static Calendar getCalendar(long timeInMillis) {
        Calendar date = Calendar.getInstance();
        date.setTimeInMillis(timeInMillis);
        return date;
    }
}

Related Tutorials