Example usage for java.util Calendar getMaximum

List of usage examples for java.util Calendar getMaximum

Introduction

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

Prototype

public abstract int getMaximum(int field);

Source Link

Document

Returns the maximum value for the given calendar field of this Calendar instance.

Usage

From source file:com.indoqa.lang.util.TimeUtils.java

public static Date getEndOfDay(Date date, TimeZone timeZone) {
    Calendar calendar = Calendar.getInstance(timeZone);

    calendar.setTime(date);// www.java 2s  .  com
    for (int eachField : CALENDAR_TIME_FIELDS) {
        calendar.set(eachField, calendar.getMaximum(eachField));
    }

    return calendar.getTime();
}

From source file:com.vaadin.addon.jpacontainer.demo.TestDataGenerator.java

private static Date createRandomDate() {
    Calendar cal = Calendar.getInstance();
    cal.clear();//from  ww w  . j  a v a  2  s. com
    cal.set(Calendar.YEAR, 1970 + dateRnd.nextInt(40));
    cal.set(Calendar.MONTH, dateRnd.nextInt(12));
    cal.set(Calendar.DATE, dateRnd.nextInt(cal.getMaximum(Calendar.DATE)) + 1);
    return cal.getTime();
}

From source file:DateUtil.java

public static Date getEndOfHour(Date day, Calendar cal) {
    if (day == null || cal == null) {
        return day;
    }/*  ww  w  .jav  a 2 s .c  om*/

    cal.setTime(day);
    cal.set(Calendar.MINUTE, cal.getMaximum(Calendar.MINUTE));
    cal.set(Calendar.SECOND, cal.getMaximum(Calendar.SECOND));
    cal.set(Calendar.MILLISECOND, cal.getMaximum(Calendar.MILLISECOND));
    return cal.getTime();
}

From source file:DateUtil.java

public static Date getEndOfMonth(Date day, Calendar cal) {
    if (day == null)
        day = new Date();
    cal.setTime(day);//from w ww.j  a  v a2  s  . c o  m

    // set time to end of day
    cal.set(Calendar.HOUR_OF_DAY, cal.getMaximum(Calendar.HOUR_OF_DAY));
    cal.set(Calendar.MINUTE, cal.getMaximum(Calendar.MINUTE));
    cal.set(Calendar.SECOND, cal.getMaximum(Calendar.SECOND));
    cal.set(Calendar.MILLISECOND, cal.getMaximum(Calendar.MILLISECOND));

    // set time to first day of month
    cal.set(Calendar.DAY_OF_MONTH, 1);

    // add one month
    cal.add(Calendar.MONTH, 1);

    // back up one day
    cal.add(Calendar.DAY_OF_MONTH, -1);

    return cal.getTime();
}

From source file:org.silverpeas.util.DateUtil.java

/**
 * Compute the date of the last day in the month of the specified date.
 * @param date the specified date.//from   w w  w .ja va  2 s  . c  o m
 * @return a date for the last day of the month of the specified date.
 */
public static Date getEndDateOfMonth(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.set(DAY_OF_MONTH, calendar.getMaximum(DAY_OF_MONTH));
    calendar.set(HOUR_OF_DAY, 23);
    calendar.set(MINUTE, 59);
    calendar.set(SECOND, 59);
    calendar.set(MILLISECOND, 999);
    return calendar.getTime();
}

From source file:edu.stanford.muse.email.CalendarUtil.java

public static int getDiffInMonths(Date firstDate, Date lastDate) {
    Calendar cFirst = new GregorianCalendar();
    cFirst.setTime(firstDate);//from  w ww.j  av a2s.  co  m
    Calendar cLast = new GregorianCalendar();
    cLast.setTime(lastDate);
    int cFirst_year = cFirst.get(Calendar.YEAR);
    int cFirst_month = cFirst.get(Calendar.MONTH);
    int cLast_year = cLast.get(Calendar.YEAR);
    int cLast_month = cLast.get(Calendar.MONTH);
    return (cLast_year - cFirst_year)
            * (cLast.getMaximum(Calendar.MONTH) - cLast.getMinimum(Calendar.MONTH) + 1)
            + (cLast_month - cFirst_month);
}

From source file:com.firstproj.testtwo.web.Test.java

