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:Main.java
/** * get month between minTime and maxTime, including head and tail * * @param minTime/* ww w . ja v a2s . c om*/ * @param maxTime * @return */ public static int getMonthNum(long minTime, long maxTime) { Calendar min = Calendar.getInstance(); min.setTimeInMillis(minTime); Calendar max = Calendar.getInstance(); max.setTimeInMillis(maxTime); if (max.before(min)) { throw new IllegalArgumentException("max date is before min date"); } int minMonth = min.get(Calendar.MONTH) + 1; int maxMonth = max.get(Calendar.MONTH) + 1; return (max.get(Calendar.YEAR) - min.get(Calendar.YEAR)) * 12 + maxMonth - minMonth + 1; }
From source file:Main.java
public static int getUserAge(Date birthDay) { Calendar cal = Calendar.getInstance(); if (cal.before(birthDay)) { return 0; }/*from w w w . j av a2 s.c om*/ int yearNow = cal.get(Calendar.YEAR); int monthNow = cal.get(Calendar.MONTH); int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH); if (null == birthDay) { return 0; } else { cal.setTime(birthDay); int yearBirth = cal.get(Calendar.YEAR); int monthBirth = cal.get(Calendar.MONTH); int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH); int age = yearNow - yearBirth; if (monthNow <= monthBirth) { if (monthNow == monthBirth) { // monthNow==monthBirth if (dayOfMonthNow < dayOfMonthBirth) { age--; } else { // do nothing } } else { // monthNow>monthBirth age--; } } else { // monthNow<monthBirth // donothing } return age; } }
From source file:Main.java
private static boolean isCurrentTimeBeforeConfigedUpdateTime() { Calendar current = Calendar.getInstance(); Calendar configedTime = getTodayAutoPushTime(); return current.before(configedTime); }
From source file:Main.java
public static Calendar nextBirthday(Calendar today, Calendar dayOfBirth) { Calendar nextBirthday = (Calendar) dayOfBirth.clone(); nextBirthday.set(Calendar.YEAR, today.get(Calendar.YEAR)); if (nextBirthday.before(today)) { nextBirthday.add(Calendar.YEAR, 1); }/* ww w . j av a2 s . co m*/ return nextBirthday; }
From source file:Main.java
/** * Checks for the given calendar is in coming 7 days * @param calendar/* w ww . j a v a 2 s .co m*/ * @return true if given calendar is in coming week */ public static boolean isComingWeek(Calendar calendar) { boolean flag = false; Calendar calendar2 = Calendar.getInstance(); calendar2.add(Calendar.DATE, 7); if (calendar.before(calendar2)) { flag = true; } return flag; }
From source file:MainClass.java
public static int diff(Date date1, Date date2) { Calendar c1 = Calendar.getInstance(); Calendar c2 = Calendar.getInstance(); c1.setTime(date1);//from w ww.j a v a2 s.c om c2.setTime(date2); int diffDay = 0; if (c1.before(c2)) { diffDay = countDiffDay(c1, c2); } else { diffDay = countDiffDay(c2, c1); } return diffDay; }
From source file:Main.java
public static boolean isBrithdayAvable(Long d) { if (d == null) { return false; }//from w w w . j av a2 s. co m boolean back = false; Calendar c = Calendar.getInstance(); c.setTimeInMillis(d.longValue()); Calendar current = Calendar.getInstance(); if (c.get(Calendar.YEAR) > 1801 && c.before(current) && c.get(Calendar.YEAR) <= current.get(Calendar.YEAR)) { return true; } return back; }
From source file:MainGeneratePicasaIniFile.java
public static long daysBetween(Calendar startDate, Calendar endDate) { Calendar date = (Calendar) startDate.clone(); long daysBetween = 0; while (date.before(endDate)) { date.add(Calendar.DAY_OF_MONTH, 1); daysBetween++;//from www . ja va 2 s . com } return daysBetween; }
From source file:Main.java
public static int getDayCounts(Date startDate, Date endDate) { int days = 0; Calendar startCalendar = Calendar.getInstance(); startCalendar.setTime(startDate);// www. j ava 2 s. c o m Calendar endCalendar = Calendar.getInstance(); endCalendar.setTime(endDate); while (startCalendar.before(endCalendar)) { days++; startCalendar.add(Calendar.DATE, 1); } return days; }
From source file:Main.java
static int dateDiff(int type, Calendar fromDate, Calendar toDate, boolean future) { int diff = 0; long savedDate = fromDate.getTimeInMillis(); while ((future && !fromDate.after(toDate)) || (!future && !fromDate.before(toDate))) { savedDate = fromDate.getTimeInMillis(); fromDate.add(type, future ? 1 : -1); diff++;/*from ww w . ja v a2s.c o m*/ } diff--; fromDate.setTimeInMillis(savedDate); return diff; }