Example usage for java.util Calendar add

List of usage examples for java.util Calendar add

Introduction

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

Prototype

public abstract void add(int field, int amount);

Source Link

Document

Adds or subtracts the specified amount of time to the given calendar field, based on the calendar's rules.

Usage

From source file:Main.java

public static Calendar GetLastMonthRightRange(Calendar today) {
    Calendar calendar = (Calendar) today.clone();
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    calendar.add(Calendar.MINUTE, 0);
    return calendar;
}

From source file:Main.java

public static Calendar GetThisMonthLeftRange(Calendar today) {
    Calendar calendar = (Calendar) today.clone();
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    calendar.add(Calendar.MINUTE, 0);
    return calendar;
}

From source file:io.github.sparta.helpers.date.DateUtil.java

/**
 * Add days on date.//w w w . jav  a  2s . c  o  m
 * 
 * @param date
 *            base date
 * @param days
 *            days to be added.
 * @return added Date
 */
public static Date addDay(Date date, int days) {
    Calendar cal = new GregorianCalendar();
    cal.setTime(date);
    cal.add(Calendar.DAY_OF_YEAR, days);
    return cal.getTime();
}

From source file:Main.java

public static boolean isCheckChangePermitted(Context context, Calendar date) {

    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
    String limitActivePref = sharedPrefs.getString("active-date-key", "ALLOW_ALL");

    Calendar today = Calendar.getInstance();
    today.set(Calendar.HOUR_OF_DAY, 0);
    today.set(Calendar.MINUTE, 0);
    today.set(Calendar.SECOND, 0);
    today.set(Calendar.MILLISECOND, 0);

    Calendar yesterday;

    switch (limitActivePref) {
    case "ALLOW_CURRENT":
        return (date.compareTo(today) == 0);
    case "ALLOW_CURRENT_AND_NEXT_DAY":
        yesterday = (Calendar) today.clone();
        yesterday.add(Calendar.DATE, -1);
        return (date.compareTo(yesterday) >= 0);
    case "ALLOW_CURRENT_AND_NEXT_DAY_AND_WEEKEND":
        yesterday = (Calendar) today.clone();
        yesterday.add(Calendar.DATE, today.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY ? -2 : -1);
        return (date.compareTo(yesterday) >= 0);
    case "ALLOW_ALL":
    default://from   w ww.ja  v a2 s  . c  o  m
        return true;
    }
}

From source file:Main.java

public static Calendar GetLastMonthLeftRange(Calendar today) {
    Calendar calendar = (Calendar) today.clone();
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    calendar.add(Calendar.MONTH, -1);
    calendar.add(Calendar.MINUTE, 0);
    return calendar;
}

From source file:com.mxep.web.common.persistence.SearchFilter.java

/**
 * searchParamskey?OPERATOR_FIELDNAME//  w  ww  . j  a v a  2s  .c  o  m
 */