private static void getDate() {

    Calendar calendar = Calendar.getInstance();

    StringBuffer startSb = new StringBuffer();
    StringBuffer endSb = new StringBuffer();
    String year = String.valueOf(calendar.get(calendar.YEAR));
    String prevMonth = String.format("%02d", calendar.get(calendar.MONTH));

    String startDate = startSb.append(year).append(prevMonth).append(String.format("%02d", 1)).toString();
    String endDate = endSb.append(year).append(prevMonth)
            .append(String.format("%02d", calendar.getMaximum(calendar.DAY_OF_MONTH))).toString();

    System.out.println(startDate + "\n" + endDate);
}

From source file:org.kuali.rice.krad.lookup.LookupUtils.java

/**
 * Determines what Timestamp should be used for active queries on effective dated records. Determination made as
 * follows://  w w w .jav  a 2  s  .co  m
 *
 * <ul>
 * <li>Use activeAsOfDate value from search values Map if value is not empty</li>
 * <li>If search value given, try to convert to sql date, if conversion fails, try to convert to Timestamp</li>
 * <li>If search value empty, use current Date</li>
 * <li>If Timestamp value not given, create Timestamp from given Date setting the time as 1 second before midnight
 * </ul>
 *
 * @param searchValues map containing search key/value pairs
 * @return timestamp to be used for active criteria
 */
public static Timestamp getActiveDateTimestampForCriteria(Map searchValues) {
    Date activeDate = CoreApiServiceLocator.getDateTimeService().getCurrentSqlDate();

    Timestamp activeTimestamp = null;
    if (searchValues.containsKey(KRADPropertyConstants.ACTIVE_AS_OF_DATE)) {
        String activeAsOfDate = (String) searchValues.get(KRADPropertyConstants.ACTIVE_AS_OF_DATE);
        if (StringUtils.isNotBlank(activeAsOfDate)) {
            try {
                activeDate = CoreApiServiceLocator.getDateTimeService()
                        .convertToSqlDate(KRADUtils.clean(activeAsOfDate));
            } catch (ParseException e) {
                // try to parse as timestamp
                try {
                    activeTimestamp = CoreApiServiceLocator.getDateTimeService()
                            .convertToSqlTimestamp(KRADUtils.clean(activeAsOfDate));
                } catch (ParseException e1) {
                    throw new RuntimeException("Unable to convert date: " + KRADUtils.clean(activeAsOfDate));
                }
            }
        }
    }

    // if timestamp not given set to 1 second before midnight on the given date
    if (activeTimestamp == null) {
        Calendar cal = Calendar.getInstance();

        cal.setTime(activeDate);
        cal.set(Calendar.HOUR, cal.getMaximum(Calendar.HOUR));
        cal.set(Calendar.MINUTE, cal.getMaximum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, cal.getMaximum(Calendar.SECOND));

        activeTimestamp = new Timestamp(cal.getTime().getTime());
    }

    return activeTimestamp;
}

From source file:fr.paris.lutece.plugins.calendar.web.SmallMonthCalendar.java

/**
 * Provides a small HTML month calendar displaying days with links
 * @return The HTML code of the month.//from w  ww.j  av  a  2 s  .  c o m
 * @param options The options which contains displaying settings
 * @param strDate The code date defining the month to display
 * @param agenda An agenda to hilight some days.
 * @param bIsSelectedDay true if the date is the selected day, false
 *            otherwise
 */
