Example usage for java.util GregorianCalendar add

List of usage examples for java.util GregorianCalendar add

Introduction

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

Prototype

@Override
public void add(int field, int amount) 

Source Link

Document

Adds the specified (signed) amount of time to the given calendar field, based on the calendar's rules.

Usage

From source file:DateUtil.java

/**
 * Roll the java.util.Time forward or backward.
 * @param startDate - The start date/*ww w . ja v a2s .  c o  m*/
 * @period Calendar.YEAR etc
 * @param amount - Negative to rollbackwards.
 */
public static java.sql.Time rollTime(java.util.Date startDate, int period, int amount) {
    GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(startDate);
    gc.add(period, amount);
    return new java.sql.Time(gc.getTime().getTime());
}

From source file:DateUtil.java

/**
 * Roll the java.util.Date forward or backward.
 * @param startDate - The start date//from  w ww.  j  av a 2 s. c o m
 * @period Calendar.YEAR etc
 * @param amount - Negative to rollbackwards.
 */
public static java.util.Date rollDateTime(java.util.Date startDate, int period, int amount) {
    GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(startDate);
    gc.add(period, amount);
    return new java.util.Date(gc.getTime().getTime());
}

From source file:DateUtil.java

/**
 * Roll the java.sql.Date forward or backward.
 * @param startDate - The start date/*from ww  w .  j a v a2s  .  co m*/
 * @period Calendar.YEAR etc
 * @param amount - Negative to rollbackwards.
 */
public static java.sql.Date rollDate(java.util.Date startDate, int period, int amount) {
    GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(startDate);
    gc.add(period, amount);
    return new java.sql.Date(gc.getTime().getTime());
}

From source file:Main.java

@SuppressWarnings("deprecation")
public static String changeDateAsMonth(String date, int imonth) {
    GregorianCalendar calendar = new GregorianCalendar();
    if (date.length() == 8) {
        calendar.set(Integer.parseInt(date.substring(0, 4)), Integer.parseInt(date.substring(4, 6)) - 1,
                Integer.parseInt(date.substring(6, 8)));
    } else if (date.length() == 10) {
        calendar.set(Integer.parseInt(date.substring(0, 4)), Integer.parseInt(date.substring(5, 7)) - 1,
                Integer.parseInt(date.substring(8, 10)));
    }/* w  w w .ja  v  a  2s.c  om*/
    calendar.add(GregorianCalendar.MONTH, imonth);
    String sDate[] = calendar.getTime().toLocaleString().split(" ");
    return sDate[0];
}

From source file:com.microsoftopentechnologies.azchat.web.common.utils.AzureChatStorageUtils.java

/**
 * This method will assign private access to the blob container.
 * // ww  w  .  ja  v a  2s .c  om
 * @param container
 * @return
 * @throws Exception
 */
public static String generateSASURL(CloudBlobContainer container) throws Exception {
    LOGGER.info("[AzureChatStorageUtils][assignPrivateAccess] start");
    String signature = AzureChatConstants.CONSTANT_EMPTY_STRING;
    SharedAccessBlobPolicy sharedAccessBlobPolicy = new SharedAccessBlobPolicy();
    GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone(AzureChatConstants.TIMEZONE_UTC));
    calendar.setTime(new Date());
    sharedAccessBlobPolicy.setSharedAccessStartTime(calendar.getTime());
    calendar.add(Calendar.HOUR, 23);
    sharedAccessBlobPolicy.setSharedAccessExpiryTime(calendar.getTime());
    sharedAccessBlobPolicy.setPermissions(EnumSet.of(SharedAccessBlobPermissions.READ));
    BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
    container.uploadPermissions(containerPermissions);
    LOGGER.debug("Private Access Permissions uploaded Successfully.");
    signature = container.generateSharedAccessSignature(sharedAccessBlobPolicy, null);
    LOGGER.info("[AzureChatStorageUtils][assignPrivateAccess] end");
    return signature;
}

From source file:Main.java

private static void subtractField(GregorianCalendar g1, GregorianCalendar g2, int field) {
    //      int value1 = g1.get( field );
    int value2 = g2.get(field);

    if (field == Calendar.DAY_OF_MONTH)
        value2 -= 1;//from w  ww .  j a  va 2  s.c  o  m
    g1.add(field, -value2);
}

From source file:DateUtils.java

public static String[] getDateRange(int cmd) {
    GregorianCalendar gc = new GregorianCalendar();
    GregorianCalendar gc2 = new GregorianCalendar();

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); //$NON-NLS-1$
    String ret[] = new String[2];
    ret[1] = sdf.format(gc.getTime());/*  w  w  w.  j  a va  2s.c  o m*/

    if (cmd == LAST_WEEK) {
        for (int i = 0; i < 7; i++) {
            gc2.add(Calendar.DATE, -1);
        }

    }
    if (cmd == LAST_MONTH) {
        gc2.add(Calendar.MONTH, -1);
    }
    ret[0] = sdf.format(gc2.getTime());
    return ret;
}

