Java Code Examples for java.util.Calendar.after()

The following are Jave code examples for showing how to use after() of the java.util.Calendar class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: javaportfolio   File: XIRRData.java   View Source Code Vote up 7 votes
/**
	 * Calculates the number of days between two calendar days in a manner
	 * which is independent of the Calendar type used.
	 *
	 * @param d1    The first date.
	 * @param d2    The second date.
	 *
	 * @return      The number of days between the two dates.  Zero is
	 *              returned if the dates are the same, one if the dates are
	 *              adjacent, etc.  The order of the dates
	 *              does not matter, the value returned is always >= 0.
	 *              If Calendar types of d1 and d2
	 *              are different, the result may not be accurate.
	 */
	public static int getDaysBetween( Calendar d1, Calendar d2 ) {
		if ( d1.after(d2) ) {
			// swap dates so that d1 is start and d2 is end
			Calendar swap = d1;
			d1 = d2;
			d2 = swap;
		}
		long days = (d2.getTimeInMillis() - d1.getTimeInMillis()) / (1000 * 60 * 60 * 24);
		return (int) days;
		
		
//		int days = d2.get(Calendar.DAY_OF_YEAR) - d1.get(Calendar.DAY_OF_YEAR);
//		int y2   = d2.get(Calendar.YEAR);
//		if (d1.get(Calendar.YEAR) != y2) {
//			d1 = (Calendar) d1.clone();
//			do {
//				days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);
//				d1.add(Calendar.YEAR, 1);
//			} while (d1.get(Calendar.YEAR) != y2);
//		}
//		return days;
	}
 
Example 2
Project: NeverLag   File: DateUtils.java   View Source Code Vote up 6 votes
public static String formatDateDiff(Calendar fromDate, Calendar toDate) {
	boolean future = false;
	if (toDate.equals(fromDate)) {
		return I18N.tr("now");
	}
	if (toDate.after(fromDate)) {
		future = true;
	}
	StringBuilder sb = new StringBuilder();
	int[] types = { Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH, Calendar.HOUR_OF_DAY, Calendar.MINUTE, Calendar.SECOND };
	String[] names = {
		I18N.tr("year"), I18N.tr("years"),
		I18N.tr("month"), I18N.tr("months"),
		I18N.tr("day"), I18N.tr("days"),
		I18N.tr("hour"), I18N.tr("hours"),
		I18N.tr("minute"), I18N.tr("minutes"),
		I18N.tr("second"), I18N.tr("seconds")
	};
	int accuracy = 0;
	for (int i = 0; i < types.length; i++) {
		if (accuracy > 2) {
			break;
		}
		int diff = dateDiff(types[i], fromDate, toDate, future);
		if (diff > 0) {
			accuracy++;
			sb.append(" ").append(diff).append(" ").append(names[i * 2 + (diff > 1 ? 1 : 0)]);
		}
	}
	if (sb.length() == 0) {
		return "now";
	}
	return sb.toString().trim();
}
 
Example 3
Project: happy-news   File: NotificationSetting.java   View Source Code Vote up 6 votes
@Override
public int compareTo(Object o) {
    Calendar time = Calendar.getInstance();
    time.getTimeInMillis();
    Calendar other = Calendar.getInstance();
    other.getTimeInMillis();

    time.set(Calendar.HOUR_OF_DAY, this.hour);
    time.set(Calendar.MINUTE, this.minute);

    NotificationSetting otherNs = (NotificationSetting) o;
    other.set(Calendar.HOUR_OF_DAY, otherNs.hour);
    other.set(Calendar.MINUTE, otherNs.minute);

    if (time.before(other)) {
        return -1;
    } else if (time.after(other)) {
        return 1;
    }
    return 0;
}
 
Example 4
Project: GongXianSheng   File: DateFormateUtil.java   View Source Code Vote up 6 votes
/**
 * Description:判断是不是进年
 * 
 * @param c
 * @return
 * @return boolean
 * @author name:拜力文
 *         <p>=
 *         ===========================================
 *         </p>
 *         Modified No: Modified By: Modified Date: Modified Description:
 *         <p>=
 *         ===========================================
 *         </p>
 **/
