Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The <a href="http://java.sun.com/javase/6/docs/api/java/util/Calendar.html" rel="noreferrer"><code>Calendar</code></a> object has an <a href="http://java.sun.com/javase/6/docs/api/java/util/Calendar.html#add(int,%20int)" rel="noreferrer"><code>add</code></a> method which allows one to add or subtract values of a specified field.</p> <p>For example,</p> <pre><code>Calendar c = new GregorianCalendar(2009, Calendar.JANUARY, 1); c.add(Calendar.DAY_OF_MONTH, 1); </code></pre> <p>The constants for specifying the field can be found in the "Field Summary" of the <a href="http://java.sun.com/javase/6/docs/api/java/util/Calendar.html" rel="noreferrer"><code>Calendar</code></a> class.</p> <p>Just for future reference, <a href="http://java.sun.com/javase/6/docs/api/" rel="noreferrer">The Java API Specification</a> contains a lot of helpful information about how to use the classes which are part of the Java API.</p> <hr> <p><strong>Update:</strong></p> <blockquote> <p>I am getting the error found void but expected int, in 'newDay = startDate.add(5, 1);' What should I do?</p> </blockquote> <p>The <code>add</code> method does not return anything, therefore, trying to assign the result of calling <code>Calendar.add</code> is not valid.</p> <p>The compiler error indicates that one is trying to assign a <code>void</code> to a variable with the type of <code>int</code>. This is not valid, as one cannot assign "nothing" to an <code>int</code> variable.</p> <p>Just a guess, but perhaps this may be what is trying to be achieved:</p> <pre><code>// Get a calendar which is set to a specified date. Calendar calendar = new GregorianCalendar(2009, Calendar.JANUARY, 1); // Get the current date representation of the calendar. Date startDate = calendar.getTime(); // Increment the calendar's date by 1 day. calendar.add(Calendar.DAY_OF_MONTH, 1); // Get the current date representation of the calendar. Date endDate = calendar.getTime(); System.out.println(startDate); System.out.println(endDate); </code></pre> <p>Output:</p> <pre><code>Thu Jan 01 00:00:00 PST 2009 Fri Jan 02 00:00:00 PST 2009 </code></pre> <p>What needs to be considered is what <code>Calendar</code> actually is.</p> <p>A <code>Calendar</code> is not a representation of a date. It is a representation of a calendar, and where it is currently pointing at. In order to get a representation of where the calendar is pointing at at the moment, one should obtain a <a href="http://java.sun.com/javase/6/docs/api/java/util/Date.html" rel="noreferrer"><code>Date</code></a> from the <code>Calendar</code> using the <a href="http://java.sun.com/javase/6/docs/api/java/util/Calendar.html#getTime()" rel="noreferrer"><code>getTime</code></a> method.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload