Example usage for java.util Calendar getMinimum

List of usage examples for java.util Calendar getMinimum

Introduction

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

Prototype

public abstract int getMinimum(int field);

Source Link

Document

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

Usage

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.//  www  .  j  a v  a 2  s  .  co 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:DateUtil.java

/**
 * Returns a Date set just to Noon, to the closest possible millisecond
 * of the day. If a null day is passed in, a new Date is created.
 * nnoon (00m 12h 00s)/* w w w.  java  2s.co m*/
 */
public static Date getNoonOfDay(Date day, Calendar cal) {
    if (day == null)
        day = new Date();
    cal.setTime(day);
    cal.set(Calendar.HOUR_OF_DAY, 12);
    cal.set(Calendar.MINUTE, cal.getMinimum(Calendar.MINUTE));
    cal.set(Calendar.SECOND, cal.getMinimum(Calendar.SECOND));
    cal.set(Calendar.MILLISECOND, cal.getMinimum(Calendar.MILLISECOND));
    return cal.getTime();
}

From source file:DateUtil.java

public static Date getStartOfMonth(Date day, Calendar cal) {
    if (day == null)
        day = new Date();
    cal.setTime(day);/* w  w  w .  java 2  s. c om*/

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

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

    return cal.getTime();
}

From source file:org.apache.roller.util.DateUtil.java

/**
 * Returns a Date set to the first possible millisecond of the day, just
 * after midnight. If a null day is passed in, a new Date is created.
 * midnight (00m 00h 00s)/*from www  .  j a  v  a 2s.com*/
 */
public static Date getStartOfDay(Date day, Calendar cal) {
    if (day == null)
        day = new Date();
    cal.setTime(day);
    cal.set(Calendar.HOUR_OF_DAY, cal.getMinimum(Calendar.HOUR_OF_DAY));
    cal.set(Calendar.MINUTE, cal.getMinimum(Calendar.MINUTE));
    cal.set(Calendar.SECOND, cal.getMinimum(Calendar.SECOND));
    cal.set(Calendar.MILLISECOND, cal.getMinimum(Calendar.MILLISECOND));

    return cal.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  ww w . java2  s.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.adobe.acs.commons.http.headers.impl.MonthlyExpiresHeaderFilter.java

@Override
protected void doActivate(ComponentContext context) throws Exception {
    super.doActivate(context);

    @SuppressWarnings("unchecked")
    Dictionary<String, Object> props = context.getProperties();
    dayOfMonth = PropertiesUtil.toString(props.get(PROP_EXPIRES_DAY_OF_MONTH), null);

    if (StringUtils.isBlank(dayOfMonth)) {
        throw new ConfigurationException(PROP_EXPIRES_DAY_OF_MONTH, "Day of month must be specified.");
    }/* w  ww  . ja v a 2  s  .  c o m*/

    if (!StringUtils.equalsIgnoreCase(LAST, dayOfMonth)) {
        // Make sure it's a valid value for Calendar.
        try {
            int intDay = Integer.parseInt(dayOfMonth);
            Calendar test = Calendar.getInstance();
            if (intDay < test.getMinimum(Calendar.DAY_OF_MONTH)) {
                throw new ConfigurationException(PROP_EXPIRES_DAY_OF_MONTH,
                        "Day of month is smaller than minimum allowed value.");
            }
            if (intDay > test.getActualMaximum(Calendar.DAY_OF_MONTH)) {
                throw new ConfigurationException(PROP_EXPIRES_DAY_OF_MONTH,
                        "Day of month is larger than least maximum allowed value.");
            }
        } catch (NumberFormatException ex) {
            throw new ConfigurationException(PROP_EXPIRES_DAY_OF_MONTH, "Day of month is not a valid value.");
        }
    }
}

From source file:com.gatesnfc.existing.DatePicker_Fix.java

@TargetApi(11)
@Override/*from   w  ww  .  j  a  v  a2s.  co m*/
public Dialog onCreateDialog(Bundle savedInstanceState) {

    // Jelly Bean introduced a bug in DatePickerDialog (and possibly 
    // TimePickerDialog as well), and one of the possible solutions is 
    // to postpone the creation of both the listener and the BUTTON_* .
    // 
    // Passing a null here won't harm because DatePickerDialog checks for a null
    // whenever it reads the listener that was passed here. >>> This seems to be 
    // true down to 1.5 / API 3, up to 4.1.1 / API 16. <<< No worries. For now.
    //
    // See my own question and answer, and details I included for the issue:
    //
    // http://stackoverflow.com/a/11493752/489607
    // http://code.google.com/p/android/issues/detail?id=34833
    //
    // Of course, suggestions welcome.

    final DatePickerDialog picker = new DatePickerDialog(getActivity(), getConstructorListener(), year, month,
            day);

    if (hasJellyBeanAndAbove()) {
        /*
         * Restriction of Date from 1980 Jan 1st to Current Date
         */
        Calendar c = Calendar.getInstance();
        picker.getDatePicker().setMaxDate(c.getTimeInMillis());
        c.set(Calendar.YEAR, 1980);
        c.set(Calendar.MONTH, c.getMinimum(Calendar.MONTH));
        c.set(Calendar.DATE, c.getMinimum(Calendar.DATE));
        c.set(Calendar.HOUR_OF_DAY, c.getMinimum(Calendar.HOUR_OF_DAY));
        c.set(Calendar.MINUTE, c.getMinimum(Calendar.MINUTE));
        c.set(Calendar.SECOND, c.getMinimum(Calendar.SECOND));
        c.set(Calendar.MILLISECOND, c.getMinimum(Calendar.MILLISECOND));
        picker.getDatePicker().setMinDate(c.getTimeInMillis());
        picker.setButton(DialogInterface.BUTTON_POSITIVE, getActivity().getString(android.R.string.ok),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        DatePicker dp = picker.getDatePicker();
                        mListener.onDateSet(dp, dp.getYear(), dp.getMonth(), dp.getDayOfMonth());
                    }
                });
        picker.setButton(DialogInterface.BUTTON_NEGATIVE, getActivity().getString(android.R.string.cancel),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
    }
    return picker;
}