public static boolean isCurrentYear(Calendar c) {
	Calendar today_start = Calendar
			.getInstance(TimeZone.getTimeZone(GMT_8));
	today_start.set(Calendar.HOUR_OF_DAY, 0);
	today_start.set(Calendar.MINUTE, 0);
	today_start.set(Calendar.MONTH, 0);
	today_start.set(Calendar.DAY_OF_MONTH, 1);
	today_start.set(Calendar.SECOND, 0);
	System.out.println(dateFormatFromCalender(today_start,
			DateFormateUtil.FORMAT_FULL_DATE_TIME_WITH_SYMBOL));
	Calendar today_end = Calendar.getInstance(TimeZone.getTimeZone(GMT_8));
	today_end.set(Calendar.HOUR_OF_DAY, 23);
	today_end.set(Calendar.MINUTE, 59);
	today_end.set(Calendar.MONTH, 11);
	today_end.set(Calendar.DAY_OF_MONTH, 31);
	today_end.set(Calendar.SECOND, 59);
	System.out.println(dateFormatFromCalender(today_end,
			DateFormateUtil.FORMAT_FULL_DATE_TIME_WITH_SYMBOL));
	return c.before(today_end) && c.after(today_start);
}
 
Example 5
Project: openjdk-jdk10   File: CalendarRegression.java   View Source Code Vote up 6 votes
/**
 * 4738710: API: Calendar comparison methods should be improved
 */
public void Test4738710() {
    Calendar cal0 = new GregorianCalendar(2003, SEPTEMBER, 30);
    Comparable<Calendar> cal1 = new GregorianCalendar(2003, OCTOBER, 1);
    Calendar cal2 = new GregorianCalendar(2003, OCTOBER, 2);
    if (!(cal1.compareTo(cal0) > 0)) {
        errln("!(cal1 > cal0)");
    }
    if (!(cal1.compareTo(cal2) < 0)) {
        errln("!(cal1 < cal2)");
    }
    if (cal1.compareTo(new GregorianCalendar(2003, OCTOBER, 1)) != 0) {
        errln("cal1 != new GregorianCalendar(2003, OCTOBER, 1)");
    }

    if (cal0.after(cal2)) {
        errln("cal0 shouldn't be after cal2");
    }
    if (cal2.before(cal0)) {
        errln("cal2 shouldn't be before cal0");
    }

    if (cal0.after(0)) {
        errln("cal0.after() returned true with an Integer.");
    }
    if (cal0.before(0)) {
        errln("cal0.before() returned true with an Integer.");
    }
    if (cal0.after(null)) {
        errln("cal0.after() returned true with null.");
    }
    if (cal0.before(null)) {
        errln("cal0.before() returned true with null.");
    }
}
 
Example 6
Project: GoalsAndHabits   File: Habit.java   View Source Code Vote up 6 votes
/**
 * Gets the number of events that have been completed.
 * That is, the number of events corresponding to the schedule that
 * have occurred after the habit start date.
 * Ex: Schedule = Mon,Tue,Wed. Events: Mon,Wed,Fri.
 * Would return 2
 * @return number of completed events.
 */
public int getEventsCompleted(){
    if (events==null || events.isEmpty()) return 0;
    if (schedule==null || schedule.isEmpty()) return 0;
    if (startDate==null) return 0;
    int total = 0;
    Calendar cal = Calendar.getInstance();
    Calendar start = Calendar.getInstance();
    start.setTime(startDate);
    for (HabitEvent event : events){
        Date d = event.getDate();
        if (d==null) continue;
        cal.setTime(d);
        if(cal.after(start) && schedule.contains(cal.get(Calendar.DAY_OF_WEEK))){
            total++;
        }
    }
    return total;
}
 
Example 7
Project: GoalsAndHabits   File: FollowedHabitsArrayAdapter.java   View Source Code Vote up 6 votes
/**
 * Gets the most recent habit event date
 * @param h Habit
 * @return Most recent habit event date
 */
private Calendar getLatestHabitEventDate(Habit h){
    ArrayList<HabitEvent> events = h.getEvents();
    if (events==null) return null;
    if (events.isEmpty()) return null;
    Calendar latest = Calendar.getInstance();
    latest.setTime(events.get(0).getDate());
    if (events.size()==1) return latest;
    Calendar test = Calendar.getInstance();
    Calendar now = Calendar.getInstance();
    for (HabitEvent event : events){
        if (event.getDate()==null) continue;
        test.setTime(event.getDate());
        if (test.after(latest)){
            if (!test.after(now)){
                latest.setTime(test.getTime());
            }
        }
    }
    return latest;
}
 
Example 8
Project: FakeWeather   File: TimeUtils.java   View Source Code Vote up 6 votes
/**
 * 判断当前时间是否在两个时间段之内
 *
 * @param beginTime 开始时间
 * @param endTime   结束时间
 * @return
 */
