Example usage for java.util GregorianCalendar roll

List of usage examples for java.util GregorianCalendar roll

Introduction

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

Prototype

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

Source Link

Document

Adds a signed amount to the specified calendar field without changing larger fields.

Usage

From source file:MainClass.java

public static void main(String[] a) {
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.roll(Calendar.MONTH, false); // Go back a month
    System.out.println(calendar.get(Calendar.MONTH));
}

From source file:DateComboBoxRenderer.java

public static void main(String[] str) {
    JComboBox combo = new JComboBox();
    GregorianCalendar calendar = new GregorianCalendar();
    combo.addItem(calendar.getTime());/*from  ww  w .j a va2  s.c om*/

    calendar.roll(GregorianCalendar.DAY_OF_MONTH, 1);
    combo.addItem(calendar.getTime());

    combo.setRenderer(new DateComboBoxRenderer());

    JFrame frame = new JFrame();

    JPanel panel = new JPanel();
    panel.add(new JLabel("Date Combo: "));
    panel.add(combo);

    frame.add(panel);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {

    GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();

    // print the current date and time
    System.out.println(cal.getTime());

    // roll a month
    cal.roll(GregorianCalendar.MONTH, true);
    System.out.println("Date:" + cal.getTime());

    // roll a year backwards
    cal.roll(GregorianCalendar.YEAR, false);
    System.out.println("Date:" + cal.getTime());

}

From source file:Main.java

public static void main(String[] args) {

    GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();

    // print the current date and time
    System.out.println(cal.getTime());

    // roll 2 months
    cal.roll(GregorianCalendar.MONTH, 2);
    System.out.println("Date:" + cal.getTime());

    // roll 5 year backwards
    cal.roll(GregorianCalendar.YEAR, -5);
    System.out.println("Date:" + cal.getTime());

}

From source file:MainClass.java

public static void main(String[] args) throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
    GregorianCalendar gc = new GregorianCalendar();
    java.util.Date d = sdf.parse("12/12/2003");
    gc.setTime(d);/*from  ww w  .  j a va 2s  . c o m*/
    System.out.println("Input Date = " + sdf.format(d));
    int dayBefore = gc.get(Calendar.DAY_OF_YEAR);
    gc.roll(Calendar.DAY_OF_YEAR, -1);
    int dayAfter = gc.get(Calendar.DAY_OF_YEAR);
    if (dayAfter > dayBefore) {
        gc.roll(Calendar.YEAR, -1);
    }
    gc.get(Calendar.DATE);
    java.util.Date yesterday = gc.getTime();
    System.out.println("Yesterdays Date = " + sdf.format(yesterday));

}

From source file:com.saax.gestorweb.util.DAOAleatorio.java

public static Date getDataByOffset(int offsetDataAtual, boolean up) {
    GregorianCalendar gc = new GregorianCalendar();
    Date hoje = DateUtils.truncate(new Date(), Calendar.DATE);
    gc.setTime(hoje); // hoje truncando as horas

    for (int i = 0; i < offsetDataAtual; i++) {
        gc.roll(GregorianCalendar.DAY_OF_MONTH, up);
        if (gc.getActualMaximum(GregorianCalendar.DAY_OF_MONTH) == gc.get(GregorianCalendar.DAY_OF_MONTH)) {
            gc.roll(GregorianCalendar.MONTH, up);

        }/*from   w  w  w  .  j av  a  2  s.  c o  m*/
    }
    return DateUtils.truncate(new Date(gc.getTimeInMillis()), Calendar.DATE);

}

From source file:DateUtility.java

/**
 * Gets the last millsecond of the month, according to the timestamp and 
 * timezone arguments./*w w  w .j  a va  2  s  .  c  o m*/
 * @param timestamp
 * @param tz
 * @return long
 */
static public long getLastMilliOfMonth(long timestamp, TimeZone tz) {
    timestamp = getFirstMilliOfMonth(timestamp, tz);

    GregorianCalendar cal = new GregorianCalendar();
    cal.setTimeZone(tz);
    cal.setTime(new Date(timestamp));

    // now we'll roll the calendar forward one month.
    // in the case of december, we need to roll forward the year too    
    if (cal.get(GregorianCalendar.MONTH) == GregorianCalendar.DECEMBER) {
        cal.roll(GregorianCalendar.YEAR, true);
    }

    cal.roll(GregorianCalendar.MONTH, true);

    long date = cal.getTime().getTime();

    date = date - 1L;
    return date;
}

