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 long timeInMillis(int hour, int minute) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.MINUTE, minute);
    calendar.set(Calendar.SECOND, 0);

    long timeBefore = calendar.getTimeInMillis();

    if (Calendar.getInstance().getTimeInMillis() > timeBefore)
        calendar.add(Calendar.DAY_OF_WEEK, 1);
    return calendar.getTimeInMillis();
}

From source file:Main.java

/**
 * Add date time over one year.//w  ww . j a  va2s .  c om
 *
 * @param listing     the listing
 * @param currentTime the current time
 * @param isPast      true/false if past time
 */
private static void addDateTimeOverOneYear(final List<Long> listing, final long currentTime,
        final boolean isPast) {
    final Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(currentTime);
    calendar.add(Calendar.MONTH, isPast ? -4 : 4);
    calendar.add(Calendar.YEAR, isPast ? -1 : 1);
    listing.add(calendar.getTimeInMillis());
}

From source file:Main.java

public static Calendar GetThisYearLeftRange(Calendar today) {
    Calendar calendar = (Calendar) today.clone();
    calendar.set(Calendar.MONTH, 0);
    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 GetLastYearRightRange(Calendar today) {
    Calendar calendar = (Calendar) today.clone();
    calendar.set(Calendar.MONTH, 0);
    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:com.project.framework.util.DateUtils.java

/**
 * ???/*from  www .ja v  a2  s .c om*/
 * @param time
 * @return
 */
public static Date getBeforeDate(int time) {
    Calendar c = Calendar.getInstance();
    c.add(Calendar.MINUTE, (-1 * time));
    return c.getTime();
}

From source file:Main.java

static int dateDiff(int type, Calendar fromDate, Calendar toDate, boolean future) {
    int diff = 0;
    long savedDate = fromDate.getTimeInMillis();
    while ((future && !fromDate.after(toDate)) || (!future && !fromDate.before(toDate))) {
        savedDate = fromDate.getTimeInMillis();
        fromDate.add(type, future ? 1 : -1);
        diff++;/*w  w  w.j a  v a  2s .  c o m*/
    }
    diff--;
    fromDate.setTimeInMillis(savedDate);
    return diff;
}

From source file:TimeLib.java

/**
 * Increment a calendar by a given number of time units.
 * @param c the calendar to increment//from  w w w  .j  a v a2  s .  co  m
 * @param field the time unit to increment, one of the
 * {@link java.util.Calendar} fields, or one of the extended fields
 * provided by this class (MILLENIUM, CENTURY, or DECADE).
 * @param val the number of time units to increment by
 */
public static void increment(Calendar c, int field, int val) {
    if (isMultiYear(field)) {
        c.add(Calendar.YEAR, -field * val);
    } else {
        c.add(field, val);
    }
}

From source file:Main.java

public static boolean equalDate(String date) {
    Date todayDate = null;//from  ww  w .  j  a v a2s .  c  om
    Date inputDate = null;
    Date thirdDate = null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String today = sdf.format(new Date());
    Calendar cal = Calendar.getInstance();
    //      cal.add(Calendar.DATE, -1);      
    //      String today = sdf.format(cal.getTime());
    try {
        todayDate = sdf.parse(today);
        inputDate = sdf.parse(date);
        cal.setTime(inputDate);
        cal.add(Calendar.DATE, 3);
        thirdDate = cal.getTime();
        if (todayDate.compareTo(inputDate) >= 0 && todayDate.compareTo(thirdDate) < 0) {
            return true;
        } else {
            return false;
        }
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return false;
    }

}

From source file:com.cablevision.util.sso.UtilSSO.java

/**
 * Gets the date and time for the end of the Assertion time interval in
 * as specified by SAML v2.0 section 2.5.1.
 *
 * @return the date and time as a String
 *///from  w  w  w  . java2 s .c o m
public static String getNotOnOrAfterDateAndTime() {
    Calendar afterCal = Calendar.getInstance();
    afterCal.add(Calendar.MINUTE, ASSERTION_NOT_ON_OR_AFTER_MINUTES);
    return DATE_TIME_FORMAT.format(afterCal.getTime());
}

From source file:com.lk.ofo.util.DateUtil.java

/**
 * /*from   w  w w  .java  2 s.  com*/
 *
 * @param date
 * @param changeValue ?
 * @return
 */
public static Date changeDay(Date date, int changeValue) {
    if (date == null || changeValue == 0) {
        return date;
    }
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    c.add(Calendar.DAY_OF_YEAR, changeValue);
    return c.getTime();
}