public static boolean isNowBetween(String beginTime, String endTime) {
    SimpleDateFormat df = new SimpleDateFormat("HH:mm");
    Date now = null;
    Date begin = null;
    Date end = null;
    try {
        now = df.parse(df.format(new Date()));
        begin = df.parse(beginTime);
        end = df.parse(endTime);
    } catch (Exception e) {
        e.printStackTrace();
    }
    Calendar nowCal = Calendar.getInstance();
    nowCal.setTime(now);

    Calendar beginCal = Calendar.getInstance();
    beginCal.setTime(begin);

    Calendar endCal = Calendar.getInstance();
    endCal.setTime(end);

    if (nowCal.after(beginCal) && nowCal.before(endCal)) {
        return true;
    } else {
        return false;
    }
}
 
Example 9
Project: GongXianSheng   File: DateFormateUtil.java   View Source Code Vote up 5 votes
public static boolean befToday(String timeStr) {
	if(TextUtils.isEmpty(timeStr)) {
		return false;
	}
	final Calendar calend = dateFormatFromString(timeStr, DateFormateUtil.FORMAT_FULL_DATE_TIME_WITH_SYMBOL);
	Calendar today = Calendar.getInstance(TimeZone.getTimeZone(GMT_8));
	today.setTime(new Date());
	//差几天
	if (today.after(calend) || isToday(calend)) {
		return true;
	}
	return false;
}
 
Example 10
Project: winter   File: CalendarUtil.java   View Source Code Vote up 5 votes
/**
 * Check if the first calendar is actually dated before the second calendar.
 * @param before The first calendar with expected date before the second calendar.
 * @param after The second calendar with expected date after the first calendar.
 * @throws IllegalArgumentException If the first calendar is dated after the second calendar.
 */
private static void checkBeforeAfter(Calendar before, Calendar after) {
    if (before.after(after)) {
        throw new IllegalArgumentException(
            "The first calendar should be dated before the second calendar.");
    }
}
 
Example 11
Project: CustomFormViews   File: MainPresenterImpl.java   View Source Code Vote up 5 votes
/**
 * Called when a date is selected from DatePicker.
 *
 * @param view Returns the calling view
 * @param date Selected date
 * @return {@code true} if the selected date is valid. {@code false} Otherwise.
 */
@Override
public boolean getDateValidity(CustomDateView view, Calendar date) {
    switch (view.getId()) {
        case R.id.activity_main_date_of_birth:
            Calendar today = Calendar.getInstance();
            today.add(Calendar.YEAR, -18);
            return today.after(date);
    }
    return false;
}
 
Example 12
Project: CXJPadProject   File: TimeUtils.java   View Source Code Vote up 5 votes
/**
 * 时间转化为显示字符串
 *
 * @param timeStamp 单位为秒
 */
public static String formatTime(long timeStamp, boolean simple, Context context) {
    if (timeStamp == 0) return "";

    Calendar input = Calendar.getInstance();
    input.setTimeInMillis(timeStamp);
    String time = (new SimpleDateFormat("HH:mm:ss")).format(input.getTime());
    String date = (new SimpleDateFormat(
            "yyyy" + context.getResources().getString(R.string.time_year)
                    + "MM" + context.getResources().getString(R.string.time_month)
                    + "dd" + context.getResources().getString(R.string.time_day))).format(input.getTime());
    Calendar calendar = Calendar.getInstance();
    //当前时间在输入时间之前
    if (!calendar.after(input)) {
        return date;
    }

    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    if (calendar.before(input)) {
        return String.format("%s %s", context.getResources().getString(R.string.time_today), simple ? "" : time);
    }
    calendar.add(Calendar.DAY_OF_MONTH, -1);
    if (calendar.before(input)) {
        return String.format("%s %s", context.getResources().getString(R.string.time_yesterday), simple ? "" : time);
    }

    calendar.set(Calendar.MONTH, Calendar.JANUARY);
    calendar.set(Calendar.DAY_OF_MONTH, 1);

    if (!simple && calendar.before(input)) {
        date = (new SimpleDateFormat(
                "MM" + context.getResources().getString(R.string.time_month)
                        + "dd" + context.getResources().getString(R.string.time_day))).format(input.getTime());
    }
    return String.format("%s %s", date, simple ? "" : time);
}
 
