Example usage for java.util Calendar getActualMaximum

List of usage examples for java.util Calendar getActualMaximum

Introduction

In this page you can find the example usage for java.util Calendar getActualMaximum.

Prototype

public int getActualMaximum(int field) 

Source Link

Document

Returns the maximum value that the specified calendar field could have, given the time value of this Calendar.

Usage

From source file:org.jfree.data.time.Week.java

/**
 * Returns the week preceding this one.  This method will return
 * <code>null</code> for some lower limit on the range of weeks (currently
 * week 1, 1900).  For week 1 of any year, the previous week is always week
 * 53, but week 53 may not contain any days (you should check for this).
 *
 * @return The preceding week (possibly <code>null</code>).
 *//*from  ww w. ja  v a  2s  .c o  m*/
@Override
public RegularTimePeriod previous() {

    Week result;
    if (this.week != FIRST_WEEK_IN_YEAR) {
        result = new Week(this.week - 1, this.year);
    } else {
        // we need to work out if the previous year has 52 or 53 weeks...
        if (this.year > 1900) {
            int yy = this.year - 1;
            Calendar prevYearCalendar = Calendar.getInstance();
            prevYearCalendar.set(yy, Calendar.DECEMBER, 31);
            result = new Week(prevYearCalendar.getActualMaximum(Calendar.WEEK_OF_YEAR), yy);
        } else {
            result = null;
        }
    }
    return result;

}

From source file:org.openhab.binding.cardio2e.internal.code.Cardio2eDateTime.java

private boolean isValidCardio2eDateTime(String dateTime) { // Verifies if
    // dateTime//from  w ww  .j  av  a2 s .  co  m
    // string is
    // valid for
    // Cardio2e
    boolean isValid = false;
    int dateTimeIntArray[] = cardio2eDateTimeToInt(dateTime);
    int year, month, day, hour, minute;
    Calendar myCalendar;
    year = dateTimeIntArray[0];
    month = dateTimeIntArray[1];
    day = dateTimeIntArray[2];
    hour = dateTimeIntArray[3];
    minute = dateTimeIntArray[4];
    if ((year >= CARDIO2E_DATE_MIN_YEAR) && (year <= CARDIO2E_DATE_MAX_YEAR) && (month >= 1) && (month <= 12)
            && (day >= 1) && (day <= 31) && (hour >= 0) && (hour <= 23) && (minute >= 0) && (minute <= 59)) {
        // Checks day of the month
        try {
            myCalendar = new GregorianCalendar(year, (month - 1), 1);
            if (day <= myCalendar.getActualMaximum(Calendar.DAY_OF_MONTH)) {
                isValid = true;
            }
        } catch (Exception ex) {
            isValid = false;
        }
    }
    return isValid;
}

From source file:com.clustercontrol.calendar.factory.SelectCalendar.java

/**
 * ??????/*from w  w w  .  j a  v a2  s  . co m*/
 * @param info
 * @param year
 * @param month
 * @return
 */