public static String getSmallMonthCalendar(String strDate, Agenda agenda, CalendarUserOptions options,
        boolean bIsSelectedDay) {
    Map<String, Object> model = new HashMap<String, Object>();
    Calendar calendar = new GregorianCalendar();
    calendar.set(Utils.getYear(strDate), Utils.getMonth(strDate), 1);

    Calendar firstDayOfMonth = new GregorianCalendar();
    firstDayOfMonth.set(Utils.getYear(strDate), Utils.getMonth(strDate),
            calendar.getMinimum(Calendar.DAY_OF_MONTH));

    Date dFirstDayOfMonth = firstDayOfMonth.getTime();

    Calendar lastDayOfMonth = new GregorianCalendar();
    lastDayOfMonth.set(Utils.getYear(strDate), Utils.getMonth(strDate),
            calendar.getMaximum(Calendar.DAY_OF_MONTH));

    Date dLastDayOfMonth = lastDayOfMonth.getTime();

    int nDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

    if (nDayOfWeek == 1) {
        nDayOfWeek = 8;
    }

    StringBuffer sbWeeks = new StringBuffer();

    boolean bDone = false;
    boolean bStarted = false;

    while (!bDone) {
        Map<String, Object> weekModel = new HashMap<String, Object>();

        //HtmlTemplate tWeek = new HtmlTemplate( templateWeek );
        StringBuffer sbDays = new StringBuffer();

        for (int i = 0; i < 7; i++) {
            if ((((i + 2) != nDayOfWeek) && !bStarted) || bDone) {
                sbDays.append(AppTemplateService.getTemplate(TEMPLATE_EMPTY_DAY).getHtml());

                continue;
            }
            bStarted = true;

            if (strDate.equals(Utils.getDate(calendar)) && bIsSelectedDay) {
                sbDays.append(getDay(calendar, agenda, options, true));
            } else {
                sbDays.append(getDay(calendar, agenda, options, false));
            }

            int nDay = calendar.get(Calendar.DAY_OF_MONTH);
            calendar.roll(Calendar.DAY_OF_MONTH, true);

            int nNewDay = calendar.get(Calendar.DAY_OF_MONTH);

            if (nNewDay < nDay) {
                bDone = true;
            }
        }

        weekModel.put(Constants.MARK_DAYS, sbDays.toString());
        sbWeeks.append(AppTemplateService.getTemplate(TEMPLATE_WEEK, options.getLocale(), weekModel).getHtml());
    }

    model.put(Constants.MARK_MONTH_LABEL, Utils.getMonthLabel(strDate, options.getLocale()));
    model.put(Constants.MARK_PREVIOUS, Utils.getPreviousMonth(strDate));
    model.put(Constants.MARK_DATE, strDate);
    model.put(Constants.MARK_NEXT, Utils.getNextMonth(strDate));
    model.put(Constants.MARK_DATE_START, dFirstDayOfMonth);
    model.put(Constants.MARK_DATE_END, dLastDayOfMonth);

    model.put(Constants.MARK_WEEKS, sbWeeks.toString());
    model.put(Constants.MARK_JSP_URL, AppPropertiesService.getProperty(Constants.PROPERTY_RUNAPP_JSP_URL));

    HtmlTemplate template = AppTemplateService.getTemplate(TEMPLATE_VIEW_MONTH, options.getLocale(), model);

    return template.getHtml();
}

From source file:fr.paris.lutece.plugins.calendar.service.XMLUtils.java

/**
 * Get the XML of a calendar//  ww  w  .  j  a  v a 2 s. c  o m
 * @param local The locale
 * @param cal The calendar
 * @param request The request
 * @return The XML
 */