From source file:org.apache.roller.weblogger.business.runnable.TaskScheduler.java

public void run() {

    boolean firstRun = true;

    // run forever, or until we get interrupted
    while (true) {
        try {//  w w w.  ja  v  a2s .com
            // get current time
            Date now = new Date();
            log.debug("Current time = " + now);

            // run tasks, skip run on first pass
            if (firstRun) {
                // add a slight delay to scheduler start
                Calendar cal = Calendar.getInstance();
                cal.setTime(now);
                cal.add(Calendar.MINUTE, 1);
                cal.set(Calendar.SECOND, cal.getMinimum(Calendar.SECOND));
                cal.set(Calendar.MILLISECOND, cal.getMinimum(Calendar.MILLISECOND));
                now = cal.getTime();
                log.debug("Start time = " + now);

                firstRun = false;
            } else {
                try {
                    runTasks(now);
                } finally {
                    // always release session after each pass
                    WebloggerFactory.getWeblogger().release();
                }
            }

            // wait 'til next minute
            // NOTE: we add 50ms of adjustment time to make sure we awaken
            //       during the next minute, and not before.  awakening at
            //       exactly the .000ms is not of any concern to us
            Date endOfMinute = DateUtil.getEndOfMinute(now);
            long sleepTime = (endOfMinute.getTime() + 50) - System.currentTimeMillis();
            if (sleepTime > 0) {
                log.debug("sleeping - " + sleepTime);
                Thread.sleep(sleepTime);
            } else {
                // it's taken us more than 1 minute for the last loop
                // so recalculate to sleep 'til the end of the current minute
                endOfMinute = DateUtil.getEndOfMinute(new Date());
                sleepTime = (endOfMinute.getTime() + 50) - System.currentTimeMillis();
                log.debug("sleeping - " + sleepTime);
                Thread.sleep(sleepTime);
            }

        } catch (InterruptedException ex) {
            // thread interrupted
            log.debug("Thread interrupted, scheduler is stopping");
            pool.shutdownNow();
            break;
        }
    }

}

From source file:rml.controller.ShopController.java

