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:com.kongwu.insweb.utils.DateUtils.java

public static Date addMonth(Date dtime, int month) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(dtime);//from   w ww .j  a  v  a2  s.co  m
    calendar.add(Calendar.MONTH, month);
    return calendar.getTime();
}

From source file:net.kamhon.ieagle.util.DateUtil.java

public static Date getStartDateOfWeek(Date date) {
    Calendar calendarStartDate = DateUtil.setTime(date);
    Integer noOfDayOfWeek = calendarStartDate.get(Calendar.DAY_OF_WEEK) - 1;
    calendarStartDate.add(Calendar.DATE, -noOfDayOfWeek);

    return calendarStartDate.getTime();
}

From source file:Main.java

public static java.util.Date getDate(int y, int m, int d, boolean inclusive) {
    java.util.Date dt = null;//from   w  ww .j  a  va2  s  .c  om
    Calendar c = Calendar.getInstance();
    c.clear();
    if (c.getActualMinimum(1) <= y && y <= c.getActualMaximum(1)) {
        c.set(1, y);
        if (c.getActualMinimum(2) <= m && m <= c.getActualMaximum(2)) {
            c.set(2, m);
            if (c.getActualMinimum(5) <= d && d <= c.getActualMaximum(5))
                c.set(5, d);
        }
        if (inclusive) {
            c.add(5, 1);
            c.add(14, -1);
        }
        dt = c.getTime();
    }
    return dt;
}

From source file:com.aurel.track.dbase.jobs.EmailReminderJob.java

private static Date getDailyReminderEndDateLimit(Integer lead) {
    Calendar calendar = Calendar.getInstance();
    CalendarUtil.clearTime(calendar);//www.j  a  va 2  s  .co m
    if (lead == null) {
        lead = Integer.valueOf(1);
    }
    calendar.add(Calendar.DATE, lead);
    return calendar.getTime();
}

From source file:com.mycompany.craftdemo.utility.java

public static boolean isValidTime() {
    Calendar calendar = Calendar.getInstance();
    TimeZone fromTimeZone = calendar.getTimeZone();
    TimeZone toTimeZone = TimeZone.getTimeZone("EST");

    calendar.setTimeZone(fromTimeZone);//from   w  ww .j  a  v  a2 s  .c  om
    calendar.add(Calendar.MILLISECOND, fromTimeZone.getRawOffset() * -1);
    if (fromTimeZone.inDaylightTime(calendar.getTime())) {
        calendar.add(Calendar.MILLISECOND, calendar.getTimeZone().getDSTSavings() * -1);
    }

    calendar.add(Calendar.MILLISECOND, toTimeZone.getRawOffset());
    if (toTimeZone.inDaylightTime(calendar.getTime())) {
        calendar.add(Calendar.MILLISECOND, toTimeZone.getDSTSavings());
    }

    int hour = calendar.get(Calendar.HOUR_OF_DAY);

    return hour >= 9 && hour <= 16;
}

From source file:com.t3.persistence.FileUtil.java

public static void delete(File file, int daysOld) throws IOException {
    Calendar olderThan = new GregorianCalendar();
    olderThan.add(Calendar.DATE, -daysOld);

    boolean shouldDelete = new Date(file.lastModified()).before(olderThan.getTime());

    if (file.isDirectory()) {
        // Wipe the contents first
        for (File currfile : file.listFiles()) {
            if (".".equals(currfile.getName()) || "..".equals(currfile.getName()))
                continue;
            delete(currfile, daysOld);//  ww w .j  a va 2s  .  c o m
        }
    }
    if (shouldDelete)
        file.delete();
}

From source file:com.keybox.manage.db.SessionAuditDB.java

/**
 * deletes audit history for users if after time set in properties file
 *
 * @param con DB connection/* w w w  . j  a  v a 2  s  .  c  o  m*/
 */
public static void deleteAuditHistory(Connection con) {

    try {

        //delete logs with no terminal entries
        PreparedStatement stmt = con.prepareStatement(
                "delete from session_log where id not in (select session_id from terminal_log)");
        stmt.execute();

        //take today's date and subtract how many days to keep history
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, (-1 * Integer.parseInt(AppConfig.getProperty("deleteAuditLogAfter")))); //subtract
        java.sql.Date date = new java.sql.Date(cal.getTimeInMillis());

        stmt = con.prepareStatement("delete from session_log where session_tm < ?");
        stmt.setDate(1, date);
        stmt.execute();

        DBUtils.closeStmt(stmt);

    } catch (Exception e) {
        log.error(e.toString(), e);
    }

}

From source file:net.granoeste.commons.util.DateUtils.java

/**
 * ?????/* ww w  .j av  a2  s.c  om*/
 *
 * @param cal
 * @param amount ex) last month : -1. the current month : 0. the next month : 1
 */
public static void shiftDateOnMondayOfAWeekAtTheBeginningOfTheMonth(final Calendar cal, final int amount) {

    cal.set(Calendar.DATE, 1);
    cal.add(Calendar.MONTH, amount);
    if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
        cal.add(Calendar.DATE, -6);
    } else {
        cal.add(Calendar.DATE, -cal.get(Calendar.DAY_OF_WEEK) + 2);
    }
}

From source file:net.granoeste.commons.util.DateUtils.java

/**
 * ????/*from  w w w  .  j av  a 2s .c o  m*/
 *
 * @param cal
 * @param amount ex) last month : -1. the current month : 0. the next month : 1
 */
public static void shiftDateOnSundayOfTheWeekOfTheEndOfTheMonth(final Calendar cal, final int amount) {

    cal.set(Calendar.DATE, 1);
    cal.add(Calendar.MONTH, amount);
    cal.add(Calendar.DATE, -1);
    if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
        cal.add(Calendar.DATE, 1);
    } else {
        cal.add(Calendar.DATE, 7 - cal.get(Calendar.DAY_OF_WEEK) + 1);
    }
}

From source file:freebase.api.FreebaseAPIMusic.java

public static void FetchingFromGoogleAPI() {

    int max_step = 1;
    int step = 0;

    int month_step = 1;
    Calendar last_date = Calendar.getInstance();
    last_date.set(Calendar.YEAR, 2016);
    last_date.set(Calendar.MONTH, 0);
    last_date.set(Calendar.DAY_OF_MONTH, 1);

    Calendar from_date = Calendar.getInstance();
    from_date.set(Calendar.YEAR, 2000);
    from_date.set(Calendar.MONTH, 0);
    from_date.set(Calendar.DAY_OF_MONTH, 1);

    Calendar to_date = (Calendar) from_date.clone();
    to_date.add(Calendar.MONTH, month_step);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    while (true) {
        //            if (step > max_step) {
        //                break;
        //            }
        //            System.out.println(">>> Cursor = " + current_cursor);
        //            if (/*films == null || films.isEmpty() ||*/step == max_step) {
        //                System.out.println("Films is null or zero size. Break.");
        //                break;
        //            }
        if (from_date.after(last_date)) {
            System.out.println("Date is after last date");
            break;
        }/*w w w . ja  v a  2s . co m*/
        String fromDate = sdf.format(from_date.getTime());
        String toDate = sdf.format(to_date.getTime());
        System.out.println(step + " ) fetch films from " + fromDate + " to " + toDate);
        step++;
        List<Recording> recordings = getRecording(fromDate, toDate);
        System.out.println("Films#: " + recordings.size());
        writeToFiles(recordings);
        //            for (Film f : films) {
        //                System.out.println(f);
        //            }
        from_date.add(Calendar.MONTH, month_step);
        to_date.add(Calendar.MONTH, month_step);

    }
}