Java Code Examples for java.util.GregorianCalendar#getActualMinimum()

The following examples show how to use java.util.GregorianCalendar#getActualMinimum() . 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: Schedule.java    From fosstrak-epcis with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Sets the specified field of the given callendar to the next scheduled
 * value. Returns whether the new value has been set and is valid.
 * 
 * @param cal
 *            Calendar to adjust.
 * @param field
 *            Field to adjust.
 * @return Returns whether the new value has been set and is valid.
 * @throws ImplementationException
 *             Almost any error.
 */
private boolean setToNextScheduledValue(final GregorianCalendar cal, final int field)
        throws ImplementationExceptionResponse {
    int next;
    TreeSet<Integer> vals = getValues(field);
    if (vals.isEmpty()) {
        next = cal.get(field) + 1;
    } else {
        try {
            // get next scheduled value which is bigger than current
            int incrValue = cal.get(field) + 1;
            next = vals.tailSet(new Integer(incrValue)).first().intValue();
        } catch (NoSuchElementException nse) {
            // there is no bigger scheduled value
            return false;
        }
    }
    if (next > cal.getActualMaximum(field) || next < cal.getActualMinimum(field)) {
        return false;
    }
    // all is well, set it to next
    cal.set(field, next);
    return true;
}
 
Example 2
Source File: Schedule.java    From fosstrak-epcis with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Sets the field of a GregorianCalender to its minimum, which is defined as
 * the minimal possible value according to the calendar type possibly
 * superseded by the defined values in the schedule we have. Returns whether
 * the new value has been set and is valid.
 * 
 * @param cal
 *            Calendar to adjust.
 * @param field
 *            Field to adjust.
 * @return Returns whether the new value has been set and is valid.
 * @throws ImplementationException
 *             Almost any error.
 */
private boolean setFieldToMinimum(final GregorianCalendar cal, final int field)
        throws ImplementationExceptionResponse {
    int min;
    TreeSet<Integer> values = getValues(field);
    if (values.isEmpty()) {
        min = cal.getActualMinimum(field);
    } else {
        min = Math.max(values.first().intValue(), cal.getActualMinimum(field));
        if (min > cal.getActualMaximum(field)) {
            min = cal.getActualMaximum(field);
            if (!values.contains(Integer.valueOf(min)) || min < cal.getActualMinimum(field)
                    || min > cal.getActualMaximum(field)) {
                return false;
            }
        }
    }
    cal.set(field, min);
    return true;
}
 
Example 3
Source File: Anonymizer.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
private Date generateRandomDate( final Date value ) {
  final GregorianCalendar c = new GregorianCalendar();
  c.setTime( value );
  final int min = c.getActualMinimum( GregorianCalendar.DAY_OF_YEAR );
  final int max = c.getActualMaximum( GregorianCalendar.DAY_OF_YEAR );
  c.set( GregorianCalendar.DAY_OF_YEAR, r.nextInt( max - min ) + min );
  if ( value instanceof java.sql.Date ) {
    return c.getTime();
  }

  c.set( GregorianCalendar.HOUR_OF_DAY, 0 );
  c.set( GregorianCalendar.MINUTE, 0 );
  c.set( GregorianCalendar.SECOND, 0 );
  c.set( GregorianCalendar.MILLISECOND, 0 );
  final long time = c.getTime().getTime();
  return new Date( time + r.nextInt( 24 * 60 * 60 * 1000 ) );
}
 