Example 13
Project: HiBangClient   File: MyDateUtils.java   View Source Code Vote up 5 votes
/**
	 * 格式化时间
	 * @param time
	 * @return
	 */
	public static String formatDateTime(String time) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		if(time==null ||"".equals(time)){
			return "";
		}
		Date date = null;
		try {
			date = format.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		Calendar current = Calendar.getInstance();
		
		Calendar today = Calendar.getInstance();	//今天
		
		today.set(Calendar.YEAR, current.get(Calendar.YEAR));
		today.set(Calendar.MONTH, current.get(Calendar.MONTH));
		today.set(Calendar.DAY_OF_MONTH,current.get(Calendar.DAY_OF_MONTH));
		//  Calendar.HOUR——12小时制的小时数 Calendar.HOUR_OF_DAY——24小时制的小时数
		today.set( Calendar.HOUR_OF_DAY, 0);
		today.set( Calendar.MINUTE, 0);
		today.set(Calendar.SECOND, 0);
		
		Calendar tomorrow = Calendar.getInstance();	//明天
		
		tomorrow.set(Calendar.YEAR, current.get(Calendar.YEAR));
		tomorrow.set(Calendar.MONTH, current.get(Calendar.MONTH));
		tomorrow.set(Calendar.DAY_OF_MONTH,current.get(Calendar.DAY_OF_MONTH)+1);
		tomorrow.set( Calendar.HOUR_OF_DAY, 0);
		tomorrow.set( Calendar.MINUTE, 0);
		tomorrow.set(Calendar.SECOND, 0);
		
        Calendar afterTomorrow = Calendar.getInstance();	//后天
		
        afterTomorrow.set(Calendar.YEAR, current.get(Calendar.YEAR));
        afterTomorrow.set(Calendar.MONTH, current.get(Calendar.MONTH));
        afterTomorrow.set(Calendar.DAY_OF_MONTH,current.get(Calendar.DAY_OF_MONTH)+2);
        afterTomorrow.set( Calendar.HOUR_OF_DAY, 0);
        afterTomorrow.set( Calendar.MINUTE, 0);
        afterTomorrow.set(Calendar.SECOND, 0);
		
        Calendar bigAfterTomorrow = Calendar.getInstance();	//大后天
		
        bigAfterTomorrow.set(Calendar.YEAR, current.get(Calendar.YEAR));
        bigAfterTomorrow.set(Calendar.MONTH, current.get(Calendar.MONTH));
        bigAfterTomorrow.set(Calendar.DAY_OF_MONTH,current.get(Calendar.DAY_OF_MONTH)+3);
        bigAfterTomorrow.set( Calendar.HOUR_OF_DAY, 0);
        bigAfterTomorrow.set( Calendar.MINUTE, 0);
        bigAfterTomorrow.set(Calendar.SECOND, 0);
        
		current.setTime(date);
		
		if(current.after(today) && current.before(tomorrow)){
			return "今天  ";
		}else if(current.before(afterTomorrow) && current.after(tomorrow)){
			return "明天  ";
		}else if(current.before(bigAfterTomorrow) && current.after(afterTomorrow)){
			return "后天  ";
		}else{
//			int index = time.indexOf("-")+1;
//			return time.substring(index, time.length());
			return "";
		}
	}
 
Example 14
Project: GoalsAndHabits   File: Util.java   View Source Code Vote up 5 votes
/**
 * Check if one date is after another (to the day).
 * If the days are the same, return true.
 * @param before the date supposedly int the past
 * @param after the date supposedly after before
 * @return True if 'after' is after 'before', otherwise false.
 */
public static boolean isDateAfter(Date before, Date after){
    Calendar b = Calendar.getInstance();
    b.setTime(before);
    Calendar a = Calendar.getInstance();
    a.setTime(after);
    if (a.get(Calendar.YEAR) == b.get(Calendar.YEAR) && a.get(Calendar.DAY_OF_YEAR) == b.get(Calendar.DAY_OF_YEAR)) return false;
    return a.after(b);
}
 
Example 15
Project: TaipeiTechRefined   File: ActivityList.java   View Source Code Vote up 5 votes
public void checkActivity() {
    Calendar calendar = Calendar.getInstance();
    ActivityList clone = (ActivityList) clone();
    for (ActivityInfo info : clone) {
        Calendar activity_end = Calendar.getInstance();
        activity_end.setTime(info.getEndDate());
        activity_end.add(Calendar.DAY_OF_MONTH, 1);
        if (calendar.after(activity_end)) {
            remove(info);
        }
    }
}
 
