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:com.ikanow.infinit.e.harvest.enrichment.legacy.opencalais.ExtractorOpenCalais.java

/**
 * Modifies both the time start and time end variables to create time ranges
 * when possible.//from  ww  w . j av  a2  s. c  om
 * 
 * Takes a 2 String array [ timestart, timeend] so that it can be passed by
 * refence and therefore both items  can be modified
 * 
 * @param times 2 String array consisting of index 0 = timestart and index 1 = timeend
 */
private void parseEndDate(String[] times) {
    String time_start = times[0];
    String time_end = times[1];
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
    int num_time_end = 0;
    try {
        num_time_end = Integer.parseInt(time_end);
    } catch (Exception ex) {
        num_time_end = 0;
    }

    try {
        if (num_time_end != 0 && time_end.length() == 4) //CASE 1: 2004 (just a year)
        {
            //just a year, span from jan 1 to dec 31
            Calendar cal = Calendar.getInstance();
            cal.set(num_time_end, 0, 1);
            Date datestart = cal.getTime();
            cal.set(num_time_end, 11, 31);
            Date dateend = cal.getTime();
            time_start = sdf.format(datestart);
            time_end = sdf.format(dateend);
        } else if (time_end.substring(0, 2).toLowerCase().equals("in")) //CASE 2: in 2004 (in year) OR in May (in month)
        {
            try {
                //pull out year and span from jan1 to dec 31
                num_time_end = Integer.parseInt(time_end.substring(3, 7));
                Calendar cal = Calendar.getInstance();
                cal.set(num_time_end, 0, 1);
                Date datestart = cal.getTime();
                cal.set(num_time_end, 11, 31);
                Date dateend = cal.getTime();
                time_start = sdf.format(datestart);
                time_end = sdf.format(dateend);
            } catch (Exception ex) {
                //was not a year, try a month
                String monthString = time_end.substring(3);
                int monthint = parseMonth(monthString);
                if (monthint > -1) {
                    Calendar cal = Calendar.getInstance();
                    cal.set(num_time_end, monthint, 1);
                    Date datestart = cal.getTime();
                    cal.set(num_time_end, monthint, cal.getActualMaximum(Calendar.DATE));
                    Date dateend = cal.getTime();
                    time_start = sdf.format(datestart);
                    time_end = sdf.format(dateend);
                } else {
                    time_end = null;
                }
            }
        } else if (time_end.substring(0, 4).toLowerCase().equals("last")) //CASE 3: last june
        {
            String monthString = time_end.substring(5);
            int monthint = parseMonth(monthString);
            if (monthint > -1) {
                Calendar cal = Calendar.getInstance();
                num_time_end = cal.get(Calendar.YEAR) - 1;
                cal.set(num_time_end, monthint, 1);
                Date datestart = cal.getTime();
                cal.set(num_time_end, monthint, cal.getActualMaximum(Calendar.DATE));
                Date dateend = cal.getTime();
                time_start = sdf.format(datestart);
                time_end = sdf.format(dateend);
            } else {
                time_end = null;
            }
        } else if (time_end.split(" ").length == 2) //CASE 4: June 2004 (month and year)
        {
            String[] parts = time_end.split(" ");
            //try to get month
            int monthint = parseMonth(parts[0]);
            if (monthint > -1) {
                try {
                    num_time_end = Integer.parseInt(parts[1]);
                    Calendar cal = Calendar.getInstance();
                    cal.set(num_time_end, monthint, 1);
                    Date datestart = cal.getTime();
                    cal.set(num_time_end, monthint, cal.getActualMaximum(Calendar.DATE));
                    Date dateend = cal.getTime();
                    time_start = sdf.format(datestart);
                    time_end = sdf.format(dateend);
                } catch (Exception ex) {
                    num_time_end = 0;
                }
            } else {
                time_end = null;
            }
        } else //didn't fall into one of our cases, we either dont need to parse or dont know how so null out
        {
            time_end = null;
        }
    } catch (Exception ex) {
        //we had some sort of error, null out the end date, and leave start date whatever open calais extracted
        time_end = null;
    }

    //System.out.println(time_start + " to " + time_end);
    times[0] = time_start;
    times[1] = time_end;
}

From source file:com.clustercontrol.calendar.composite.CalendarWeekComposite.java

/**
 * ?<BR>/*from ww w. j  a va  2  s.  c o  m*/
 *
 * @since 4.1.0
 */
