Android examples for java.util:Date Compare
Compares two Calendars for equality down to a given precision.
import android.util.Log; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; import java.util.TimeZone; public class Main{ private static final String TAG = TimeUtils.class.getName(); /**Hours elapsed in current day (24 hour format)**/ public static final int HOUR_OF_DAY = 4; /**Days elapsed in current year**/ public static final int DAY_OF_YEAR = 8; /** Second precision **/ public static final int SECOND = 16; /** Minute precision **/ public static final int MINUTE = 32; /** Hour precision **/ public static final int HOUR = 64; /** Day precision **/ public static final int DAY = 128; /** Week precision **/ public static final int WEEK = 256; /** Month precision **/ public static final int MONTH = 512; /** Year precision **/ public static final int YEAR = 1024; private static final String ERR_MESSAGE_INVALID_FIELD = "Invalid time field requested"; /**/*from w w w. java 2s . c o m*/ * Compares two Calendars for equality down to a given precision. To specify precision use one of the following * class constants * * @param cal1 first Calendar to compare * @param cal2 second Calendar to compare * @param field precision required for equality * @return true if Calendars are equal down to specified precision */ public static boolean compareCalendarsToPrecision(Calendar cal1, Calendar cal2, int field) { switch (field) { case SECOND: if (cal1.get(Calendar.SECOND) != cal2.get(Calendar.SECOND)) { return false; } case MINUTE: if (cal1.get(Calendar.MINUTE) != cal2.get(Calendar.MINUTE)) { return false; } case HOUR: if (cal1.get(Calendar.HOUR_OF_DAY) != cal2 .get(Calendar.HOUR_OF_DAY)) { return false; } case DAY: if (cal1.get(Calendar.DAY_OF_YEAR) != cal2 .get(Calendar.DAY_OF_YEAR)) { return false; } case WEEK: if (cal1.get(Calendar.WEEK_OF_YEAR) != cal2 .get(Calendar.WEEK_OF_YEAR)) { return false; } case MONTH: if (cal1.get(Calendar.MONTH) != cal2.get(Calendar.MONTH)) { return false; } case YEAR: if (cal1.get(Calendar.YEAR) != cal2.get(Calendar.YEAR)) { return false; } default: Log.e(TAG, ERR_MESSAGE_INVALID_FIELD + "(in compareCalendarsToPrecision)"); } //Everything matched up to desired precision return true; } }