public ArrayList<Integer> getCalendarMonth(CalendarInfo info, Integer year, Integer month) {
    ArrayList<Integer> ret = new ArrayList<Integer>();
    ArrayList<CalendarDetailInfo> list24 = new ArrayList<CalendarDetailInfo>();
    for (CalendarDetailInfo d : info.getCalendarDetailList()) {
        list24.addAll(CalendarUtil.getDetail24(d));
    }

    long validFrom = info.getValidTimeFrom();
    long validTo = info.getValidTimeTo();

    Calendar cal = HinemosTime.getCalendarInstance();
    cal.set(year, month - 1, 1);
    int lastDate = cal.getActualMaximum(Calendar.DATE) + 1;
    m_log.debug("maxDate=" + year + "/" + month + "/" + lastDate);
    for (int i = 1; i < lastDate; i++) {
        Calendar startCalendar = HinemosTime.getCalendarInstance();
        startCalendar.clear();
        startCalendar.set(year, month - 1, i, 0, 0, 0);
        long dayStartTime = startCalendar.getTimeInMillis();
        Calendar endCalendar = HinemosTime.getCalendarInstance();
        endCalendar.clear();
        endCalendar.set(year, month - 1, i + 1, 0, 0, 0);
        long dayEndTime = endCalendar.getTimeInMillis();
        m_log.debug("i=" + i + " ==== start=" + new Date(dayStartTime) + ",end=" + new Date(dayEndTime));

        // 1????????
        // ???true??????????
        boolean isContainInvalidPeriod = false;
        // ????????
        long dayValidStart;
        if (dayStartTime < validFrom) {
            dayValidStart = validFrom;
            isContainInvalidPeriod = true;
        } else {
            dayValidStart = dayStartTime;
        }
        long dayValidEnd;
        if (validTo < dayEndTime) {
            dayValidEnd = validTo;
            isContainInvalidPeriod = true;
        } else {
            dayValidEnd = dayEndTime;
        }
        // ????????????
        dayValidEnd--;

        // ?????????????????
        if (dayValidStart > dayValidEnd) {
            ret.add(2);
            continue;
        }

        /**
         * ??????
         * ??????????????
         * ?CalendarDetailInfo?From?To???????
         */
        Set<Long> borderTimeSet = new HashSet<Long>();
        borderTimeSet.add(dayValidStart);
        borderTimeSet.add(dayValidEnd);
        // detail
        for (CalendarDetailInfo detail : list24) {
            long detailStart = dayStartTime + detail.getTimeFrom() + TIMEZONE;
            if (dayValidStart < detailStart && detailStart < dayValidEnd) {
                borderTimeSet.add(detailStart);
            }

            long detailEnd = dayStartTime + detail.getTimeTo() + TIMEZONE;
            if (dayValidStart < detailEnd && detailEnd < dayValidEnd) {
                borderTimeSet.add(detailEnd);
            }
        }

        /**
         * ?????
         * [0]???????OK
         * [2]???????NG
         * [1]?OK??NG
         * ??
         */
        boolean isAllNG = true; // OK????false??
        boolean isAllOK = true; // NG????false??
        for (Long borderTime : borderTimeSet) {
            // ????????
            // ?????????
            m_log.debug("date:" + new Date(borderTime));
            boolean retRun = CalendarUtil.isRun(info, new Date(borderTime));
            if (retRun) {
                isAllNG = false;
            } else {
                isAllOK = false;
            }
            // OKNG???????????????????
            if (isAllNG == false && isAllOK == false) {
                break;
            }
        }

        if (isAllNG == true) {
            // NG
            ret.add(2);
        } else {
            if (isAllOK == true) {
                if (isContainInvalidPeriod) {
                    // OKNG ??OK????????????
                    ret.add(1);
                } else {
                    // ?OK
                    ret.add(0);
                }
            } else {
                // OKNG
                ret.add(1);
            }
        }
    }
    return ret;
}

From source file:org.richfaces.renderkit.CalendarRendererBase.java

protected Map<String, String[]> getSymbolsMap(FacesContext facesContext, UICalendar calendar) {
    Map<String, String[]> map = new HashMap<String, String[]>();

    Locale locale = calendar.getAsLocale(calendar.getLocale());
    Calendar cal = calendar.getCalendar();
    int maximum = cal.getActualMaximum(Calendar.DAY_OF_WEEK);
    int minimum = cal.getActualMinimum(Calendar.DAY_OF_WEEK);

    int monthMax = cal.getActualMaximum(Calendar.MONTH);
    int monthMin = cal.getActualMinimum(Calendar.MONTH);

    DateFormatSymbols symbols = new DateFormatSymbols(locale);
    String[] weekDayLabels = ComponentUtil.asArray(calendar.getWeekDayLabels());
    if (weekDayLabels == null) {
        weekDayLabels = symbols.getWeekdays();
        weekDayLabels = shiftDates(minimum, maximum, weekDayLabels);
    }//from w w w. j a  v a2 s. c o  m

    String[] weekDayLabelsShort = ComponentUtil.asArray(calendar.getWeekDayLabelsShort());
    if (weekDayLabelsShort == null) {
        weekDayLabelsShort = symbols.getShortWeekdays();
        weekDayLabelsShort = shiftDates(minimum, maximum, weekDayLabelsShort);
    }

    String[] monthLabels = ComponentUtil.asArray(calendar.getMonthLabels());
    if (monthLabels == null) {
        monthLabels = symbols.getMonths();
        monthLabels = shiftDates(monthMin, monthMax, monthLabels);
    }

    String[] monthLabelsShort = ComponentUtil.asArray(calendar.getMonthLabelsShort());
    if (monthLabelsShort == null) {
        monthLabelsShort = symbols.getShortMonths();
        monthLabelsShort = shiftDates(monthMin, monthMax, monthLabelsShort);
    }

    map.put(WEEK_DAY_LABELS, weekDayLabels);
    map.put(WEEK_DAY_LABELS_SHORT, weekDayLabelsShort);
    map.put(MONTH_LABELS, monthLabels);
    map.put(MONTH_LABELS_SHORT, monthLabelsShort);

    return map;
}