public void update(String managerName, String calendarId, int year, int month, int day) {
    if (calendarId == null) {
        return;
    }
    // ???
    createPanel();

    int period = 7;
    //??
    Calendar now = Calendar.getInstance(TimezoneUtil.getTimeZone());
    now.set(year, month - 1, day);
    int dayOfWeek = now.get(Calendar.DAY_OF_WEEK);

    m_panel.repaint(); // Sometimes labels disappear in RAP. Repaint to make sure labels show      it 
    //?(???)
    scheduleBars = createScheduleBars(m_panel, dayOfWeek);
    //???
    for (ScheduleBar bar : scheduleBars) {
        bar.getInitBar().setBackgroundColor(ColorConstantsWrapper.red());
    }
    try {
        for (int i = 0; i < period; i++) {
            //?????
            CalendarEndpointWrapper wrapper = CalendarEndpointWrapper.getWrapper(managerName);
            List<CalendarDetailInfo> detailList = wrapper.getCalendarWeek(calendarId, year, month, day);
            m_log.trace("detailList.size=" + detailList.size() + ", " + year + "/" + month + "/" + day);
            //????????
            ArrayList<CalendarDetailInfo> detailListOrder = new ArrayList<CalendarDetailInfo>();
            while (detailList.size() > 0) {
                int size = detailList.size();
                detailListOrder.add(detailList.get(size - 1));
                detailList.remove(size - 1);
            }
            for (CalendarDetailInfo detail : detailListOrder) {
                addScheduleBar(m_panel, dayOfWeek, detail.getTimeFrom(), detail.getTimeTo(),
                        detail.isOperateFlg());
            }
            //
            m_labelMatrix[i].setText(String.format("%02d/%02d/%02d", year, month, day) + " ( "
                    + DayOfWeekConstant.typeToString(dayOfWeek) + " )");
            //??
            if (day + 1 > now.getActualMaximum(Calendar.DAY_OF_MONTH)) {
                //??
                if (month == 12) {
                    year++;
                    month = 1;
                } else {
                    month++;
                }
                day = 1;
            } else {
                day++;
            }
            //???
            now.set(year, month - 1, day);
            dayOfWeek = now.get(Calendar.DAY_OF_WEEK);
        }
    } catch (InvalidRole_Exception e) {
        // ???
        MessageDialog.openInformation(null, Messages.getString("message"),
                Messages.getString("message.accesscontrol.16"));
    } catch (CalendarNotFound_Exception e) {
        // ????????
        m_log.info("update(), " + HinemosMessage.replace(e.getMessage()));
    } catch (Exception e) {
        // ?
        m_log.warn("update(), " + HinemosMessage.replace(e.getMessage()), e);
        MessageDialog.openError(null, Messages.getString("failed"),
                Messages.getString("message.hinemos.failure.unexpected") + ", "
                        + HinemosMessage.replace(e.getMessage()));
    }
    //?
    if (calendarId.length() > 0) {
        m_labelId.setText(Messages.getString("calendar.id") + " : " + calendarId);
    } else {
        m_labelId.setText(Messages.getString("calendar.id") + " : ");
    }
}

From source file:com.ozpathway.sw.erms.webapp.action.schedule.ScheduleBean.java