public static Map<String, SearchFilter> parse(Map<String, Object> searchParams) {
    Map<String, SearchFilter> filters = Maps.newHashMap();

    for (Entry<String, Object> entry : searchParams.entrySet()) {
        // 
        String key = entry.getKey();
        Object value = entry.getValue();
        if (value != null && StringUtils.isBlank(value.toString())) {
            continue;
        }

        // operatorfiledAttribute
        String[] names = StringUtils.split(key, "_");
        if (names.length < 2) {
            throw new IllegalArgumentException(key + " is not a valid search filter name");
        }
        String filedName = "";
        for (int i = 1; i < names.length; i++) {
            filedName += names[i];
            if ((i + 1) < names.length) {
                filedName += ".";
            }
        }

        Operator operator = getOperator(names[0]);
        if (null == operator) {
            continue;
        }

        // searchFilter
        if (isValidDate(value.toString())) {
            try {
                Date date = DateUtils.parseDate((String) value, "yyyy-MM-dd");
                // ?
                if (operator.equals(Operator.LT) || operator.equals(Operator.LTE)) {
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(date);
                    cal.add(Calendar.DAY_OF_YEAR, 1);
                    date = cal.getTime();
                }
                // ????
                if (operator.equals(Operator.GT) || operator.equals(Operator.GTE)) {
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(date);
                    date = cal.getTime();
                }
                value = date;
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (isValidDateTime(value.toString())) {
            try {
                Date date = DateUtils.parseDate((String) value, "yyyy-MM-dd HH:mm:ss");
                value = date;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        // searchFilter
        SearchFilter filter = new SearchFilter(filedName, operator, value);
        filters.put(key, filter);
    }

    return filters;
}

From source file:com.streamhub.StreamHubLicenseGenerator.java

private static JPanel createMacAddressRow() {
    JPanel macAddressPanel = new JPanel(new FlowLayout());

    Calendar yesterday = Calendar.getInstance();
    yesterday.setTime(new Date());
    yesterday.add(Calendar.DAY_OF_MONTH, -1);

    Calendar expiry = Calendar.getInstance();
    expiry.setTime(yesterday.getTime());
    expiry.add(Calendar.DAY_OF_MONTH, 60);

    JLabel macAddressLabel = new JLabel("MAC Address:");
    JTextField macAddress = new JTextField(17);
    JLabel startDateLabel = new JLabel("Start Date");
    final JFormattedTextField startDate = new JFormattedTextField(new SimpleDateFormat(DATE_FORMAT));
    startDate.setValue(yesterday.getTime());

    JLabel expiryDateLabel = new JLabel("Expiry Date");
    final JFormattedTextField expiryDate = new JFormattedTextField(new SimpleDateFormat(DATE_FORMAT));
    expiryDate.setValue(expiry.getTime());

    startDate.addKeyListener(new KeyListener() {
        @Override//w w w  . ja  v a2 s.  c o m
        public void keyTyped(KeyEvent arg0) {
            // Get the new date and change expiry to suit
            Date date = (Date) startDate.getValue();
            Calendar tempCal = Calendar.getInstance();
            tempCal.setTime(date);
            tempCal.add(Calendar.DAY_OF_MONTH, 60);
            expiryDate.setValue(tempCal.getTime());
        }

        @Override
        public void keyPressed(KeyEvent e) {
        }

        @Override
        public void keyReleased(KeyEvent e) {
        }
    });

    JComboBox edition = new JComboBox(new String[] { "web", "enterprise" });
    JLabel nameLabel = new JLabel("Name:");
    JTextField name = new JTextField(15);

    JLabel numUsersLabel = new JLabel("Num. Users:");
    JTextField numUsers = new JTextField(DEFAULT_NUM_USERS);

    macAddressPanel.add(macAddressLabel);
    macAddressPanel.add(macAddress);
    macAddressPanel.add(startDateLabel);
    macAddressPanel.add(startDate);
    macAddressPanel.add(expiryDateLabel);
    macAddressPanel.add(expiryDate);
    macAddressPanel.add(edition);
    macAddressPanel.add(nameLabel);
    macAddressPanel.add(name);
    macAddressPanel.add(numUsersLabel);
    macAddressPanel.add(numUsers);
    return macAddressPanel;
}

From source file:helper.util.DateHelper.java

/**
 * @param start date already set to full day boundary (midnight)
 * @param end   date already set to full day boundary (midnight)
 **//* ww  w .  j a va2 s.c o  m*/
private static CalendarRange calculateRangeBeforeEnd(Calendar start, Calendar end, int days) {
    start.add(Calendar.DATE, -(days + 1));
    start.add(Calendar.MINUTE, -1);
    start.add(Calendar.SECOND, 59);
    start.add(Calendar.MILLISECOND, 999);
    return new CalendarRange(start, end);
}

From source file:com.b5m.user.frame.util.DateUtils.java

public static String getBeforeOrAfterDay(Date date, int daycount) {
    if (daycount != 0) {
        Calendar calendar = Calendar.getInstance(); //
        calendar.setTime(date);//?
        calendar.add(Calendar.DAY_OF_MONTH, daycount); //  ? + dayCount
        date = calendar.getTime(); //? + dayCount 
    }//from ww w. j av a  2  s.c  o  m
    return Date2String(date);
}

From source file:Main.java

/** 
 * Adds to the given <code>JComponent</code> two date spinners 
 * labeled "To" and "From". Used to specify a specific date range. The 
 * default range is -10 years from today's date through today's date. 
 * @param c - <code>JComponent</code> to add the date spinners to
 *///from  ww  w  .j a v a  2s .c  o  m
public static void addDateRangePanel(JComponent c) {
    Calendar calendar = Calendar.getInstance();
    JSpinner dateSpinner;

    //Set up dates
    Date initDate = calendar.getTime();
    Date latestDate = calendar.getTime();
    calendar.add(Calendar.YEAR, -10);
    Date earliestDate = calendar.getTime();

    //Date Spinners
    SpinnerModel fromModel = new SpinnerDateModel(initDate, earliestDate, latestDate, Calendar.DAY_OF_MONTH);
    dateSpinner = addLabeledSpinner(c, "From: ", fromModel, false);
    dateSpinner.setEditor(new JSpinner.DateEditor(dateSpinner, "MM/dd/yyyy"));

    SpinnerModel toModel = new SpinnerDateModel(initDate, earliestDate, latestDate, Calendar.DAY_OF_MONTH);
    dateSpinner = addLabeledSpinner(c, "To: ", toModel, true);
    dateSpinner.setEditor(new JSpinner.DateEditor(dateSpinner, "MM/dd/yyyy"));
}