From source file:org.kuali.coeus.s2sgen.impl.datetime.S2SDateTimeServiceImpl.java

/**
 *
 * This method computes the number of months between any 2 given {@link java.sql.Date} objects
 *
 * @param dateStart starting date.//from   w  ww  .  j  av  a 2  s  .  c om
 * @param dateEnd end date.
 *
 * @return number of months between the start date and end date.
 */
@Override
public ScaleTwoDecimal getNumberOfMonths(java.util.Date dateStart, java.util.Date dateEnd) {
    ScaleTwoDecimal monthCount = ScaleTwoDecimal.ZERO;
    int fullMonthCount = 0;

    Calendar startDate = Calendar.getInstance();
    Calendar endDate = Calendar.getInstance();
    startDate.setTime(dateStart);
    endDate.setTime(dateEnd);

    startDate.clear(Calendar.HOUR);
    startDate.clear(Calendar.MINUTE);
    startDate.clear(Calendar.SECOND);
    startDate.clear(Calendar.MILLISECOND);

    endDate.clear(Calendar.HOUR);
    endDate.clear(Calendar.MINUTE);
    endDate.clear(Calendar.SECOND);
    endDate.clear(Calendar.MILLISECOND);

    if (startDate.after(endDate)) {
        return ScaleTwoDecimal.ZERO;
    }
    int startMonthDays = startDate.getActualMaximum(Calendar.DATE) - startDate.get(Calendar.DATE);
    startMonthDays++;
    int startMonthMaxDays = startDate.getActualMaximum(Calendar.DATE);
    BigDecimal startMonthFraction = BigDecimal.valueOf(startMonthDays).setScale(2, RoundingMode.HALF_UP).divide(
            BigDecimal.valueOf(startMonthMaxDays).setScale(2, RoundingMode.HALF_UP), RoundingMode.HALF_UP);

    int endMonthDays = endDate.get(Calendar.DATE);
    int endMonthMaxDays = endDate.getActualMaximum(Calendar.DATE);

    BigDecimal endMonthFraction = BigDecimal.valueOf(endMonthDays).setScale(2, RoundingMode.HALF_UP).divide(
            BigDecimal.valueOf(endMonthMaxDays).setScale(2, RoundingMode.HALF_UP), RoundingMode.HALF_UP);

    startDate.set(Calendar.DATE, 1);
    endDate.set(Calendar.DATE, 1);

    while (startDate.getTimeInMillis() < endDate.getTimeInMillis()) {
        startDate.set(Calendar.MONTH, startDate.get(Calendar.MONTH) + 1);
        fullMonthCount++;
    }
    fullMonthCount = fullMonthCount - 1;
    monthCount = monthCount.add(new ScaleTwoDecimal(fullMonthCount))
            .add(new ScaleTwoDecimal(startMonthFraction)).add(new ScaleTwoDecimal(endMonthFraction));
    return monthCount;
}

From source file:org.kuali.coeus.propdev.impl.print.ProposalBaseStream.java

/**
 *
 * This method computes the number of months between any 2 given {@link java.sql.Date} objects
 *
 * @param dateStart starting date./*from w w w . jav a 2s.c  o m*/
 * @param dateEnd end date.
 *
 * @return number of months between the start date and end date.
 */