public void addSampleEntries(ActionEvent event) {
    if (model == null)
        return;//w  w w  .  j  a v  a  2s.  c o m
    Calendar calendar = GregorianCalendar.getInstance();
    calendar.setTime(model.getSelectedDate());
    calendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
    calendar.set(Calendar.HOUR_OF_DAY, 14);
    DefaultScheduleEntry entry1 = new DefaultScheduleEntry();
    // every entry in a schedule must have a unique id
    entry1.setId(RandomStringUtils.randomNumeric(32));
    entry1.setStartTime(calendar.getTime());
    calendar.add(Calendar.MINUTE, 45);
    entry1.setEndTime(calendar.getTime());
    entry1.setTitle("Test MyFaces schedule component");
    entry1.setSubtitle("my office");
    entry1.setDescription("We need to get this thing out of the sandbox ASAP");
    model.addEntry(entry1);
    DefaultScheduleEntry entry2 = new DefaultScheduleEntry();
    entry2.setId(RandomStringUtils.randomNumeric(32));
    // entry2 overlaps entry1
    calendar.add(Calendar.MINUTE, -20);
    entry2.setStartTime(calendar.getTime());
    calendar.add(Calendar.HOUR, 2);
    entry2.setEndTime(calendar.getTime());
    entry2.setTitle("Show schedule component to boss");
    entry2.setSubtitle("my office");
    entry2.setDescription("Convince him to get time to thoroughly test it");
    model.addEntry(entry2);
    DefaultScheduleEntry entry3 = new DefaultScheduleEntry();
    entry3.setId(RandomStringUtils.randomNumeric(32));
    calendar.add(Calendar.DATE, 1);
    calendar.set(Calendar.HOUR_OF_DAY, 9);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    entry3.setStartTime(calendar.getTime());
    calendar.set(Calendar.HOUR_OF_DAY, 17);
    entry3.setEndTime(calendar.getTime());
    entry3.setTitle("Thoroughly test schedule component");
    model.addEntry(entry3);
    DefaultScheduleEntry entry4 = new DefaultScheduleEntry();
    entry4.setId(RandomStringUtils.randomNumeric(32));
    calendar.add(Calendar.MONTH, -1);
    calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    calendar.set(Calendar.HOUR_OF_DAY, 11);
    entry4.setStartTime(calendar.getTime());
    calendar.set(Calendar.HOUR_OF_DAY, 14);
    entry4.setEndTime(calendar.getTime());
    entry4.setTitle("Long lunch");
    model.addEntry(entry4);
    DefaultScheduleEntry entry5 = new DefaultScheduleEntry();
    entry5.setId(RandomStringUtils.randomNumeric(32));
    calendar.add(Calendar.MONTH, 2);
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    calendar.set(Calendar.HOUR_OF_DAY, 1);
    entry5.setStartTime(calendar.getTime());
    calendar.set(Calendar.HOUR_OF_DAY, 5);
    entry5.setEndTime(calendar.getTime());
    entry5.setTitle("Fishing trip");
    model.addEntry(entry5);
    // Let's add a zero length entry...
    DefaultScheduleEntry entry6 = new DefaultScheduleEntry();
    calendar.setTime(model.getSelectedDate());
    calendar.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
    calendar.set(Calendar.HOUR_OF_DAY, 16);
    entry6.setId(RandomStringUtils.randomNumeric(32));
    entry6.setStartTime(calendar.getTime());
    entry6.setEndTime(calendar.getTime());
    entry6.setTitle("Zero length entry");
    entry6.setDescription("Is only rendered when the 'renderZeroLengthEntries' attribute is 'true'");
    model.addEntry(entry6);
    // And also an allday event
    DefaultScheduleEntry entry7 = new DefaultScheduleEntry();
    entry7.setId(RandomStringUtils.randomNumeric(32));
    entry7.setTitle("All day event");
    entry7.setSubtitle("This event renders as an all-day event");
    entry7.setAllDay(true);
    model.addEntry(entry7);
    model.refresh();
}

From source file:CalendarUtils.java

private static void modify(Calendar val, int field, boolean round) {
    boolean roundUp = false;
    for (int i = 0; i < fields.length; i++) {
        for (int j = 0; j < fields[i].length; j++) {
            if (fields[i][j] == field) {
                //This is our field... we stop looping
                if (round && roundUp) {
                    if (field == CalendarUtils.SEMI_MONTH) {
                        //This is a special case that's hard to generalize
                        //If the date is 1, we round up to 16, otherwise
                        //  we subtract 15 days and add 1 month
                        if (val.get(Calendar.DATE) == 1) {
                            val.add(Calendar.DATE, 15);
                        } else {
                            val.add(Calendar.DATE, -15);
                            val.add(Calendar.MONTH, 1);
                        }//from  www.  ja  va2 s  .co  m
                    } else {
                        //We need at add one to this field since the
                        //  last number causes us to round up
                        val.add(fields[i][0], 1);
                    }
                }
                return;
            }
        }
        //We have various fields that are not easy roundings
        int offset = 0;
        boolean offsetSet = false;
        //These are special types of fields that require different rounding rules
        switch (field) {
        case CalendarUtils.SEMI_MONTH:
            if (fields[i][0] == Calendar.DATE) {
                //If we're going to drop the DATE field's value,
                //  we want to do this our own way.
                //We need to subtrace 1 since the date has a minimum of 1
                offset = val.get(Calendar.DATE) - 1;
                //If we're above 15 days adjustment, that means we're in the
                //  bottom half of the month and should stay accordingly.
                if (offset >= 15) {
                    offset -= 15;
                }
                //Record whether we're in the top or bottom half of that range
                roundUp = offset > 7;
                offsetSet = true;
            }
            break;
        case Calendar.AM_PM:
            if (fields[i][0] == Calendar.HOUR) {
                //If we're going to drop the HOUR field's value,
                //  we want to do this our own way.
                offset = val.get(Calendar.HOUR);
                if (offset >= 12) {
                    offset -= 12;
                }
                roundUp = offset > 6;
                offsetSet = true;
            }
            break;
        }
        if (!offsetSet) {
            int min = val.getActualMinimum(fields[i][0]);
            int max = val.getActualMaximum(fields[i][0]);
            //Calculate the offset from the minimum allowed value
            offset = val.get(fields[i][0]) - min;
            //Set roundUp if this is more than half way between the minimum and maximum
            roundUp = offset > ((max - min) / 2);
        }
        //We need to remove this field
        val.add(fields[i][0], -offset);
    }
    throw new RuntimeException("We do not support that field.");

}

