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

The following examples show how to use java.util.GregorianCalendar#getActualMaximum() . 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: DateDifFunction.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
private long computeYearDays( final GregorianCalendar min, final GregorianCalendar max, final long dayDiff ) {
  final int year1 = min.get( Calendar.YEAR );
  final int year2 = max.get( Calendar.YEAR );
  if ( year1 == year2 ) {
    // simple case: We are within the same year
    return Math.abs( dayDiff );
  }

  final int dayMinDate = min.get( Calendar.DAY_OF_YEAR );
  final int dayMaxDate = max.get( Calendar.DAY_OF_YEAR );
  if ( dayMinDate <= dayMaxDate ) {
    return dayMaxDate - dayMinDate;
  }

  int daysInMinYear = min.getActualMaximum( Calendar.DAY_OF_YEAR );
  int daysToEndOfYear = daysInMinYear - dayMinDate;
  return dayMaxDate + daysToEndOfYear;
}
 
Example 4
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 5
Source File: CalendarRegression.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * GregorianCalendar.getActualMaximum() does not account for first day of week.
 */
public void Test4166109() {
    /* Test month:
    *
    *      March 1998
    * Su Mo Tu We Th Fr Sa
    *  1  2  3  4  5  6  7
    *  8  9 10 11 12 13 14
    * 15 16 17 18 19 20 21
    * 22 23 24 25 26 27 28
    * 29 30 31
     */
    boolean passed = true;
    int field = WEEK_OF_MONTH;

    GregorianCalendar calendar = new GregorianCalendar(Locale.US);
    calendar.set(1998, MARCH, 1);
    calendar.setMinimalDaysInFirstWeek(1);
    logln("Date:  " + calendar.getTime());

    int firstInMonth = calendar.get(DAY_OF_MONTH);

    for (int firstInWeek = SUNDAY; firstInWeek <= SATURDAY; firstInWeek++) {
        calendar.setFirstDayOfWeek(firstInWeek);
        int returned = calendar.getActualMaximum(field);
        int expected = (31 + ((firstInMonth - firstInWeek + 7) % 7) + 6) / 7;

        logln("First day of week = " + firstInWeek
                + "  getActualMaximum(WEEK_OF_MONTH) = " + returned
                + "  expected = " + expected
                + ((returned == expected) ? "  ok" : "  FAIL"));

        if (returned != expected) {
            passed = false;
        }
    }
    if (!passed) {
        errln("Test failed");
    }
}
 
Example 6
Source File: DateUtils.java    From tianti with Apache License 2.0 5 votes vote down vote up
/**
 * 月最后一天
 * @param date
 * @return
 */
public static Date getMonthEndDate(Date date){
	GregorianCalendar c = new GregorianCalendar();
	c.setTime(date);
	int maxMonth = c.getActualMaximum(Calendar.DAY_OF_MONTH);
	c.set(Calendar.DAY_OF_MONTH, maxMonth);
	c.set(Calendar.HOUR_OF_DAY, 23);
	c.set(Calendar.MINUTE, 59);
	c.set(Calendar.SECOND, 59);
	return c.getTime();
}
 
Example 7
Source File: DateDifFunction.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
private long computeMonthDays( final GregorianCalendar min, final GregorianCalendar max ) {
  // The number of days between Date1 and Date2, as if Date1 and
  // Date2 were in the same month and the same year.
  int dayMin = min.get( Calendar.DAY_OF_MONTH );
  int dayMax = max.get( Calendar.DAY_OF_MONTH );
  if ( dayMin <= dayMax ) {
    return dayMax - dayMin;
  }

  int maxDaysInMonth = max.getActualMaximum( Calendar.DAY_OF_MONTH );
  return maxDaysInMonth + dayMax - dayMin;
}
 
Example 8
Source File: CalendarRegression.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Calendar.getActualMaximum(YEAR) works wrong.
 *
 * Note: Before 1.5, this test case assumed that
 * setGregorianChange didn't change object's date. But it was
 * changed. See 4928615.
 */
public void Test4167060() {
    int field = YEAR;
    DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy G",
            Locale.US);

    int[][] dates = {
        // year, month, day of month
        {100, NOVEMBER, 1},
        {-99 /*100BC*/, JANUARY, 1},
        {1996, FEBRUARY, 29}};

    String[] id = {"Hybrid", "Gregorian", "Julian"};

    for (int k = 0; k < 3; ++k) {
        logln("--- " + id[k] + " ---");

        for (int j = 0; j < dates.length; ++j) {
            GregorianCalendar calendar = new GregorianCalendar();
            if (k == 1) {
                calendar.setGregorianChange(new Date(Long.MIN_VALUE));
            } else if (k == 2) {
                calendar.setGregorianChange(new Date(Long.MAX_VALUE));
            }
            calendar.set(dates[j][0], dates[j][1], dates[j][2]);
            format.setCalendar((Calendar) calendar.clone());

            Date dateBefore = calendar.getTime();

            int maxYear = calendar.getActualMaximum(field);
            logln("maxYear: " + maxYear + " for " + format.format(calendar.getTime()));
            logln("date before: " + format.format(dateBefore));

            int[] years = {2000, maxYear - 1, maxYear, maxYear + 1};

            for (int i = 0; i < years.length; i++) {
                boolean valid = years[i] <= maxYear;
                calendar.set(field, years[i]);
                Date dateAfter = calendar.getTime();
                int newYear = calendar.get(field);
                calendar.setTime(dateBefore); // restore calendar for next use

                logln(" Year " + years[i] + (valid ? " ok " : " bad")
                        + " => " + format.format(dateAfter));
                if (valid && newYear != years[i]) {
                    errln("  FAIL: " + newYear + " should be valid; date, month and time shouldn't change");
                } else if (!valid && newYear == years[i]) {
                    errln("  FAIL: " + newYear + " should be invalid");
                }
            }
        }
    }
}
 
