List of usage examples for java.util Calendar before
public boolean before(Object when)
Calendar
represents a time before the time represented by the specified Object
. From source file:gov.nih.nci.ncicb.tcga.dcc.qclive.common.util.DateComparator.java
/** * Return <code>true</code> if left operand is on or after right operand, or if any of the operand is null, <code>false</code> otherwise * * * @param leftOperandValue the <code>Calendar</code> value for the left operand * @param rightOperandValue the <code>Calendar</code> value for the right operand * @param leastPrecision the least precise precision of the 2 dates * @return <code>true</code> if left operand is on or after right operand, or if any of the operand is null, <code>false</code> otherwise *///ww w . ja v a2 s. c o m private boolean isGreaterThanOrEqual(final Calendar leftOperandValue, final Calendar rightOperandValue, final String leastPrecision) { if (leftOperandValue == null || rightOperandValue == null) { return true; } else { if (PRECISION_YEAR.equals(leastPrecision)) { return leftOperandValue.get(Calendar.YEAR) >= rightOperandValue.get(Calendar.YEAR); } else if (PRECISION_MONTH.equals(leastPrecision)) { return (leftOperandValue.get(Calendar.YEAR) > rightOperandValue.get(Calendar.YEAR)) || (leftOperandValue.get(Calendar.YEAR) == rightOperandValue.get(Calendar.YEAR) && leftOperandValue.get(Calendar.MONTH) >= rightOperandValue.get(Calendar.MONTH)); } else { return !leftOperandValue.before(rightOperandValue); } } }
From source file:com.android.deskclock.alarms.AlarmStateManager.java
/** * This registers the AlarmInstance to the state manager. This will look at the instance * and choose the most appropriate state to put it in. This is primarily used by new * alarms, but it can also be called when the system time changes. * * Most state changes are handled by the states themselves, but during major time changes we * have to correct the alarm instance state. This means we have to handle special cases as * describe below:/* www. j a va 2 s .c o m*/ * * <ul> * <li>Make sure all dismissed alarms are never re-activated</li> * <li>Make sure pre-dismissed alarms stay predismissed</li> * <li>Make sure firing alarms stayed fired unless they should be auto-silenced</li> * <li>Missed instance that have parents should be re-enabled if we went back in time</li> * <li>If alarm was SNOOZED, then show the notification but don't update time</li> * <li>If low priority notification was hidden, then make sure it stays hidden</li> * </ul> * * If none of these special case are found, then we just check the time and see what is the * proper state for the instance. * * @param context application context * @param instance to register */ public static void registerInstance(Context context, AlarmInstance instance, boolean updateNextAlarm) { final ContentResolver cr = context.getContentResolver(); final Alarm alarm = Alarm.getAlarm(cr, instance.mAlarmId); final Calendar currentTime = getCurrentTime(); final Calendar alarmTime = instance.getAlarmTime(); final Calendar timeoutTime = instance.getTimeout(context); final Calendar lowNotificationTime = instance.getLowNotificationTime(); final Calendar highNotificationTime = instance.getHighNotificationTime(); final Calendar missedTTL = instance.getMissedTimeToLive(); // Handle special use cases here if (instance.mAlarmState == AlarmInstance.DISMISSED_STATE) { // This should never happen, but add a quick check here LogUtils.e("Alarm Instance is dismissed, but never deleted"); deleteInstanceAndUpdateParent(context, instance); return; } else if (instance.mAlarmState == AlarmInstance.FIRED_STATE) { // Keep alarm firing, unless it should be timed out boolean hasTimeout = timeoutTime != null && currentTime.after(timeoutTime); if (!hasTimeout) { setFiredState(context, instance); return; } } else if (instance.mAlarmState == AlarmInstance.MISSED_STATE) { if (currentTime.before(alarmTime)) { if (instance.mAlarmId == null) { LogUtils.i("Cannot restore missed instance for one-time alarm"); // This instance parent got deleted (ie. deleteAfterUse), so // we should not re-activate it.- deleteInstanceAndUpdateParent(context, instance); return; } // TODO: This will re-activate missed snoozed alarms, but will // use our normal notifications. This is not ideal, but very rare use-case. // We should look into fixing this in the future. // Make sure we re-enable the parent alarm of the instance // because it will get activated by by the below code alarm.enabled = true; Alarm.updateAlarm(cr, alarm); } } else if (instance.mAlarmState == AlarmInstance.PREDISMISSED_STATE) { if (currentTime.before(alarmTime)) { setPreDismissState(context, instance); } else { deleteInstanceAndUpdateParent(context, instance); } return; } // Fix states that are time sensitive if (currentTime.after(missedTTL)) { // Alarm is so old, just dismiss it deleteInstanceAndUpdateParent(context, instance); } else if (currentTime.after(alarmTime)) { // There is a chance that the TIME_SET occurred right when the alarm should go off, so // we need to add a check to see if we should fire the alarm instead of marking it // missed. Calendar alarmBuffer = Calendar.getInstance(); alarmBuffer.setTime(alarmTime.getTime()); alarmBuffer.add(Calendar.SECOND, ALARM_FIRE_BUFFER); if (currentTime.before(alarmBuffer)) { setFiredState(context, instance); } else { setMissedState(context, instance); } } else if (instance.mAlarmState == AlarmInstance.SNOOZE_STATE) { // We only want to display snooze notification and not update the time, // so handle showing the notification directly AlarmNotifications.showSnoozeNotification(context, instance); scheduleInstanceStateChange(context, instance.getAlarmTime(), instance, AlarmInstance.FIRED_STATE); } else if (currentTime.after(highNotificationTime)) { setHighNotificationState(context, instance); } else if (currentTime.after(lowNotificationTime)) { // Only show low notification if it wasn't hidden in the past if (instance.mAlarmState == AlarmInstance.HIDE_NOTIFICATION_STATE) { setHideNotificationState(context, instance); } else { setLowNotificationState(context, instance); } } else { // Alarm is still active, so initialize as a silent alarm setSilentState(context, instance); } // The caller prefers to handle updateNextAlarm for optimization if (updateNextAlarm) { updateNextAlarm(context); } }
From source file:com.kyleszombathy.sms_scheduler.AddMessage.java
/**Sets date/time to current time if selected time is before the current time*/ private void fixDateIfBeforeCurrentTime() { Calendar selDateTime = datePicker.getSelectedDate(); Calendar oneMinuteAgo = Calendar.getInstance(); oneMinuteAgo.add(Calendar.MINUTE, -1); if (selDateTime.before(oneMinuteAgo)) { fixDateTime();//from w w w. ja v a2 s . c om Toast.makeText(this, R.string.FixDateTimeToast, Toast.LENGTH_LONG).show(); } }
From source file:org.wso2.carbon.apimgt.usage.client.APIUsageStatisticsClient.java
private void insertZeroElementsByType(Calendar from, Calendar to, List<Result<ExecutionTimeOfAPIValues>> resultList, int field) { List<Result<ExecutionTimeOfAPIValues>> tempList = new ArrayList<Result<ExecutionTimeOfAPIValues>>(); Calendar checkedDate = Calendar.getInstance(); Set<String> mediationTypes = new HashSet<String>(); for (Date date = from.getTime(); from.before(to); from.add(field, 1), date = from.getTime()) { checkedDate.setTime(date);//from w w w . j a v a 2 s . c om boolean status = false; for (Result<ExecutionTimeOfAPIValues> executionTimeOfAPIValuesResult : resultList) { int year = executionTimeOfAPIValuesResult.getValues().getYear(); int month = executionTimeOfAPIValuesResult.getValues().getMonth(); int day = executionTimeOfAPIValuesResult.getValues().getDay(); int hour = executionTimeOfAPIValuesResult.getValues().getHour(); int minute = executionTimeOfAPIValuesResult.getValues().getMinutes(); int seconds = executionTimeOfAPIValuesResult.getValues().getSeconds(); mediationTypes.add(executionTimeOfAPIValuesResult.getValues().getMediationName()); if (checkedDate.get(Calendar.YEAR) == year && checkedDate.get(Calendar.MONTH) + 1 == month) { if (field == Calendar.DATE && checkedDate.get(field) == day) { status = true; break; } else if (field == Calendar.HOUR_OF_DAY && checkedDate.get(Calendar.DATE) == day && checkedDate.get(field) == hour) { status = true; break; } else if (field == Calendar.MINUTE && checkedDate.get(Calendar.DATE) == day && checkedDate.get(Calendar.HOUR_OF_DAY) == hour && checkedDate.get(field) == minute) { status = true; break; } else if (field == Calendar.SECOND && checkedDate.get(Calendar.DATE) == day && checkedDate.get(Calendar.HOUR_OF_DAY) == hour && checkedDate.get(Calendar.MINUTE) == minute && checkedDate.get(field) == seconds) { status = true; break; } } } if (!status) { int hour, minutes = 0, seconds = 0; if (field == Calendar.HOUR_OF_DAY) { hour = checkedDate.get(Calendar.HOUR_OF_DAY); } else if (field == Calendar.MINUTE) { hour = checkedDate.get(Calendar.HOUR_OF_DAY); minutes = checkedDate.get(Calendar.MINUTE); } else { hour = checkedDate.get(Calendar.HOUR_OF_DAY); minutes = checkedDate.get(Calendar.MINUTE); seconds = checkedDate.get(Calendar.SECOND); } for (String mediation : mediationTypes) { Result<ExecutionTimeOfAPIValues> tempResult = new Result<ExecutionTimeOfAPIValues>(); ExecutionTimeOfAPIValues executionTimeOfAPIValues = new ExecutionTimeOfAPIValues(); executionTimeOfAPIValues.setYear(checkedDate.get(Calendar.YEAR)); executionTimeOfAPIValues.setMonth(checkedDate.get(Calendar.MONTH) + 1); executionTimeOfAPIValues.setDay(checkedDate.get(Calendar.DATE)); executionTimeOfAPIValues.setHour(hour); executionTimeOfAPIValues.setMinutes(minutes); executionTimeOfAPIValues.setSeconds(seconds); executionTimeOfAPIValues.setMediationName(mediation); tempResult.setValues(executionTimeOfAPIValues); tempList.add(tempResult); } } } resultList.addAll(tempList); }
From source file:com.androidinspain.deskclock.alarms.AlarmStateManager.java
/** * This registers the AlarmInstance to the state manager. This will look at the instance * and choose the most appropriate state to put it in. This is primarily used by new * alarms, but it can also be called when the system time changes. * * Most state changes are handled by the states themselves, but during major time changes we * have to correct the alarm instance state. This means we have to handle special cases as * describe below:/*from w w w . jav a2 s . com*/ * * <ul> * <li>Make sure all dismissed alarms are never re-activated</li> * <li>Make sure pre-dismissed alarms stay predismissed</li> * <li>Make sure firing alarms stayed fired unless they should be auto-silenced</li> * <li>Missed instance that have parents should be re-enabled if we went back in time</li> * <li>If alarm was SNOOZED, then show the notification but don't update time</li> * <li>If low priority notification was hidden, then make sure it stays hidden</li> * </ul> * * If none of these special case are found, then we just check the time and see what is the * proper state for the instance. * * @param context application context * @param instance to register */ public static void registerInstance(Context context, AlarmInstance instance, boolean updateNextAlarm) { LogUtils.i("Registering instance: " + instance.mId); final ContentResolver cr = context.getContentResolver(); final Alarm alarm = Alarm.getAlarm(cr, instance.mAlarmId); final Calendar currentTime = getCurrentTime(); final Calendar alarmTime = instance.getAlarmTime(); final Calendar timeoutTime = instance.getTimeout(); final Calendar lowNotificationTime = instance.getLowNotificationTime(); final Calendar highNotificationTime = instance.getHighNotificationTime(); final Calendar missedTTL = instance.getMissedTimeToLive(); // Handle special use cases here if (instance.mAlarmState == AlarmInstance.DISMISSED_STATE) { // This should never happen, but add a quick check here LogUtils.e("Alarm Instance is dismissed, but never deleted"); deleteInstanceAndUpdateParent(context, instance); return; } else if (instance.mAlarmState == AlarmInstance.FIRED_STATE) { // Keep alarm firing, unless it should be timed out boolean hasTimeout = timeoutTime != null && currentTime.after(timeoutTime); if (!hasTimeout) { setFiredState(context, instance); return; } } else if (instance.mAlarmState == AlarmInstance.MISSED_STATE) { if (currentTime.before(alarmTime)) { if (instance.mAlarmId == null) { LogUtils.i("Cannot restore missed instance for one-time alarm"); // This instance parent got deleted (ie. deleteAfterUse), so // we should not re-activate it.- deleteInstanceAndUpdateParent(context, instance); return; } // TODO: This will re-activate missed snoozed alarms, but will // use our normal notifications. This is not ideal, but very rare use-case. // We should look into fixing this in the future. // Make sure we re-enable the parent alarm of the instance // because it will get activated by by the below code alarm.enabled = true; Alarm.updateAlarm(cr, alarm); } } else if (instance.mAlarmState == AlarmInstance.PREDISMISSED_STATE) { if (currentTime.before(alarmTime)) { setPreDismissState(context, instance); } else { deleteInstanceAndUpdateParent(context, instance); } return; } // Fix states that are time sensitive if (currentTime.after(missedTTL)) { // Alarm is so old, just dismiss it deleteInstanceAndUpdateParent(context, instance); } else if (currentTime.after(alarmTime)) { // There is a chance that the TIME_SET occurred right when the alarm should go off, so // we need to add a check to see if we should fire the alarm instead of marking it // missed. Calendar alarmBuffer = Calendar.getInstance(); alarmBuffer.setTime(alarmTime.getTime()); alarmBuffer.add(Calendar.SECOND, ALARM_FIRE_BUFFER); if (currentTime.before(alarmBuffer)) { setFiredState(context, instance); } else { setMissedState(context, instance); } } else if (instance.mAlarmState == AlarmInstance.SNOOZE_STATE) { // We only want to display snooze notification and not update the time, // so handle showing the notification directly AlarmNotifications.showSnoozeNotification(context, instance); scheduleInstanceStateChange(context, instance.getAlarmTime(), instance, AlarmInstance.FIRED_STATE); } else if (currentTime.after(highNotificationTime)) { setHighNotificationState(context, instance); } else if (currentTime.after(lowNotificationTime)) { // Only show low notification if it wasn't hidden in the past if (instance.mAlarmState == AlarmInstance.HIDE_NOTIFICATION_STATE) { setHideNotificationState(context, instance); } else { setLowNotificationState(context, instance); } } else { // Alarm is still active, so initialize as a silent alarm setSilentState(context, instance); } // The caller prefers to handle updateNextAlarm for optimization if (updateNextAlarm) { updateNextAlarm(context); } }
From source file:com.kyleszombathy.sms_scheduler.AddMessage.java
private void validateDateTime() { Calendar selDateTime = datePicker.getSelectedDate(); // Get time 5 minutes from now GregorianCalendar in5Mins = new GregorianCalendar(); in5Mins.add(Calendar.MINUTE, 5); if (selDateTime.before(new GregorianCalendar())) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(R.string.ValidateDateTimeDialogTitle1) .setMessage(R.string.ValidateDateTimeDialogMessage1) .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // Do nothing }/*from w ww . ja va 2 s . co m*/ }).show(); dateTimeIsValid = false; } else if (selDateTime.before(in5Mins)) { showValidateDateTimeDialog(); dateTimeIsValid = false; } else { dateTimeIsValid = true; } }
From source file:com.stasbar.knowyourself.alarms.AlarmStateManager.java
/** * Fix and update all alarm instance when a time change event occurs. * * @param context application context//from w ww . j a v a2 s . c o m */ public static void fixAlarmInstances(Context context) { // Register all instances after major time changes or when phone restarts final ContentResolver contentResolver = context.getContentResolver(); final Calendar currentTime = getCurrentTime(); // Sort the instances in reverse chronological order so that later instances are fixed or // deleted before re-scheduling prior instances (which may re-create or update the later // instances). final List<AlarmInstance> instances = AlarmInstance.getInstances(contentResolver, null /* selection */); Collections.sort(instances, new Comparator<AlarmInstance>() { @Override public int compare(AlarmInstance lhs, AlarmInstance rhs) { return rhs.getAlarmTime().compareTo(lhs.getAlarmTime()); } }); for (AlarmInstance instance : instances) { final Alarm alarm = Alarm.getAlarm(contentResolver, instance.mAlarmId); if (alarm == null) { unregisterInstance(context, instance); AlarmInstance.deleteInstance(contentResolver, instance.mId); LogUtils.e("Found instance without matching alarm; deleting instance %s", instance); continue; } final Calendar priorAlarmTime = alarm.getPreviousAlarmTime(instance.getAlarmTime()); final Calendar missedTTLTime = instance.getMissedTimeToLive(); if (currentTime.before(priorAlarmTime) || currentTime.after(missedTTLTime)) { final Calendar oldAlarmTime = instance.getAlarmTime(); final Calendar newAlarmTime = alarm.getNextAlarmTime(currentTime); final CharSequence oldTime = DateFormat.format("MM/dd/yyyy hh:mm a", oldAlarmTime); final CharSequence newTime = DateFormat.format("MM/dd/yyyy hh:mm a", newAlarmTime); LogUtils.i("A time change has caused an existing alarm scheduled to fire at %s to" + " be replaced by a new alarm scheduled to fire at %s", oldTime, newTime); // The time change is so dramatic the AlarmInstance doesn't make any sense; // remove it and schedule the new appropriate instance. AlarmStateManager.deleteInstanceAndUpdateParent(context, instance); } else { registerInstance(context, instance, false /* updateNextAlarm */); } } updateNextAlarm(context); }
From source file:gov.nih.nci.ncicb.tcga.dcc.qclive.common.util.DateComparator.java
/** * Return <code>true</code> if left operand is strictly before right operand, or if any of the operand is null, <code>false</code> otherwise * * * @param leftOperandValue the <code>Calendar</code> value for the left operand * @param rightOperandValue the <code>Calendar</code> value for the right operand * @param leastPrecision the least-precise precision of the two dates: day, month, or year * @return <code>true</code> if left operand is strictly before right operand, or if any of the operand is null, <code>false</code> otherwise *///from ww w . java 2 s . c o m private boolean isLowerThan(final Calendar leftOperandValue, final Calendar rightOperandValue, final String leastPrecision) { if (leftOperandValue == null || rightOperandValue == null) { return true; } else { if (PRECISION_YEAR.equals(leastPrecision)) { // if year is the same, can't say for sure so return true, so don't just do < comparison return leftOperandValue.get(Calendar.YEAR) <= rightOperandValue.get(Calendar.YEAR); } else if (PRECISION_MONTH.equals(leastPrecision)) { return (leftOperandValue.get(Calendar.YEAR) < rightOperandValue.get(Calendar.YEAR)) || (leftOperandValue.get(Calendar.YEAR) == rightOperandValue.get(Calendar.YEAR) && leftOperandValue.get(Calendar.MONTH) <= rightOperandValue.get(Calendar.MONTH)); } else { return leftOperandValue.before(rightOperandValue); } } }
From source file:com.androidinspain.deskclock.alarms.AlarmStateManager.java
/** * Fix and update all alarm instance when a time change event occurs. * * @param context application context//from w ww .j a v a 2 s. c om */ public static void fixAlarmInstances(Context context) { LogUtils.i("Fixing alarm instances"); // Register all instances after major time changes or when phone restarts final ContentResolver contentResolver = context.getContentResolver(); final Calendar currentTime = getCurrentTime(); // Sort the instances in reverse chronological order so that later instances are fixed or // deleted before re-scheduling prior instances (which may re-create or update the later // instances). final List<AlarmInstance> instances = AlarmInstance.getInstances(contentResolver, null /* selection */); Collections.sort(instances, new Comparator<AlarmInstance>() { @Override public int compare(AlarmInstance lhs, AlarmInstance rhs) { return rhs.getAlarmTime().compareTo(lhs.getAlarmTime()); } }); for (AlarmInstance instance : instances) { final Alarm alarm = Alarm.getAlarm(contentResolver, instance.mAlarmId); if (alarm == null) { unregisterInstance(context, instance); AlarmInstance.deleteInstance(contentResolver, instance.mId); LogUtils.e("Found instance without matching alarm; deleting instance %s", instance); continue; } final Calendar priorAlarmTime = alarm.getPreviousAlarmTime(instance.getAlarmTime()); final Calendar missedTTLTime = instance.getMissedTimeToLive(); if (currentTime.before(priorAlarmTime) || currentTime.after(missedTTLTime)) { final Calendar oldAlarmTime = instance.getAlarmTime(); final Calendar newAlarmTime = alarm.getNextAlarmTime(currentTime); final CharSequence oldTime = DateFormat.format("MM/dd/yyyy hh:mm a", oldAlarmTime); final CharSequence newTime = DateFormat.format("MM/dd/yyyy hh:mm a", newAlarmTime); LogUtils.i("A time change has caused an existing alarm scheduled to fire at %s to" + " be replaced by a new alarm scheduled to fire at %s", oldTime, newTime); // The time change is so dramatic the AlarmInstance doesn't make any sense; // remove it and schedule the new appropriate instance. AlarmStateManager.deleteInstanceAndUpdateParent(context, instance); } else { registerInstance(context, instance, false /* updateNextAlarm */); } } updateNextAlarm(context); }
From source file:gov.utah.dts.sdc.actions.CommercialStudentCreateAction.java
/** * Checks if date1 is in the between date2 and today date inclusively. * @param date1/* w ww . j a va 2 s. c o m*/ * @param date2 * @return */ private boolean isValidCompletionDate(Date date1, Date date2) { Calendar today = Calendar.getInstance(); Calendar date1Cal = Calendar.getInstance(); date1Cal.setTime(date1); Calendar date2Cal = Calendar.getInstance(); date2Cal.setTime(date2); // set hour, minute, and secod to 0 today.set(Calendar.HOUR_OF_DAY, 0); today.set(Calendar.MINUTE, 0); today.set(Calendar.SECOND, 0); today.set(Calendar.MILLISECOND, 0); date1Cal.set(Calendar.HOUR_OF_DAY, 0); date1Cal.set(Calendar.MINUTE, 0); date1Cal.set(Calendar.SECOND, 0); date1Cal.set(Calendar.MILLISECOND, 0); date2Cal.set(Calendar.HOUR_OF_DAY, 0); date2Cal.set(Calendar.MINUTE, 0); date2Cal.set(Calendar.SECOND, 0); if (date2Cal.after(today)) return false; if (date1Cal.equals(date2Cal) || date1Cal.equals(today)) return true; if (date1Cal.before(today) && date1Cal.after(date2Cal)) return true; return false; }