<p>I'm trying to use the following code but it's returning the wrong day of month.</p>

<pre><code>Calendar cal = Calendar.getInstance();
cal.setTime(sampleDay.getTime());
cal.set(Calendar.MONTH, sampleDay.get(Calendar.MONTH)+1);
cal.set(Calendar.DAY_OF_MONTH, 0);
return cal.getTime();
</code></pre>

[{'answer_id': 72411, 'author': 'Stephen Wrighton', 'author_id': 7516, 'author_profile': 'https://Stackoverflow.com/users/7516', 'pm_score': 2, 'selected': False, 'text': '<p>I would create a date object for the first day of the NEXT month, and then just subtract a single day from the date object.</p>\n'}, {'answer_id': 72438, 'author': 'Argelbargel', 'author_id': 2992, 'author_profile': 'https://Stackoverflow.com/users/2992', 'pm_score': 5, 'selected': True, 'text': '<p>Get the number of days for this month:</p>\n\n<p><pre><code>\nCalendar cal = Calendar.getInstance();\ncal.setTime(sampleDay.getTime());\nint noOfLastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);\n</pre></code></p>\n\n<p>Set the Calendar  to the last day of this month:</p>\n\n<p><pre><code>\nCalendar cal = Calendar.getInstance();\ncal.setTime(sampleDay.getTime());\ncal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));\n</pre></code></p>\n'}, {'answer_id': 72460, 'author': 'Peter Hilton', 'author_id': 2670, 'author_profile': 'https://Stackoverflow.com/users/2670', 'pm_score': 2, 'selected': False, 'text': '<p>It looks like you set the calendar to the first day of the next month, so you need one more line to subtract one day, to get the last day of the month that <em>sampleDay</em> is in:</p>\n\n<pre><code>Calendar cal = Calendar.getInstance();\ncal.setTime(sampleDay.getTime());\ncal.roll(Calendar.MONTH, true);\ncal.set(Calendar.DAY_OF_MONTH, 0);\ncal.add(Calendar.DAY_OF_MONTH, -1);\n</code></pre>\n\n<p>In general, it\'s much easier to do this kind of thing using <a href="http://joda-time.sourceforge.net/" rel="nofollow noreferrer">Joda Time</a>, eg:</p>\n\n<pre><code>DateTime date = new DateTime(sampleDay.getTime());\nreturn date.plusMonths(1).withDayOfMonth(0).minusDays(1).getMillis();\n</code></pre>\n'}, {'answer_id': 72526, 'author': 'paul', 'author_id': 11249, 'author_profile': 'https://Stackoverflow.com/users/11249', 'pm_score': 2, 'selected': False, 'text': '<p>Use calObject.getActualMaximum(calobject.DAY_OF_MONTH)</p>\n\n<p>See <a href="http://www.rgagnon.com/javadetails/java-0098.html" rel="nofollow noreferrer">Real\'s Java How-to</a> for more info on this.</p>\n'}, {'answer_id': 4981993, 'author': 'John', 'author_id': 157080, 'author_profile': 'https://Stackoverflow.com/users/157080', 'pm_score': 1, 'selected': False, 'text': '<p>If you use the <a href="http://www.date4j.net" rel="nofollow">date4j</a> library:</p>\n\n<pre><code>DateTime monthEnd = dt.getEndOfMonth();\n</code></pre>\n'}, {'answer_id': 13409132, 'author': 'Nick', 'author_id': 1828375, 'author_profile': 'https://Stackoverflow.com/users/1828375', 'pm_score': -1, 'selected': False, 'text': "<p>I think this should work nicely:</p>\n\n<pre><code>Dim MyDate As Date = #11/14/2012#  'This is just an example date\n\nMyDate = MyDate.AddDays(DateTime.DaysInMonth(MyDate.Year, MyDate.Month) - MyDate.Day)\n</code></pre>\n"}, {'answer_id': 38032903, 'author': 'Basil Bourque', 'author_id': 642706, 'author_profile': 'https://Stackoverflow.com/users/642706', 'pm_score': 2, 'selected': False, 'text': '<h1>tl;dr</h1>\n\n<pre><code>YearMonth.from(\n    LocalDate.now( ZoneId.of( "America/Montreal" ) )\n).atEndOfMonth()\n</code></pre>\n\n<h1>java.time</h1>\n\n<p>The Question and other Answers use old outmoded classes. They have been supplanted by the <a href="http://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html" rel="nofollow noreferrer">java.time</a> classes built into Java 8 and later. See <a href="http://docs.oracle.com/javase/tutorial/datetime/TOC.html" rel="nofollow noreferrer">Oracle Tutorial</a>. Much of the functionality has been back-ported to Java 6 &amp; 7 in <a href="http://www.threeten.org/threetenbp/" rel="nofollow noreferrer">ThreeTen-Backport</a> and further adapted to Android in <a href="https://github.com/JakeWharton/ThreeTenABP" rel="nofollow noreferrer">ThreeTenABP</a>.</p>\n\n<h2><code>LocalDate</code></h2>\n\n<p>The <a href="http://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html" rel="nofollow noreferrer"><code>LocalDate</code></a> class represents a date-only value without time-of-day and without time zone. While these objects store no time zone, note that time zone is crucial in determining the current date. For any given moment the date varies around the globe by time zone.</p>\n\n<pre><code>ZoneId zoneId = ZoneId.of( "America/Montreal" );\nLocalDate today = LocalDate.now( zoneId );  // 2016-06-25\n</code></pre>\n\n<h2><code>YearMonth</code></h2>\n\n<p>Combine with the <a href="http://docs.oracle.com/javase/8/docs/api/java/time/YearMonth.html" rel="nofollow noreferrer"><code>YearMonth</code></a> class to determine last day of any month.</p>\n\n<pre><code>YearMonth currentYearMonth = YearMonth.from( today );  // 2016-06\nLocalDate lastDayOfCurrentYearMonth = currentYearMonth.atEndOfMonth();  // 2016-06-30\n</code></pre>\n\n<p>By the way, both <code>LocalDate</code> and <code>YearMonth</code> use month numbers as you would expect (1-12) rather than the screwball 0-11 seen in the old date-time classes. One of many poor design decisions that make those old date-time classes so troublesome and confusing.</p>\n\n<h1><code>TemporalAdjuster</code></h1>\n\n<p>Another valid approach is using a <a href="https://docs.oracle.com/javase/9/docs/api/java/time/temporal/TemporalAdjuster.html" rel="nofollow noreferrer"><code>TemporalAdjuster</code></a>.\n See <a href="https://stackoverflow.com/a/46914556/642706">the correct Answer by Pierre Henry</a>.</p>\n'}, {'answer_id': 46914556, 'author': 'Pierre Henry', 'author_id': 315677, 'author_profile': 'https://Stackoverflow.com/users/315677', 'pm_score': 2, 'selected': False, 'text': '<h1><code>TemporalAdjuster</code></h1>\n\n<p>Using the (relatively) <strong>new Java date API</strong>, it is actually very easy :</p>\n\n<p>Let <code>date</code> be an instance of <code>LocalDate</code>, for example :</p>\n\n<pre><code>LocalDate date = LocalDate.of(2018, 1, 22);\n</code></pre>\n\n<p>or</p>\n\n<pre><code>LocalDate date = LocalDate.now();\n</code></pre>\n\n<p>or, of course, you could get it as a user input, from a database, etc.</p>\n\n<p>Then apply an implementation of the <a href="https://docs.oracle.com/javase/9/docs/api/java/time/temporal/TemporalAdjuster.html" rel="nofollow noreferrer"><code>TemporalAdjuster</code></a> interface found in the <a href="https://docs.oracle.com/javase/9/docs/api/java/time/temporal/TemporalAdjusters.html" rel="nofollow noreferrer"><code>TemporalAdjusters</code></a> class:</p>\n\n<pre><code>LocalDate first = date.with(TemporalAdjusters.firstDayOfMonth());\nLocalDate last = date.with(TemporalAdjusters.lastDayOfMonth());\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/72381', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2443/']