public ScaleTwoDecimal getNumberOfMonths(Date dateStart, Date dateEnd) {
    ScaleTwoDecimal monthCount = ScaleTwoDecimal.ZERO;
    int fullMonthCount = 0;

    Calendar startDate = Calendar.getInstance();
    Calendar endDate = Calendar.getInstance();
    startDate.setTime(dateStart);
    endDate.setTime(dateEnd);

    startDate.clear(Calendar.HOUR);
    startDate.clear(Calendar.MINUTE);
    startDate.clear(Calendar.SECOND);
    startDate.clear(Calendar.MILLISECOND);

    endDate.clear(Calendar.HOUR);
    endDate.clear(Calendar.MINUTE);
    endDate.clear(Calendar.SECOND);
    endDate.clear(Calendar.MILLISECOND);

    if (startDate.after(endDate)) {
        return ScaleTwoDecimal.ZERO;
    }
    int startMonthDays = startDate.getActualMaximum(Calendar.DATE) - startDate.get(Calendar.DATE);
    startMonthDays++;
    int startMonthMaxDays = startDate.getActualMaximum(Calendar.DATE);
    BigDecimal startMonthFraction = BigDecimal.valueOf(startMonthDays).setScale(2, RoundingMode.HALF_UP).divide(
            BigDecimal.valueOf(startMonthMaxDays).setScale(2, RoundingMode.HALF_UP), RoundingMode.HALF_UP);

    int endMonthDays = endDate.get(Calendar.DATE);
    int endMonthMaxDays = endDate.getActualMaximum(Calendar.DATE);

    BigDecimal endMonthFraction = BigDecimal.valueOf(endMonthDays).setScale(2, RoundingMode.HALF_UP).divide(
            BigDecimal.valueOf(endMonthMaxDays).setScale(2, RoundingMode.HALF_UP), RoundingMode.HALF_UP);

    startDate.set(Calendar.DATE, 1);
    endDate.set(Calendar.DATE, 1);

    while (startDate.getTimeInMillis() < endDate.getTimeInMillis()) {
        startDate.set(Calendar.MONTH, startDate.get(Calendar.MONTH) + 1);
        fullMonthCount++;
    }
    fullMonthCount = fullMonthCount - 1;
    monthCount = monthCount.add(new ScaleTwoDecimal(fullMonthCount))
            .add(new ScaleTwoDecimal(startMonthFraction)).add(new ScaleTwoDecimal(endMonthFraction));
    return monthCount;
}

From source file:it.scoppelletti.mobilepower.types.SimpleDate.java

/**
 * Calcola il codice giorno della data./* ww  w  .  j av  a 2s  .  com*/
 * 
 * <P>Il <DFN>codice giorno</DFN> corrisponde al numero di giorni che
 * intercorrono da un ipotetico anno 0 alla data presa in esame.</P>
 * 
 * @return Valore.
 */
