List of usage examples for java.util GregorianCalendar before
public boolean before(Object when)
Calendar
represents a time before the time represented by the specified Object
. From source file:Main.java
public static void main(String[] a) { GregorianCalendar today = new GregorianCalendar(); GregorianCalendar thisDate = new GregorianCalendar(); thisDate.set(Calendar.YEAR, 2000); if (thisDate.before(today)) { System.out.println("before"); }//w w w. j a va 2 s .c o m if (today.after(thisDate)) { System.out.println("after"); } }
From source file:MainClass.java
public static void main(String[] args) { GregorianCalendar birthdate = new GregorianCalendar(1999, 1, 1); GregorianCalendar today = new GregorianCalendar(); // Today's date GregorianCalendar birthday = new GregorianCalendar(today.get(YEAR), birthdate.get(MONTH), birthdate.get(DATE));/*from w ww .j a va 2 s. co m*/ int age = today.get(today.YEAR) - birthdate.get(YEAR); String[] weekdays = new DateFormatSymbols().getWeekdays(); // Get day names System.out.println("You were born on a " + weekdays[birthdate.get(DAY_OF_WEEK)]); System.out.println("This year you " + (birthday.after(today) ? " will be " : "are ") + age + " years old."); System.out .println("In " + today.get(YEAR) + " your birthday " + (today.before(birthday) ? "will be" : "was") + " on a " + weekdays[birthday.get(DAY_OF_WEEK)] + "."); }
From source file:de.escoand.readdaily.ReminderHandler.java
public static void startReminder(final Context context, final int hour, final int minute) { Intent intent = new Intent(context, ReminderHandler.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager am = (AlarmManager) context.getSystemService(Activity.ALARM_SERVICE); GregorianCalendar cal = new GregorianCalendar(); // get next reminder cal.set(Calendar.HOUR_OF_DAY, hour); cal.set(Calendar.MINUTE, minute); cal.set(Calendar.SECOND, 0);//from www. j a va2 s . c om cal.set(Calendar.MILLISECOND, 0); if (cal.before(Calendar.getInstance())) cal.add(Calendar.DATE, 1); am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent); LogHandler.log(Log.WARN, "activated " + hour + ":" + minute); }
From source file:au.com.jwatmuff.eventmanager.model.misc.PoolChecker.java
public static int calculateAge(Date dob, Date censusDate) { if (dob == null) return -1; /* calculate age */ GregorianCalendar birthCal = new GregorianCalendar(); birthCal.setTime(dob);// w w w . j av a 2 s . c om int birthYear = birthCal.get(GregorianCalendar.YEAR); GregorianCalendar censusCal = new GregorianCalendar(); censusCal.setTime(censusDate); int censusYear = censusCal.get(GregorianCalendar.YEAR); int age = censusYear - birthYear; birthCal.set(GregorianCalendar.YEAR, censusYear); if (censusCal.before(birthCal)) { age--; } return age; }
From source file:com.jmstudios.redmoon.receiver.AutomaticFilterChangeReceiver.java
public static void scheduleNextAlarm(Context context, String time, Intent operation) { GregorianCalendar calendar = new GregorianCalendar(); calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(time.split(":")[0])); calendar.set(Calendar.MINUTE, Integer.parseInt(time.split(":")[1])); GregorianCalendar now = new GregorianCalendar(); now.add(Calendar.SECOND, 1);//from w ww . j av a2 s . c om if (calendar.before(now)) { calendar.add(Calendar.DATE, 1); } if (DEBUG) Log.i(TAG, "Scheduling alarm for " + calendar.toString()); AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, operation, 0); if (android.os.Build.VERSION.SDK_INT >= 19) { alarmManager.setExact(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent); } else { alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent); } }
From source file:Main.java
/** * Given a calendar (possibly containing only a day of the year), returns the earliest possible * anniversary of the date that is equal to or after the current point in time if the date * does not contain a year, or the date converted to the local time zone (if the date contains * a year./*from w ww. j a v a 2s.c om*/ * * @param target The date we wish to convert(in the UTC time zone). * @return If date does not contain a year (year < 1900), returns the next earliest anniversary * that is after the current point in time (in the local time zone). Otherwise, returns the * adjusted Date in the local time zone. */ public static Date getNextAnnualDate(Calendar target) { final Calendar today = Calendar.getInstance(); today.setTime(new Date()); // Round the current time to the exact start of today so that when we compare // today against the target date, both dates are set to exactly 0000H. today.set(Calendar.HOUR_OF_DAY, 0); today.set(Calendar.MINUTE, 0); today.set(Calendar.SECOND, 0); today.set(Calendar.MILLISECOND, 0); final boolean isYearSet = isYearSet(target); final int targetYear = target.get(Calendar.YEAR); final int targetMonth = target.get(Calendar.MONTH); final int targetDay = target.get(Calendar.DAY_OF_MONTH); final boolean isFeb29 = (targetMonth == Calendar.FEBRUARY && targetDay == 29); final GregorianCalendar anniversary = new GregorianCalendar(); // Convert from the UTC date to the local date. Set the year to today's year if the // there is no provided year (targetYear < 1900) anniversary.set(!isYearSet ? today.get(Calendar.YEAR) : targetYear, targetMonth, targetDay); // If the anniversary's date is before the start of today and there is no year set, // increment the year by 1 so that the returned date is always equal to or greater than // today. If the day is a leap year, keep going until we get the next leap year anniversary // Otherwise if there is already a year set, simply return the exact date. if (!isYearSet) { int anniversaryYear = today.get(Calendar.YEAR); if (anniversary.before(today) || (isFeb29 && !anniversary.isLeapYear(anniversaryYear))) { // If the target date is not Feb 29, then set the anniversary to the next year. // Otherwise, keep going until we find the next leap year (this is not guaranteed // to be in 4 years time). do { anniversaryYear += 1; } while (isFeb29 && !anniversary.isLeapYear(anniversaryYear)); anniversary.set(anniversaryYear, targetMonth, targetDay); } } return anniversary.getTime(); }
From source file:org.oscarehr.web.MisReportUIBean.java
private int calculateResidentDays(Admission admission) { GregorianCalendar startBound = startDate; if (startBound.before(admission.getAdmissionCalendar())) startBound = admission.getAdmissionCalendar(); GregorianCalendar endBound = endDate; if (endBound.after(admission.getDischargeCalendar())) endBound = admission.getDischargeCalendar(); return (DateUtils.calculateDayDifference(startBound, endBound)); }
From source file:com.mb.framework.util.DateTimeUtil.java
/** * This method is used for checking whether the first date is before the * second//from ww w. j a va 2 s . c o m * * @param firstDt * @param secondDt * @return */ public static boolean isBeforeDateTime(GregorianCalendar firstDt, GregorianCalendar secondDt) { return firstDt.before(secondDt); }
From source file:com.mb.framework.util.DateTimeUtil.java
/** * Method called to check whether the specified date is before the current * date./*from w ww .ja v a 2 s.co m*/ * * @param firstDt * the Calendar to be compared with the specified Calendar * @param secondDt * the Calendar to be compared with the specified Calendar * @return <code>true</code> if firstDt is before secondDt * <code>false</code> otherwise */ public static boolean isBeforeDate(GregorianCalendar firstDt, GregorianCalendar secondDt) { if (!isEqualDate(firstDt, secondDt)) { return firstDt.before(secondDt); } return false; }
From source file:org.jamwiki.servlets.PasswordResetServlet.java
private String getChallenge(HttpServletRequest request, WikiPageInfo pageInfo, WikiUser user) throws Exception { int numOfTries = Environment.getIntValue(Environment.PROP_EMAIL_SERVICE_FORGOT_PASSWORD_CHALLENGE_RETRIES); int lockDuration = Environment.getIntValue(Environment.PROP_EMAIL_SERVICE_FORGOT_PASSWORD_IP_LOCK_DURATION); if (user.getChallengeDate() != null && user.getChallengeIp() != null) { // compute some deadlines GregorianCalendar currentDate = new GregorianCalendar(); GregorianCalendar lockExpires = new GregorianCalendar(); lockExpires.setTimeInMillis(user.getChallengeDate().getTime()); lockExpires.add(Calendar.MINUTE, lockDuration); if (request.getRemoteAddr().equals(user.getChallengeIp()) && user.getChallengeTries() >= numOfTries && currentDate.before(lockExpires)) { pageInfo.addError(new WikiMessage("password.reset.password.error.ip.locked")); return null; }/*from ww w . j av a 2s . c om*/ // reset retries after lock timeout if (user.getChallengeTries() >= numOfTries && currentDate.after(lockExpires)) { user.setChallengeTries(0); WikiBase.getDataHandler().updatePwResetChallengeData(user); } } return new Integer((int) (Math.random() * Integer.MAX_VALUE)).toString(); }