From source file:edu.duke.cabig.c3pr.web.DashboardController.java

private void getMostEnrolledStudies(HttpServletRequest request) {
    GregorianCalendar cal = new GregorianCalendar();
    Date endDate = new Date(System.currentTimeMillis());
    cal.setTime(endDate);/*from   w  w  w . j  a va 2 s  .co m*/
    cal.roll(Calendar.DATE, -6);
    Date startDate = new Date(cal.getTime().getTime());

    List<Study> studiesFound = studySubjectDao.getMostEnrolledStudies(startDate, endDate);
    List<Study> studies = new ArrayList<Study>();
    for (int i = 0; i < studiesFound.size() && i < MAX_RESULTS; i++) {
        studies.add(studiesFound.get(i));
    }
    log.debug("Most enrolled studies found: " + studies.size());
    request.setAttribute("aStudies", studies);
}

From source file:com.projity.interval.ValueObjectForIntervalTable.java

/**
 * Create a new entry one year later//from www  .  ja va2 s.c  o  m
 */
public Object createUnvalidatedObject(NodeModel nodeModel, Object parent) {
    long baseDate = DateTime.midnightToday();
    ValueObjectForInterval last = (ValueObjectForInterval) valueObjects.get(valueObjects.size() - 1); // get last one
    baseDate = Math.max(baseDate, last.getStart()); // latest of today or last entry
    GregorianCalendar cal = DateTime.calendarInstance();
    cal.setTimeInMillis(baseDate);
    cal.roll(GregorianCalendar.YEAR, true); // one year later than last one's start or today
    long date = cal.getTimeInMillis();
    try {
        return newValueObject(date);
    } catch (InvalidValueObjectForIntervalException e) {
        // TODO Auto-generated catch block
        e.printStackTrace(); // should not ever happen
        return null;
    }
}

From source file:edu.uga.cs.fluxbuster.features.FeatureCalculator.java

/**
 * Gets run dates previous to a specific date within a window
 * of days from that date./*  w w w  .  j  a va2  s . c o  m*/
 *
 * @param log_date the run date
 * @param window the number of days previous to the current date
 * @return the list of previous run dates
 * @throws SQLException if there is an error retrieving the previous
 *       run dates
 */
public ArrayList<Date> getPrevDates(Date log_date, int window) throws SQLException {
    ArrayList<Date> prevDates = new ArrayList<Date>();
    if (prevDateBufDate != null && prevDateBuf != null && prevDateBufDate.equals(log_date)
            && prevDateBufWindow >= window) {

        //pull the dates within the day window from the prevDateBuf cache
        Date pd = null;
        int windowcount = 0;
        for (Date d : prevDateBuf) {
            if (windowcount >= window) {
                break;
            }
            if (pd == null) {
                pd = d;
                windowcount++;
            } else {
                DateTime morerecent = new DateTime(d.getTime());
                DateTime lessrecent = new DateTime(pd.getTime());
                Days days = Days.daysBetween(morerecent, lessrecent);
                windowcount += days.getDays();
                pd = d;
            }
            prevDates.add(d);
        }

    } else {
        String domainsprefix = properties.getProperty(DOMAINSPREFIXKEY);
        String resipsprefix = properties.getProperty(RESIPSPREFIXKEY);

        ArrayList<String> tablenames = new ArrayList<String>();
        ResultSet rs1 = null;
        try {
            rs1 = dbi.executeQueryWithResult(properties.getProperty(TABLES_QUERY1KEY));
            while (rs1.next()) {
                tablenames.add(rs1.getString(1));
            }
        } catch (Exception e) {
            if (log.isErrorEnabled()) {
                log.error(e);
            }
        } finally {
            if (rs1 != null && !rs1.isClosed()) {
                rs1.close();
            }
        }

        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(log_date);
        for (int i = 0; i < window; i++) {
            cal.roll(Calendar.DAY_OF_YEAR, false);
            Date temp = cal.getTime();
            String datestr = df.format(temp);
            if (tablenames.contains(domainsprefix + "_" + datestr)
                    && tablenames.contains(resipsprefix + "_" + datestr)) {
                prevDates.add(temp);
            }
        }

        //cache the values for later
        if (prevDateBuf == null) {
            prevDateBuf = new ArrayList<Date>();
        } else {
            prevDateBuf.clear();
        }
        prevDateBuf.addAll(prevDates);
        prevDateBufDate = log_date;
        prevDateBufWindow = window;
    }
    return prevDates;
}