From source file:vn.com.ecopharma.emp.service.impl.EmpLocalServiceImpl.java

public void fixLaborContractSignedDate() throws java.text.ParseException, SystemException {
    List<Emp> emps = findAll();
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    Date createdDateCheck = sdf.parse("01/08/2016");
    Calendar calendar = Calendar.getInstance();
    for (Emp emp : emps) {
        Date createdDate = emp.getCreateDate();
        Date laborContractSignedDate = emp.getLaborContractSignedDate();
        if (createdDate.compareTo(createdDateCheck) < 0
                && !emp.getLaborContractType().equalsIgnoreCase(LaborContractType.PROBATION_CONTRACT.toString())
                && laborContractSignedDate != null) {
            calendar.setTime(laborContractSignedDate);
            boolean isLastDay = calendar.get(Calendar.DATE) == calendar.getActualMaximum(Calendar.DATE);

            if (isLastDay) {
                User user = UserLocalServiceUtil.fetchUser(emp.getEmpUserId());
                LOGGER.info(emp.getEmpCode() + "  " + user.getFullName() + "   Signed Date "
                        + sdf.format(calendar.getTime()));

                calendar.set(Calendar.DATE, 1);
                calendar.add(Calendar.MONTH, 1);

                LOGGER.info("Will be Changed to: " + sdf.format(calendar.getTime()));

                emp.setLaborContractSignedDate(calendar.getTime());
                updateEmp(emp);/*from  ww w  .ja va2 s . co m*/

            }

        }
    }
}

From source file:org.kuali.kra.proposaldevelopment.printing.xmlstream.NIHResearchAndRelatedXmlStream.java

/**
 * //  www  . ja v  a 2 s.c  o  m
 * This method computes the number of months between any 2 given
 * {@link Date} objects
 * 
 * @param dateStart
 *            starting date.
 * @param dateEnd
 *            end date.
 * 
 * @return number of months between the start date and end date.
 */
private BudgetDecimal getNumberOfMonths(Date dateStart, Date dateEnd) {
    BudgetDecimal monthCount = BudgetDecimal.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 BudgetDecimal.ZERO;
    }
    int startMonthDays = startDate.getActualMaximum(Calendar.DATE) - startDate.get(Calendar.DATE);
    startMonthDays++;
    int startMonthMaxDays = startDate.getActualMaximum(Calendar.DATE);
    BudgetDecimal startMonthFraction = new BudgetDecimal(startMonthDays)
            .divide(new BudgetDecimal(startMonthMaxDays));

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

    BudgetDecimal endMonthFraction = new BudgetDecimal(endMonthDays).divide(new BudgetDecimal(endMonthMaxDays));

    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 BudgetDecimal(fullMonthCount)).add(startMonthFraction)
            .add(endMonthFraction);
    return monthCount;
}

From source file:org.alfresco.service.cmr.calendar.CalendarRecurrenceHelper.java

/**
 * Build the recurrences for recurrence rules 'weekday', 'weekend day'
 * @param currentDate the Calendar for current event
 * @param dates  Map for recurrence events dates
 * @param params recurrence rules/*from   w  w w.ja  v a 2 s . c  o m*/
 * @param until  date when the current event ends
 */