@RequestMapping(value = "/Shop", method = RequestMethod.GET)
@ResponseBody/*from  www  .j  ava 2  s  . c o  m*/
public Object shopLogin(Shop shop) {

    ReturnJson returnJson = new ReturnJson();
    returnJson.setErrorCode(1000);
    returnJson.setReturnMessage("?");
    returnJson.setServerStatus(0);

    if (StringUtils.isEmpty(shop.getMobile()) || StringUtils.isEmpty(shop.getPassword())) {

        returnJson.setErrorCode(1001);
        returnJson.setReturnMessage("?" + shop.toString());
        returnJson.setServerStatus(1);
        return returnJson;
    }
    String result1 = MD5.GetMD5Code(shop.getRandomKey() + "at^&*ta");
    if (!result1.equals(shop.getSecretKey())) {
        returnJson.setErrorCode(99999);
        returnJson.setReturnMessage("" + shop.toString());
        returnJson.setServerStatus(1);
        return returnJson;
    }
    try {
        shop.setPassword(MD5.GetMD5Code(shop.getPassword()));
        Shop shop1 = shopService.getShop(shop);
        if (shop1 == null) {
            returnJson.setErrorCode(1002);
            returnJson.setReturnMessage("??");
            returnJson.setServerStatus(1);
            return returnJson;
        }
        if (shop1.getStatus() == 1) {
            returnJson.setErrorCode(1003);
            returnJson.setReturnMessage(",");
            returnJson.setServerStatus(1);
            return returnJson;
        }
        if (shop1.getStatus() == 3) {
            returnJson.setErrorCode(1004);
            returnJson.setReturnMessage("");
            returnJson.setServerStatus(1);
            return returnJson;
        }
        Date today = new Date();
        Order order = new Order();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(new Date());
        dateString = dateString + " 00:00:00";
        order.setStartDate(dateString);
        SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");
        dateString = formatter1.format(new Date());
        order.setEndDate(dateString);
        order.setShopId(shop1.getUuid());
        int orderDaily = 0;
        try {
            orderDaily = orderService.getShopReport(order);
        } catch (Exception e) {

        }
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        int YEAR = calendar.getMinimum(Calendar.YEAR);// ???()
        int day = calendar.getMinimum(Calendar.DAY_OF_WEEK);// ???()
        calendar.set(Calendar.YEAR, YEAR);
        calendar.set(Calendar.DAY_OF_WEEK, day);
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.AM_PM, 0);
        calendar.set(Calendar.MINUTE, 0);
        Date date = calendar.getTime();
        int orderTotal = 0;
        order.setStartDate(date.toLocaleString());
        try {
            orderTotal = orderService.getShopReport(order);
        } catch (Exception e) {

        }
        shop1.setMoneyDaily(orderDaily);
        shop1.setMoneyTotal(orderTotal);
        returnJson.setReturnObject(shop1);
    } catch (Exception ex) {
        ex.printStackTrace();
        returnJson.setErrorCode(1005);
        returnJson.setReturnMessage("?");
        returnJson.setServerStatus(2);
        return returnJson;
    }
    return returnJson;
}

From source file:com.autentia.tnt.manager.holiday.UserHolidaysStateManager.java