From source file:org.mifos.calendar.CalendarUtils.java

public static DateTime getNextDayForMonthUsingWeekRankAndWeekday(final DateTime startDate,
        final int weekOfMonth, final int dayOfWeek, final int every) {

    /*//from w  ww .j  a  va 2  s  . c om
     * In Joda time MONDAY=1 and SUNDAY=7, so shift these to SUNDAY=1, SATURDAY=7 to match this class
     */
    int calendarDayOfWeek = (dayOfWeek % 7) + 1;

    final GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(startDate.toDate());

    DateTime scheduleDate;

    if (!RankOfDay.getRankOfDay(weekOfMonth).equals(RankOfDay.LAST)) {
        // apply month recurrence
        gc.add(GregorianCalendar.MONTH, every);
        gc.set(Calendar.DAY_OF_WEEK, calendarDayOfWeek);
        gc.set(GregorianCalendar.DAY_OF_WEEK_IN_MONTH, weekOfMonth);
        scheduleDate = new DateTime(gc.getTime().getTime());
    } else {// weekCount=-1
        gc.set(GregorianCalendar.DATE, 15);
        gc.add(GregorianCalendar.MONTH, every);
        gc.set(Calendar.DAY_OF_WEEK, calendarDayOfWeek);
        int M1 = gc.get(GregorianCalendar.MONTH);
        // assumption: there are 5 weekdays in the month
        gc.set(GregorianCalendar.DAY_OF_WEEK_IN_MONTH, 5);
        int M2 = gc.get(GregorianCalendar.MONTH);
        // if assumption fails, it means there exists 4 weekdays in a
        // month, return last weekday date
        // if M1==M2, means there exists 5 weekdays otherwise 4 weekdays
        // in a month
        if (M1 != M2) {
            gc.set(GregorianCalendar.MONTH, gc.get(GregorianCalendar.MONTH) - 1);
            gc.set(GregorianCalendar.DAY_OF_WEEK_IN_MONTH, 4);
        }
        scheduleDate = new DateTime(gc.getTime().getTime());
    }

    return scheduleDate;
}

From source file:org.openvpms.archetype.rules.util.DateRules.java

/**
 * Calculates a date given a start time, interval and the date units.
 *
 * @param startTime the start time/*from ww  w.j  av a 2 s .  c  o m*/
 * @param interval  the time interval. May be negative to calculate a date in the past
 * @param units     the interval units
 * @return the date
 */
public static Date getDate(Date startTime, int interval, DateUnits units) {
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.setTime(startTime);
    if (units != null) {
        switch (units) {
        case YEARS:
            calendar.add(Calendar.YEAR, interval);
            break;
        case MONTHS:
            calendar.add(Calendar.MONTH, interval);
            break;
        case WEEKS:
            calendar.add(Calendar.DAY_OF_YEAR, interval * 7);
            break;
        case DAYS:
            calendar.add(Calendar.DAY_OF_YEAR, interval);
            break;
        case HOURS:
            calendar.add(Calendar.HOUR_OF_DAY, interval);
            break;
        case MINUTES:
            calendar.add(Calendar.MINUTE, interval);
            break;
        }
    }
    return calendar.getTime();
}

From source file:org.mifos.calendar.CalendarUtils.java

/**
 * for monthly on date return the next date falling on the same day. If date has passed, pass in the date of next
 * month, adjust to day number if day number exceed total number of days in month
 *//*from  www.  j  a  v  a2s . c  o m*/
public static DateTime getFirstDateForMonthOnDate(final DateTime startDate, final int dayOfMonth) {

    final GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(startDate.toDate());

    int dt = gc.get(GregorianCalendar.DATE);
    // if date passed in, is after the date on which schedule has to
    // lie, move to next month
    if (dt > dayOfMonth) {
        gc.add(GregorianCalendar.MONTH, 1);
    }
    // set the date on which schedule has to lie
    int M1 = gc.get(GregorianCalendar.MONTH);
    gc.set(GregorianCalendar.DATE, dayOfMonth);
    int M2 = gc.get(GregorianCalendar.MONTH);

    int daynum = dayOfMonth;
    while (M1 != M2) {
        gc.set(GregorianCalendar.MONTH, gc.get(GregorianCalendar.MONTH) - 1);
        gc.set(GregorianCalendar.DATE, daynum - 1);
        M2 = gc.get(GregorianCalendar.MONTH);
        daynum--;
    }

    return new DateTime(gc.getTime());
}