Example 16
Project: schulcloud-mobile-android   File: AddHomeworkPresenter.java   View Source Code Vote up 5 votes
public void addHomework(@NonNull String name, int courseIndex, boolean isPrivate,
        @NonNull String description, @NonNull Calendar availableDate, @NonNull Calendar dueDate,
        boolean publicSubmissions) {
    if (name.isEmpty())
        getViewOrThrow().showNameEmpty();
    else if (!dueDate.after(availableDate))
        getViewOrThrow().showInvalidDates();
    else {
        Course course = mCourses.get(courseIndex);
        AddHomeworkRequest addHomeworkRequest = new AddHomeworkRequest(
                mPreferencesHelper.getCurrentSchoolId(),
                mPreferencesHelper.getCurrentUserId(),
                name,
                course == null ? null : course._id,
                description,
                mDateFormat.format(availableDate.getTime()),
                mDateFormat.format(dueDate.getTime()),
                publicSubmissions,
                isPrivate);

        RxUtil.unsubscribe(mSubscription);
        mSubscription = mDataManager.addHomework(addHomeworkRequest)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        addHomeworkResponse -> sendToView(
                                AddHomeworkMvpView::reloadHomeworkList),
                        throwable -> sendToView(AddHomeworkMvpView::showSaveError),
                        () -> sendToView(AddHomeworkMvpView::showHomeworkSaved));
    }
}
 
Example 17
Project: HtmlNative   File: OsLib.java   View Source Code Vote up 5 votes
private int weekNumber(Calendar d, int startDay) {
	Calendar y0 =  beginningOfYear(d);
	y0.set(Calendar.DAY_OF_MONTH, 1 + (startDay + 8 - y0.get(Calendar.DAY_OF_WEEK)) % 7);
	if (y0.after(d)) {
		y0.set(Calendar.YEAR, y0.get(Calendar.YEAR) - 1);
		y0.set(Calendar.DAY_OF_MONTH, 1 + (startDay + 8 - y0.get(Calendar.DAY_OF_WEEK)) % 7);
	}
	long dt = d.getTime().getTime() - y0.getTime().getTime();
	return 1 + (int) (dt / (7L * 24L * 3600L * 1000L));
}
 
Example 18
Project: lorne_core   File: DateUtil.java   View Source Code Vote up 4 votes
public static boolean betweenMinute(Date date, int minute) {
    Calendar calendar = getCalendarFromDate(date);
    calendar.add(Calendar.MINUTE, minute);
    Calendar now = getDefaultCalendar();
    return calendar.after(now);
}
 
Example 19
Project: open-java-trade-manager   File: Candle.java   View Source Code Vote up 4 votes
public static boolean isComplete(Candle c) 
{
	// ist die Candle beendet?
	Calendar tmp = new GregorianCalendar();
	return tmp.after(c.getEndTime());
}
 
Example 20
Project: Hotel-Properties-Management-System   File: Walkin_CheckinWindow.java   View Source Code Vote up 4 votes
private PropertyChangeListener chechkDates() {
	final PropertyChangeListener listener = new PropertyChangeListener() {

		@Override
		public void propertyChange(PropertyChangeEvent evt) {
			
			// here leave ancestors empty because we need dates not ancestor
			if (evt.getPropertyName().equals("ancestor")) {
				return;
			}
			
			boolean showed = false;
			startDate = checkinDateChooser.getDate();
			endDate = checkoutDateChooser.getDate();

			if (startDate != null && endDate != null) {
				// add to calendar to be able get day of date
				// and compare
				Calendar cs = Calendar.getInstance();
				cs.setTime(startDate);
				Calendar ce = Calendar.getInstance();
				ce.setTime(endDate);

				// compare if start date greater than end date
				if (cs.after(ce) && !showed) {
					JOptionPane.showMessageDialog(null, "Start date is after end date!",
							JOptionPane.MESSAGE_PROPERTY, JOptionPane.WARNING_MESSAGE);
					showed = true;
				}
				// or both is same date
				else if (cs.get(Calendar.DAY_OF_YEAR) == ce.get(Calendar.DAY_OF_YEAR) && !showed) {
					JOptionPane.showMessageDialog(null,
							"Start date equals end date!\nPlease be sure you're choose right date.",
							JOptionPane.MESSAGE_PROPERTY, JOptionPane.WARNING_MESSAGE);
					showed = true;
				}
				// other odds
				else {
					value = (int) ((startDate.getTime() - endDate.getTime()) / (1000 * 60 * 60 * 24));
					totalDaysField.setText(Math.abs(value) + "");
					totalDaysField.revalidate();
					totalDaysField.repaint();
				}

			}
		}
	};
	return listener;
}