Java Code Examples for com.ibm.icu.util.Calendar#after()

The following examples show how to use com.ibm.icu.util.Calendar#after() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BirtDateTime.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Return difference in number of days
 *
 * @param d1
 * @param d2
 * @return
 */
private static long diffDay( Date d1, Date d2 )
{
	Calendar c1 = Calendar.getInstance( threadTimeZone.get( ) );
	c1.setTime( d1 );
	Calendar c2 = Calendar.getInstance( threadTimeZone.get( ) );
	c2.setTime( d2 );
	if ( c1.after( c2 ) )
	{
		return -diffDay( c2, c1 ) ;
	}
	else
	{
		return diffDay( c1, c2 );
	}
}
 
Example 2
Source File: TimeDuration.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
private static int advanceCalendar(Calendar start, Calendar end, int units, int type) {
    if (units >= 1) {
        // Bother, the below needs explanation.
        //
        // If start has a day value of 31, and you add to the month,
        // and the target month is not allowed to have 31 as the day
        // value, then the day will be changed to a value that is in
        // range.  But, when the code needs to then subtract 1 from
        // the month, because it has advanced to far, the day is *not*
        // set back to the original value of 31.
        //
        // This bug can be triggered by having a duration of -1 day,
        // then adding this duration to a calendar that represents 0
        // milliseconds, then creating a new duration by using the 2
        // Calendar constructor, with cal1 being 0, and cal2 being the
        // new calendar that you added the duration to.
        //
        // To solve this problem, we make a temporary copy of the
        // start calendar, and only modify it if we actually have to.
        Calendar tmp = (Calendar) start.clone();
        int tmpUnits = units;
        tmp.add(type, tmpUnits);
        while (tmp.after(end)) {
            tmp.add(type, -1);
            units--;
        }
        if (units != 0) {
            start.add(type, units);
        }
    }
    return units;
}
 
Example 3
Source File: TemporalExpressions.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
protected Calendar prepareCal(Calendar cal) {
    // Performs a "sane" skip forward in time - avoids time consuming loops
    // like incrementing every second from Jan 1 2000 until today
    Calendar skip = (Calendar) cal.clone();
    skip.setTime(this.start);
    long deltaMillis = cal.getTimeInMillis() - this.start.getTime();
    if (deltaMillis < 1000) {
        return skip;
    }
    long divisor = deltaMillis;
    if (this.freqType == Calendar.DAY_OF_MONTH) {
        divisor = 86400000;
    } else if (this.freqType == Calendar.HOUR) {
        divisor = 3600000;
    } else if (this.freqType == Calendar.MINUTE) {
        divisor = 60000;
    } else if (this.freqType == Calendar.SECOND) {
        divisor = 1000;
    } else {
        return skip;
    }
    long units = deltaMillis / divisor;
    units -= units % this.freqCount;
    skip.add(this.freqType, (int)units);
    while (skip.after(cal)) {
        skip.add(this.freqType, -this.freqCount);
    }
    return skip;
}
 
Example 4
Source File: DateRange.java    From org.openntf.domino with Apache License 2.0 5 votes vote down vote up
@Override
public boolean contains(final org.openntf.domino.DateTime dt) {
	Calendar dtCal = dt.toJavaCal();
	Calendar startCal = dt.toJavaCal();
	Calendar endCal = dt.toJavaCal();
	if (dt.isAnyDate()) {
		// Compare times only -normalize dates
		dtCal.set(Calendar.YEAR, 2013);
		dtCal.set(Calendar.MONTH, 0);
		dtCal.set(Calendar.DAY_OF_MONTH, 2);

		startCal.set(Calendar.YEAR, 2013);
		startCal.set(Calendar.MONTH, 0);
		startCal.set(Calendar.DAY_OF_MONTH, 2);

		endCal.set(Calendar.YEAR, 2013);
		endCal.set(Calendar.MONTH, 0);
		endCal.set(Calendar.DAY_OF_MONTH, 2);
	} else if (dt.isAnyTime()) {
		// Compare dates only - normalize times
		dtCal.set(Calendar.HOUR_OF_DAY, 12);
		dtCal.set(Calendar.MINUTE, 0);
		dtCal.set(Calendar.SECOND, 0);
		dtCal.set(Calendar.MILLISECOND, 0);

		startCal.set(Calendar.HOUR_OF_DAY, 12);
		startCal.set(Calendar.MINUTE, 0);
		startCal.set(Calendar.SECOND, 0);
		startCal.set(Calendar.MILLISECOND, 0);

		endCal.set(Calendar.HOUR_OF_DAY, 12);
		endCal.set(Calendar.MINUTE, 0);
		endCal.set(Calendar.SECOND, 0);
		endCal.set(Calendar.MILLISECOND, 0);
	}
	return (dtCal.equals(startCal) || dtCal.after(startCal)) && (dtCal.equals(endCal) || dtCal.before(endCal));
}
 
Example 5
Source File: TrailingFunction.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Get timeMember/s for current date, then make a move 
 * @param curDate
 * @param startDate
 * @param endDate
 * @param unit
 * @param step
 * @return
 */
private List<Calendar> moveAStep( Calendar curDate, Calendar endDate, String unit, String extraWeekLevel )
{
	List<Calendar> times = new ArrayList<Calendar>( );
	int step = isMoveForward ? 1 : -1 ;
	
	times.add( ( Calendar ) curDate.clone( ) );
	
	if ( unit.equals( YEAR ) )
	{
		if ( isMoveForward )
			setToYearStart( curDate );
		else
			setToYearEnd( curDate );
		curDate.add( Calendar.YEAR, step );
	}
	else if ( unit.equals( QUARTER ) )
	{
		if ( isMoveForward )
			setToQuarterStart( curDate );
		else
			setToQuarterEnd( curDate );
		curDate.add( Calendar.MONTH, step * 3 );
	}
	else if ( unit.equals( MONTH ) )
	{
		if ( isMoveForward )
			setToMonthStart( curDate );
		else
			setToMonthEnd( curDate );
		curDate.add( Calendar.MONTH, step );
	}
	else if ( unit.equals( WEEK ) )
	{
		Calendar tmpDate = ( Calendar ) curDate.clone();
		if ( isMoveForward )
		{
			if ( extraWeekLevel != null )
			{
				tmpDate.set( Calendar.DAY_OF_WEEK, 7 );
				if ( tmpDate.after( endDate ) )
					tmpDate = endDate;
				tmpDate = getExtraWeek( tmpDate, curDate, extraWeekLevel);
				if ( tmpDate != null )
					times.add( tmpDate );
			}
			setToWeekStart( curDate );
			curDate.set(Calendar.DAY_OF_WEEK, 1);
		}
		else
		{
			if ( extraWeekLevel != null )
			{
				tmpDate.set( Calendar.DAY_OF_WEEK, 1 );
				if ( tmpDate.before( endDate ) )
					tmpDate = endDate;
				tmpDate = getExtraWeek( tmpDate, curDate, extraWeekLevel);
				if ( tmpDate != null )
					times.add( tmpDate );
			}
			setToWeekEnd( curDate );
			curDate.set(Calendar.DAY_OF_WEEK, 7);
		}
		curDate.add( Calendar.WEEK_OF_YEAR, step );
	}
	else if ( unit.equals( DAY ) )
	{
		curDate.add( Calendar.DATE, step );
	}
	
	return times;
}