public long calcDayCode() {
    int month;
    long dayCode;
    Calendar cal;

    if (isEmpty()) {
        return 0;
    }

    cal = Calendar.getInstance();
    dayCode = SimpleDate.calcYearCode(myYear);
    cal.set(Calendar.YEAR, myYear);
    for (month = 0; month < myMonth; month++) {
        cal.set(Calendar.MONTH, month);
        dayCode += cal.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    return dayCode + myDay;
}

From source file:nl.strohalm.cyclos.controls.reports.statistics.StatisticsAction.java

protected Period getPeriodByTimeRange(final StatisticalQuery queryParameters) {
    final ThroughTimeRange throughTimeRange = queryParameters.getThroughTimeRange();
    Calendar calendarBegin = null;
    Calendar calendarEnd = null;/* w w  w. j ava  2s.c o m*/

    if (throughTimeRange == ThroughTimeRange.MONTH) {
        calendarBegin = new GregorianCalendar(queryParameters.getInitialMonthYear(),
                queryParameters.getInitialMonth().getValue(), 1);
        final Calendar calendarEndAux = new GregorianCalendar(queryParameters.getFinalMonthYear(),
                queryParameters.getFinalMonth().getValue(), 1);
        calendarEnd = new GregorianCalendar(queryParameters.getFinalMonthYear(),
                queryParameters.getFinalMonth().getValue(),
                calendarEndAux.getActualMaximum(Calendar.DAY_OF_MONTH), 23, 59, 59);

    } else if (throughTimeRange == ThroughTimeRange.QUARTER) {
        final Quarter initialQuarter = queryParameters.getInitialQuarter();
        final Quarter finalQuarter = queryParameters.getFinalQuarter();

        calendarBegin = new GregorianCalendar(queryParameters.getInitialQuarterYear(),
                initialQuarter.getValue() * 3 - 3, 1);
        final Calendar calendarEndAux = new GregorianCalendar(queryParameters.getFinalQuarterYear(),
                finalQuarter.getValue() * 3 - 3, 1);
        calendarEnd = new GregorianCalendar(queryParameters.getFinalQuarterYear(),
                finalQuarter.getValue() * 3 - 3, calendarEndAux.getActualMaximum(Calendar.DAY_OF_MONTH), 23, 59,
                59);

    } else if (throughTimeRange == ThroughTimeRange.YEAR) {
        calendarBegin = new GregorianCalendar(queryParameters.getInitialYear(), 0, 1);
        calendarEnd = new GregorianCalendar(queryParameters.getFinalYear(), 11, 31, 23, 59, 59);
    }

    final Period period = new Period(calendarBegin, calendarEnd);
    return period;
}

From source file:com.rogchen.common.xml.UtilDateTime.java

/**
 * Returns a List of month name Strings - suitable for calendar headings.
 *
 * @param locale/*from w w w. ja  v a2s. c o m*/
 * @return List of month name Strings
 */
public static List<String> getMonthNames(Locale locale) {
    Calendar tempCal = Calendar.getInstance(locale);
    tempCal.set(Calendar.MONTH, Calendar.JANUARY);
    SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM", locale);
    List<String> resultList = new ArrayList<String>();
    for (int i = Calendar.JANUARY; i <= tempCal.getActualMaximum(Calendar.MONTH); i++) {
        resultList.add(dateFormat.format(tempCal.getTime()));
        tempCal.roll(Calendar.MONTH, 1);
    }
    return resultList;
}

From source file:com.glaf.core.web.springmvc.WorkCalendarController.java

/**
 * //from w  w w.j a  v a2  s .  c  o  m
 * 
 * @param request
 * @param modelMap
 * @return
 */
@RequestMapping("/showCalendar")
public ModelAndView showCalendar(HttpServletRequest request, ModelMap modelMap) {
    Calendar cal = Calendar.getInstance();
    int month = RequestUtils.getIntParameter(request, "month", cal.get(Calendar.MONTH));
    int year = RequestUtils.getIntParameter(request, "year", cal.get(Calendar.YEAR));

    cal.set(Calendar.MONTH, month); // 
    cal.set(Calendar.YEAR, year); // 
    cal.setFirstDayOfWeek(Calendar.SUNDAY);
    cal.set(Calendar.DAY_OF_MONTH, 1);

    logger.info("month:" + month);
    int firstIndex = cal.get(Calendar.DAY_OF_WEEK) - 1; // 
    logger.info("firstIndex:" + firstIndex);
    int maxIndex = cal.getActualMaximum(Calendar.DAY_OF_MONTH);// 
    logger.info("maxIndex:" + maxIndex);
    int weeks = Calendar.WEEK_OF_MONTH;// 
    cal.set(Calendar.DATE, 1);// 1?
    if (cal.get(Calendar.DAY_OF_WEEK) == 7)
        weeks += 1;
    logger.info("day of week:" + cal.get(Calendar.DAY_OF_WEEK));
    logger.info("weeks:" + weeks);

    String days[] = new String[42];
    for (int i = 0; i < 42; i++) {
        days[i] = "";
    }
    for (int i = 0; i < maxIndex; i++) {
        days[firstIndex + i] = String.valueOf(i + 1);
    }

    List<Integer> list = workCalendarService.getWorkDateList(year, month + 1);
    if (list == null) {
        list = new java.util.ArrayList<Integer>();
    }

    request.setAttribute("list", list);
    request.setAttribute("year", String.valueOf(year));
    request.setAttribute("month", String.valueOf(month));
    request.setAttribute("weeks", String.valueOf(weeks));
    request.setAttribute("days", days);

    String x_view = ViewProperties.getString("calendar.showCalendar");
    if (StringUtils.isNotEmpty(x_view)) {
        return new ModelAndView(x_view, modelMap);
    }

    return new ModelAndView("/modules/sys/calendar/calendar", modelMap);
}