Example 9
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();
    }
 
Example 10
Source File: CalendarProgram.java    From mars-sim with GNU General Public License v3.0 4 votes vote down vote up
public static void refreshCalendar(int month, int year){
		//Variables
		String[] months =  {"Adir", "Bora", "Coan", "Detri",
		        "Edal", "Flo", "Geor", "Heliba", "Idanon", "Jowani", "Kireal", "Larno",
		        "Medior", "Neturima", "Ozulikan", "Pasurabi", "Rudiakel", "Safundo", "Tiunor",
		        "Ulasja", "Vadeun", "Wakumi", "Xetual", "Zungo" };
		int nod, som; //Number Of Days, Start Of Month

		//Allow/disallow buttons
		btnPrev.setEnabled(true);
		btnNext.setEnabled(true);
		if (month == 0 && year <= realYear-10){btnPrev.setEnabled(false);} //Too early
		if (month == 23 && year >= realYear+100){btnNext.setEnabled(false);} //Too late
		lblMonth.setText(months[month]); //Refresh the month label (at the top)
		lblMonth.setBounds(WIDTH/2-lblMonth.getPreferredSize().width/2, 25, 180, 25); //Re-align label with calendar
		cmbYear.setSelectedItem(String.valueOf(year)); //Select the correct year in the combo box

		//Clear table
		for (int i=0; i<6; i++){
			for (int j=0; j<7; j++){
				mtblCalendar.setValueAt(null, i, j);
			}
		}

		//Get first day of month and number of days
		GregorianCalendar cal = new GregorianCalendar(year, month, 1);
		nod = cal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
		som = cal.get(GregorianCalendar.DAY_OF_WEEK);

		//Draw calendar
		for (int i=1; i<=nod; i++){
			int row = Integer.valueOf((i+som-2)/7);
			int column  =  (i+som-2)%7;
			mtblCalendar.setValueAt(i, row, column);
		}

		//Apply renderers
		tblCalendar.setDefaultRenderer(tblCalendar.getColumnClass(0), new tblCalendarRenderer());
		
//		tblCalendar.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);//.AUTO_RESIZE_LAST_COLUMN);
		
	}
 
Example 11
Source File: DaysFunction.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
public TypeValuePair evaluate( final FormulaContext context, final ParameterCallback parameters )
  throws EvaluationException {
  if ( parameters.getParameterCount() != 2 ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE );
  }

  final TypeRegistry typeRegistry = context.getTypeRegistry();
  final Date date1 = typeRegistry.convertToDate
    ( parameters.getType( 0 ), parameters.getValue( 0 ) );
  final Date date2 = typeRegistry.convertToDate
    ( parameters.getType( 1 ), parameters.getValue( 1 ) );

  if ( date1 == null || date2 == null ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }

  final LocalizationContext localizationContext = context.getLocalizationContext();
  final TimeZone timeZone = localizationContext.getTimeZone();
  final Locale locale = localizationContext.getLocale();
  final GregorianCalendar calandar1 =
    new GregorianCalendar( timeZone, locale );
  calandar1.setTime( date1 );

  final GregorianCalendar calandar2 =
    new GregorianCalendar( timeZone, locale );
  calandar2.setTime( date2 );

  final int dayOfYear1 = calandar1.get( Calendar.DAY_OF_YEAR );
  final int dayOfYear2 = calandar2.get( Calendar.DAY_OF_YEAR );
  final int year1 = calandar1.get( Calendar.YEAR );
  final int year2 = calandar2.get( Calendar.YEAR );

  final GregorianCalendar workingCalandar =
    new GregorianCalendar( timeZone, locale );

  int res = dayOfYear2 - dayOfYear1;

  // run through the inner years, without counting the border years
  // Always run from the lower to the higher, so that we prevent infinite
  // loops ..
  final int targetYear = Math.max( year1, year2 );
  for ( int i = Math.min( year1, year2 ); i < targetYear; i++ ) {
    workingCalandar.set( Calendar.YEAR, i );
    res += workingCalandar.getActualMaximum( Calendar.DAY_OF_YEAR );
  }

  //noinspection UnpredictableBigDecimalConstructorCall
  return new TypeValuePair( NumberType.GENERIC_NUMBER, new BigDecimal( (double) res ) );
}