public UserHolidaysState calcUserHolidaysState(User usuario, Date chargeYear) {
    UserHolidaysState uhs = new UserHolidaysState();
    uhs.setUser(usuario);/*  w  w  w . j a  va2 s . c  o m*/
    // WorkingAgreement attribute is an HB proxy not initialized, we need to get it from DB
    WorkingAgreementDAO workingAgreementDao = WorkingAgreementDAO.getDefault();
    WorkingAgreement agreement = workingAgreementDao.getById(usuario.getAgreement().getId());
    // and refresh the user instance agreement attribute
    usuario.setAgreement(agreement);

    uhs.setTotalYear(agreement.getHolidays());

    int acceptedHolidays = 0;

    if (chargeYear != null) {

        HolidayManager fiestasManager = HolidayManager.getDefault();

        // We must take in account previous year holidays and next year holidays

        Calendar calMin = Calendar.getInstance();
        calMin.setTime(chargeYear);
        calMin.set(Calendar.MONTH, calMin.getMinimum(Calendar.MONTH));
        calMin.set(Calendar.DAY_OF_MONTH, calMin.getMinimum(Calendar.DAY_OF_MONTH));
        calMin.set(Calendar.HOUR_OF_DAY, calMin.getMinimum(Calendar.HOUR_OF_DAY));
        calMin.set(Calendar.MINUTE, calMin.getMinimum(Calendar.MINUTE));
        calMin.set(Calendar.SECOND, calMin.getMinimum(Calendar.SECOND));
        calMin.set(Calendar.MILLISECOND, calMin.getMinimum(Calendar.MILLISECOND));

        Calendar calMax = Calendar.getInstance();
        calMax.setTime(chargeYear);
        calMax.set(Calendar.MONTH, calMax.getMaximum(Calendar.MONTH));
        calMax.set(Calendar.DAY_OF_MONTH, calMax.getMaximum(Calendar.DAY_OF_MONTH));
        calMax.set(Calendar.HOUR_OF_DAY, calMax.getMaximum(Calendar.HOUR_OF_DAY));
        calMax.set(Calendar.MINUTE, calMax.getMaximum(Calendar.MINUTE));
        calMax.set(Calendar.SECOND, calMax.getMaximum(Calendar.SECOND));
        calMax.set(Calendar.MILLISECOND, calMax.getMaximum(Calendar.MILLISECOND));

        calMin.add(Calendar.YEAR, -1);
        calMax.add(Calendar.YEAR, 1);

        HolidaySearch fiestaSearch = new HolidaySearch();
        fiestaSearch.setStartDate(calMin.getTime());
        fiestaSearch.setEndDate(calMax.getTime());

        List<Holiday> listFiestas = fiestasManager.getAllEntities(fiestaSearch, null);

        calMin.setTime(chargeYear);
        calMin.set(Calendar.MONTH, calMin.getMinimum(Calendar.MONTH));
        calMin.set(Calendar.DAY_OF_MONTH, calMin.getMinimum(Calendar.DAY_OF_MONTH));
        calMin.set(Calendar.HOUR_OF_DAY, calMin.getMinimum(Calendar.HOUR_OF_DAY));
        calMin.set(Calendar.MINUTE, calMin.getMinimum(Calendar.MINUTE));
        calMin.set(Calendar.SECOND, calMin.getMinimum(Calendar.SECOND));
        calMin.set(Calendar.MILLISECOND, calMin.getMinimum(Calendar.MILLISECOND));

        calMax.setTime(chargeYear);
        calMax.set(Calendar.MONTH, calMax.getMaximum(Calendar.MONTH));
        calMax.set(Calendar.DAY_OF_MONTH, calMax.getMaximum(Calendar.DAY_OF_MONTH));
        calMax.set(Calendar.HOUR_OF_DAY, calMax.getMaximum(Calendar.HOUR_OF_DAY));
        calMax.set(Calendar.MINUTE, calMax.getMaximum(Calendar.MINUTE));
        calMax.set(Calendar.SECOND, calMax.getMaximum(Calendar.SECOND));
        calMax.set(Calendar.MILLISECOND, calMax.getMaximum(Calendar.MILLISECOND));

        RequestHolidayManager holyManager = RequestHolidayManager.getDefault();
        RequestHolidaySearch holSearch = new RequestHolidaySearch();
        holSearch.setUserRequest(uhs.getUser());
        holSearch.setState(HolidayState.ACCEPT);
        holSearch.setStartChargeYear(calMin.getTime());
        holSearch.setEndChargeYear(calMax.getTime());

        List<RequestHoliday> listH = holyManager.getAllEntities(holSearch, null);

        for (RequestHoliday rH : listH) {
            Calendar cActual = Calendar.getInstance();
            cActual.setTime(rH.getBeginDate());
            while (!cActual.getTime().after(rH.getFinalDate())) {
                if (cActual.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY
                        && cActual.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
                    boolean isFiesta = false;

                    for (Holiday fiest : listFiestas) {
                        Calendar cFiesta = Calendar.getInstance();
                        cFiesta.setTime(fiest.getDate());
                        if (cFiesta.get(Calendar.YEAR) == cActual.get(Calendar.YEAR)
                                && cFiesta.get(Calendar.MONTH) == cActual.get(Calendar.MONTH)
                                && cFiesta.get(Calendar.DAY_OF_MONTH) == cActual.get(Calendar.DAY_OF_MONTH)) {
                            isFiesta = true;
                        }
                    }

                    if (!isFiesta) {
                        acceptedHolidays++;
                    }
                }

                cActual.add(Calendar.DAY_OF_MONTH, 1);
            }

        }
        uhs.setTotalAccepted(acceptedHolidays);

        Calendar calAuxCont = Calendar.getInstance();
        calAuxCont.setTime(uhs.getUser().getStartDate());

        Calendar calAux = Calendar.getInstance();
        calAux.setTime(chargeYear);
        int yearCharge = calAux.get(Calendar.YEAR);
        int yearContract = calAuxCont.get(Calendar.YEAR);

        if (yearCharge == yearContract) {
            // Dividimos los das de cada usuario entre los meses del ao.
            double ratio = uhs.getUser().getAgreement().getHolidays() / 12.0;
            int monthContract = calAuxCont.get(Calendar.MONTH);
            int meses = (Calendar.DECEMBER - monthContract);
            double diasVacaciones = meses * ratio;
            double aux = Math.ceil(diasVacaciones);
            uhs.setTotalYear((int) aux);
        }
    }

    return uhs;
}