public static String getXMLPortletCalendar(Locale local, Calendar cal, HttpServletRequest request) {
    StringBuffer strXml = new StringBuffer();

    String strDay = null;
    Calendar calendar = new GregorianCalendar();

    //Set the calendar in the beginning of the month
    calendar.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 1);

    int nDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

    //If day of week is sunday: nDayOfWeek = 8
    if (nDayOfWeek == 1) {
        nDayOfWeek = 8;
    }

    Calendar calendar2 = new GregorianCalendar();
    calendar2.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), calendar.getMaximum(Calendar.DAY_OF_MONTH));

    //Beginning of the main xml block: month
    XmlUtil.beginElement(strXml, TAG_AGENDA_MONTH);

    String strBaseUrl = AppPathService.getPortalUrl();

    UrlItem urlMonth = new UrlItem(strBaseUrl);
    urlMonth.addParameter(Constants.PARAMETER_PAGE, Constants.PLUGIN_NAME);
    urlMonth.addParameter(Constants.PARAMETER_ACTION, Constants.ACTION_DO_SEARCH);

    urlMonth.addParameter(Constants.PARAMETER_DATE_START, DateUtil.getDateString(calendar.getTime(), local));
    urlMonth.addParameter(Constants.PARAMETER_DATE_END, DateUtil.getDateString(calendar2.getTime(), local));
    urlMonth.addParameter(Constants.PARAMETER_PERIOD, Constants.PROPERTY_PERIOD_RANGE);

    String strMonthLabel = Utils.getMonthLabel(Utils.getDate(calendar), local);

    String strUrlMonth = BEGIN_A_TAG + urlMonth.getUrl() + "\">" + strMonthLabel + END_A_TAG;

    XmlUtil.addElementHtml(strXml, TAG_AGENDA_MONTH_LABEL, strUrlMonth);

    //Shortcut tags
    //Begenning of the xml block: week-shortcuts
    XmlUtil.beginElement(strXml, TAG_WEEK_SHORTCUTS);

    //Today shortcut 
    XmlUtil.beginElement(strXml, TAG_WEEK_SHORTCUT);
    XmlUtil.addElement(strXml, TAG_WEEK_SHORTCUT_LABEL,
            I18nService.getLocalizedString(Constants.PROPERTY_SHORTCUT_TODAY, local));
    XmlUtil.addElement(strXml, TAG_WEEK_SHORTCUT_PERIOD, Constants.PROPERTY_PERIOD_TODAY);
    XmlUtil.addElement(strXml, TAG_WEEK_SHORTCUT_DATE_START, DateUtil.getDateString(new Date(), local));
    XmlUtil.addElement(strXml, TAG_WEEK_SHORTCUT_DATE_END, DateUtil.getDateString(new Date(), local));
    XmlUtil.endElement(strXml, TAG_WEEK_SHORTCUT);

    //Tomorrow shortcut 
    Calendar calTomorrow = new GregorianCalendar();
    calTomorrow.add(Calendar.DATE, 1);
    XmlUtil.beginElement(strXml, TAG_WEEK_SHORTCUT);
    XmlUtil.addElement(strXml, TAG_WEEK_SHORTCUT_LABEL,
            I18nService.getLocalizedString(Constants.PROPERTY_SHORTCUT_TOMORROW, local));
    XmlUtil.addElement(strXml, TAG_WEEK_SHORTCUT_PERIOD, Constants.PROPERTY_PERIOD_RANGE);
    XmlUtil.addElement(strXml, TAG_WEEK_SHORTCUT_DATE_START,
            DateUtil.getDateString(calTomorrow.getTime(), local));
    XmlUtil.addElement(strXml, TAG_WEEK_SHORTCUT_DATE_END,
            DateUtil.getDateString(calTomorrow.getTime(), local));
    XmlUtil.endElement(strXml, TAG_WEEK_SHORTCUT);

    //Week shortcut
    Date dateBeginWeek = null;
    Date dateEndWeek = null;

    Calendar calendarToday = new GregorianCalendar();
    Calendar calendarFirstDay = Calendar.getInstance();
    Calendar calendarLastDay = new GregorianCalendar();

    calendarFirstDay = calendarToday;
    calendarFirstDay.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    calendarLastDay = (GregorianCalendar) calendarFirstDay.clone();
    calendarLastDay.add(Calendar.DATE, 6);
    dateBeginWeek = calendarFirstDay.getTime();
    dateEndWeek = calendarLastDay.getTime();

    XmlUtil.beginElement(strXml, TAG_WEEK_SHORTCUT);
    XmlUtil.addElement(strXml, TAG_WEEK_SHORTCUT_LABEL,
            I18nService.getLocalizedString(Constants.PROPERTY_SHORTCUT_WEEK, local));
    XmlUtil.addElement(strXml, TAG_WEEK_SHORTCUT_PERIOD, Constants.PROPERTY_PERIOD_WEEK);
    XmlUtil.addElement(strXml, TAG_WEEK_SHORTCUT_DATE_START, DateUtil.getDateString(dateBeginWeek, local));
    XmlUtil.addElement(strXml, TAG_WEEK_SHORTCUT_DATE_END, DateUtil.getDateString(dateEndWeek, local));
    XmlUtil.endElement(strXml, TAG_WEEK_SHORTCUT);

    //Ending of the xml block: week-shortcuts
    XmlUtil.endElement(strXml, TAG_WEEK_SHORTCUTS);

    //Begenning of the xml block: weeks
    XmlUtil.beginElement(strXml, TAG_AGENDA_WEEKS);

    //Day label tags
    XmlUtil.addElement(strXml, TAG_AGENDA_DAY_LABEL,
            I18nService.getLocalizedString(Constants.PROPERTY_SHORTLABEL_MONDAY, local));
    XmlUtil.addElement(strXml, TAG_AGENDA_DAY_LABEL,
            I18nService.getLocalizedString(Constants.PROPERTY_SHORTLABEL_TUESDAY, local));
    XmlUtil.addElement(strXml, TAG_AGENDA_DAY_LABEL,
            I18nService.getLocalizedString(Constants.PROPERTY_SHORTLABEL_WEDNESDAY, local));
    XmlUtil.addElement(strXml, TAG_AGENDA_DAY_LABEL,
            I18nService.getLocalizedString(Constants.PROPERTY_SHORTLABEL_THURSDAY, local));
    XmlUtil.addElement(strXml, TAG_AGENDA_DAY_LABEL,
            I18nService.getLocalizedString(Constants.PROPERTY_SHORTLABEL_FRIDAY, local));
    XmlUtil.addElement(strXml, TAG_AGENDA_DAY_LABEL,
            I18nService.getLocalizedString(Constants.PROPERTY_SHORTLABEL_SATURDAY, local));
    XmlUtil.addElement(strXml, TAG_AGENDA_DAY_LABEL,
            I18nService.getLocalizedString(Constants.PROPERTY_SHORTLABEL_SUNDAY, local));

    //Check if the month is ended
    boolean bDone = false;

    //check if the month is started
    boolean bStarted = false;

    //While the month isn't over...
    while (!bDone) {
        //Begenning of the xml block: week
        XmlUtil.beginElement(strXml, TAG_AGENDA_WEEK);

        for (int i = 0; i < 7; i++) {
            if ((((i + 2) != nDayOfWeek) && !bStarted) || bDone) {
                XmlUtil.beginElement(strXml, TAG_AGENDA_DAY);
                strDay = BEGIN_TD_TAG + getDayClass(calendar) + ">" + PROPERTY_EMPTY_DAY + END_TD_TAG;
                XmlUtil.addElementHtml(strXml, TAG_AGENDA_DAY_CODE, strDay);
                XmlUtil.endElement(strXml, TAG_AGENDA_DAY);

                continue;
            }
            bStarted = true;

            //put parameters in the url
            UrlItem urlDay = new UrlItem(strBaseUrl);
            String strPageId = request != null ? request.getParameter(Constants.PARAMETER_PAGE_ID)
                    : StringUtils.EMPTY;
            if (StringUtils.isNotBlank(strPageId) && StringUtils.isNumeric(strPageId)) {
                urlDay.addParameter(Constants.PARAMETER_PAGE_ID, strPageId);
            }
            urlDay.addParameter(Constants.PARAMETER_DATE, DateUtil.getDateString(calendar.getTime(), local));

            //construct on url based on day
            String strUrlDay = new String();
            strUrlDay = BEGIN_A_TAG + urlDay.getUrl() + "\">"
                    + Integer.toString(calendar.get(Calendar.DAY_OF_MONTH)) + END_A_TAG;

            XmlUtil.beginElement(strXml, TAG_AGENDA_DAY);

            Date date = Utils.getDate(Utils.getDate(calendar));
            Plugin plugin = PluginService.getPlugin(CalendarPlugin.PLUGIN_NAME);

            String[] arrayCalendarIds = Utils.getCalendarIds(request);

            List<Event> listEvent = CalendarSearchService.getInstance().getSearchResults(arrayCalendarIds, null,
                    "", date, date, plugin);
            if (listEvent.size() != 0) {
                strDay = BEGIN_TD_TAG + Constants.STYLE_CLASS_SMALLMONTH_DAY
                        + Constants.STYLE_CLASS_SUFFIX_EVENT + ">" + BEGIN_BOLD_TAG + strUrlDay + END_BOLD_TAG
                        + END_TD_TAG;
            } else {
                strDay = BEGIN_TD_TAG + getDayClass(calendar) + ">"
                        + Integer.toString(calendar.get(Calendar.DAY_OF_MONTH)) + END_TD_TAG;
            }

            XmlUtil.addElementHtml(strXml, TAG_AGENDA_DAY_CODE, strDay);

            XmlUtil.endElement(strXml, TAG_AGENDA_DAY);

            int nDay = calendar.get(Calendar.DAY_OF_MONTH);
            calendar.roll(Calendar.DAY_OF_MONTH, true);

            int nNewDay = calendar.get(Calendar.DAY_OF_MONTH);

            if (nNewDay < nDay) {
                bDone = true;
            }
        }

        //Ending of the xml block: week
        XmlUtil.endElement(strXml, TAG_AGENDA_WEEK);
    }

    //Ending of the xml block: weeks
    XmlUtil.endElement(strXml, TAG_AGENDA_WEEKS);

    //Ending of the xml block: month
    XmlUtil.endElement(strXml, TAG_AGENDA_MONTH);

    return strXml.toString();
}