private static void buildWeekdayAndWeekEndRecurence(Calendar currentDate, List<Date> dates,
        Map<String, String> params, Date until, int intervalInMonths) {
    String dayPosStr;
    String dayWeekType;

    // founds which of the recurrence pattern is used "weekday" or "weekend day"
    if (params.get("BYWEEKDAY") != null) {
        dayPosStr = params.get("BYWEEKDAY");
        dayWeekType = "WEEKDAYS";
    } else if (params.get("BYWEEKENDDAY") != null) {
        dayPosStr = params.get("BYWEEKENDDAY");
        dayWeekType = "WEEKENDS";
    } else {
        dayPosStr = params.get("BYANYDAY");
        dayWeekType = "DAY";
    }

    List<Integer> daysOfWeek = getDaysOfWeek(params, dayWeekType);

    boolean isCurrentDateAfterUntil = false;
    int firstMonthDay = 1;

    while (!isCurrentDateAfterUntil) {
        // Setting the current date to the first day of month
        currentDate.set(Calendar.DAY_OF_MONTH, firstMonthDay);
        if (currentDate.getTime().before(until)) {
            int currentDayOfWeek;

            // The sequence number for "BYSETPOS" parameter from recurrence rule for current date.
            int dayCount = 0;
            // week day position, e.q.: first, second. third, forth, last. If the weekday position is 'last' the weekDayPos
            // value will less then '0'
            int weekDayPos = Integer.parseInt(dayPosStr);

            if (weekDayPos > 0) {
                // Setting the current date to the first day of month
                currentDate.set(Calendar.DAY_OF_MONTH, firstMonthDay);
                // Walk forward from the first day of the month to the required day position according the recurrence
                // rule, skipping the unnecessary  days. F.ex, if we need only weekdays then weekends days should be skipped.
                while (dayCount != weekDayPos) {
                    currentDayOfWeek = currentDate.get(Calendar.DAY_OF_WEEK);

                    if (daysOfWeek.contains(currentDayOfWeek)) {
                        dayCount++;
                    }

                    // If dayCount is not what we need go to the next day of the current month
                    if (dayCount != weekDayPos) {
                        currentDate.add(Calendar.DAY_OF_MONTH, 1);
                    }
                }
            }
            //when weekday position is 'last'
            else {
                // Sets the last day of moth and retrieves the weekday number
                currentDate.set(Calendar.DAY_OF_MONTH, currentDate.getActualMaximum(Calendar.DAY_OF_MONTH));
                currentDayOfWeek = currentDate.get(Calendar.DAY_OF_WEEK);

                // walk back from the last day of the month to last weekend day
                while (!daysOfWeek.contains(currentDayOfWeek)) {
                    currentDate.add(Calendar.DAY_OF_MONTH, -1);
                    currentDayOfWeek = currentDate.get(Calendar.DAY_OF_WEEK);
                }
            }
            dates.add(currentDate.getTime());
            currentDate.add(Calendar.MONTH, intervalInMonths);
        } else {
            // The currentDate is after 'until' date.
            isCurrentDateAfterUntil = true;
        }
    }
}

From source file:com.lynk.hrm.ui.dialog.InfoEmployee.java

protected void setWorkAge() {
    Calendar probationCalendar = uiEntryDate.getCalendar();
    uiWorkAge.setText(UtilsClient.convertWorkAgeToStr(UtilsClient.getWorkAge(probationCalendar)));

    probationCalendar.add(Calendar.MONTH, 6);
    probationCalendar.add(Calendar.DAY_OF_MONTH, -1);
    uiProbation.setCalendar(probationCalendar);

    Calendar expirationCalendar = uiEntryDate.getCalendar();
    expirationCalendar.add(Calendar.YEAR, 3);
    expirationCalendar.set(Calendar.DAY_OF_MONTH, expirationCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    uiExpiration.setCalendar(expirationCalendar);
}

From source file:mvc.OrderController.java

private Date getEndMonth() {
    Calendar cl = Calendar.getInstance();
    cl.set(Calendar.DAY_OF_MONTH, cl.getActualMaximum(Calendar.DAY_OF_MONTH));
    cl.set(Calendar.HOUR_OF_DAY, 23);
    cl.set(Calendar.MINUTE, 59);/*from w  w  w  . j ava  2s  . co  m*/
    cl.set(Calendar.SECOND, 59);
    return cl.getTime();
}

From source file:mvc.OrderController.java

private Date getStatisticsEndDate(Date dateTo) {
    if (dateTo != null) {
        return dateTo;
    } else {//from   ww  w. jav  a  2 s.co m
        Calendar cl = Calendar.getInstance();
        cl.set(Calendar.DAY_OF_MONTH, cl.getActualMaximum(Calendar.DAY_OF_MONTH));
        cl.set(Calendar.HOUR_OF_DAY, 23);
        cl.set(Calendar.MINUTE, 59);
        return cl.getTime();
    }
}