Example 4
Source File: BaseDateType.java    From json-data-generator with Apache License 2.0 4 votes vote down vote up
public Date getRandomDate() {
        GregorianCalendar gc = new GregorianCalendar();
        GregorianCalendar minCal = new GregorianCalendar();
        minCal.setTime(min);
        GregorianCalendar maxCal = new GregorianCalendar();
        maxCal.setTime(max);

        int year = getRand().nextInt(minCal.get(GregorianCalendar.YEAR), maxCal.get(GregorianCalendar.YEAR));
        gc.set(GregorianCalendar.YEAR, year);

        int month = -1;
        if (minCal.get(GregorianCalendar.YEAR) == maxCal.get(GregorianCalendar.YEAR)) {
            month = getRand().nextInt(minCal.get(GregorianCalendar.MONTH), maxCal.get(GregorianCalendar.MONTH));
        } else if (year == minCal.get(GregorianCalendar.YEAR)) {
            month = getRand().nextInt(minCal.get(GregorianCalendar.MONTH), gc.getActualMaximum(GregorianCalendar.MONTH));
        } else if (year == maxCal.get(GregorianCalendar.YEAR)) {
            month = getRand().nextInt(gc.getActualMinimum(GregorianCalendar.MONTH), maxCal.get(GregorianCalendar.MONTH));
        } else {
            month = getRand().nextInt(gc.getActualMinimum(GregorianCalendar.MONTH), gc.getActualMaximum(GregorianCalendar.MONTH));
        }
        gc.set(GregorianCalendar.MONTH, month);

        int day = -1;
        if (minCal.get(GregorianCalendar.YEAR) == maxCal.get(GregorianCalendar.YEAR) && minCal.get(GregorianCalendar.MONTH) == maxCal.get(GregorianCalendar.MONTH)) {
            day = getRand().nextInt(minCal.get(GregorianCalendar.DAY_OF_MONTH), maxCal.get(GregorianCalendar.DAY_OF_MONTH));
        } else if (year == minCal.get(GregorianCalendar.YEAR) && month == minCal.get(GregorianCalendar.MONTH)) {
            day = getRand().nextInt(minCal.get(GregorianCalendar.DAY_OF_MONTH), gc.getActualMaximum(GregorianCalendar.DAY_OF_MONTH));
        } else if (year == maxCal.get(GregorianCalendar.YEAR) && month == maxCal.get(GregorianCalendar.MONTH)) {
            day = getRand().nextInt(gc.getActualMinimum(GregorianCalendar.DAY_OF_MONTH), maxCal.get(GregorianCalendar.DAY_OF_MONTH));
        } else {
            day = getRand().nextInt(1, gc.getActualMaximum(GregorianCalendar.DAY_OF_MONTH));
        }
        gc.set(GregorianCalendar.DAY_OF_MONTH, day);

        //generate a random time too
        int minHour = gc.getActualMinimum(GregorianCalendar.HOUR_OF_DAY);
        int minMin = gc.getActualMinimum(GregorianCalendar.MINUTE);
        int minSec = gc.getActualMinimum(GregorianCalendar.SECOND);
        int maxHour = gc.getActualMaximum(GregorianCalendar.HOUR_OF_DAY);
        int maxMin = gc.getActualMaximum(GregorianCalendar.MINUTE);
        int maxSec = gc.getActualMaximum(GregorianCalendar.SECOND);

        if (minCal.get(GregorianCalendar.YEAR) == gc.get(GregorianCalendar.YEAR) && minCal.get(GregorianCalendar.MONTH) == gc.get(GregorianCalendar.MONTH) && minCal.get(GregorianCalendar.DAY_OF_MONTH) == gc.get(GregorianCalendar.DAY_OF_MONTH)) {
            //same day as min.  Must be after the min hour, min, sec
            minHour = minCal.get(GregorianCalendar.HOUR_OF_DAY);
        }
        if (maxCal.get(GregorianCalendar.YEAR) == gc.get(GregorianCalendar.YEAR) && maxCal.get(GregorianCalendar.MONTH) == gc.get(GregorianCalendar.MONTH) && maxCal.get(GregorianCalendar.DAY_OF_MONTH) == gc.get(GregorianCalendar.DAY_OF_MONTH)) {
            //same day as max. Must be before max hour, min, sec
            maxHour = maxCal.get(GregorianCalendar.HOUR_OF_DAY);
        }
//        else {
//            //different day than either min or max. Time doesn't matter. 
//        }

        int hour = getRand().nextInt(minHour, maxHour);
        gc.set(GregorianCalendar.HOUR_OF_DAY, hour);
        if (minHour == maxHour) {
            minMin = minCal.get(GregorianCalendar.MINUTE);
            maxMin = maxCal.get(GregorianCalendar.MINUTE);
        } else if (hour == minHour) {
            minMin = minCal.get(GregorianCalendar.MINUTE);
        } else if (hour == maxHour) {
            maxMin = maxCal.get(GregorianCalendar.MINUTE);
        }
        
        int minute = getRand().nextInt(minMin, maxMin);
        gc.set(GregorianCalendar.MINUTE, minute);
        
        if (minHour == maxHour && minMin == maxMin) {
            minSec = minCal.get(GregorianCalendar.SECOND);
            maxSec = maxCal.get(GregorianCalendar.SECOND);
        } else if (hour == minHour && minute == minMin) {
            minSec = minCal.get(GregorianCalendar.SECOND);
        } else if (hour == maxHour && minute == maxMin) {
            maxSec = maxCal.get(GregorianCalendar.SECOND);
        }
        int sec = getRand().nextInt(minSec, maxSec);
        gc.set(GregorianCalendar.SECOND, sec);

        //clear MS because we don't care about that much precision
        gc.set(GregorianCalendar.MILLISECOND, 0);

        return gc.getTime();
    }