I need to get the first date (as org.joda.time.LocalDate
) of a month and the last one. Getting the first is trivial, but getting the last seems to need some logic as months have different length and February length even varies over years. Is there a mechanism for this already built in to JodaTime or should I implement it myself?
Asked
Active
Viewed 6.2k times
116

Ivan
- 63,011
- 101
- 250
- 382
-
2Just a heads-up, this also works with `DateTime` types :) – vikingsteve Sep 26 '14 at 11:14
4 Answers
234
How about:
LocalDate endOfMonth = date.dayOfMonth().withMaximumValue();
dayOfMonth()
returns a LocalDate.Property
which represents the "day of month" field in a way which knows the originating LocalDate
.
As it happens, the withMaximumValue()
method is even documented to recommend it for this particular task:
This operation is useful for obtaining a LocalDate on the last day of the month, as month lengths vary.
LocalDate lastDayOfMonth = dt.dayOfMonth().withMaximumValue();
-
1
-
8@WarrenM.Nocos: I'd use `dt.with(TemporalAdjusters.lastDayOfMonth())` – Jon Skeet Nov 09 '15 at 12:38
3
Another simple method is this:
//Set the Date in First of the next Month:
answer = new DateTime(year,month+1,1,0,0,0);
//Now take away one day and now you have the last day in the month correctly
answer = answer.minusDays(1);

Abraham Maldonado Barrios
- 55
- 1
- 7
-
5
-
The JodaTime API is a sophisticated, fully-loaded, and convenient piece of work. There are many other, more correct ways of accomplishing this. – aaiezza Jun 13 '18 at 18:10
-
If the month is 12, you know the last day is 31 right? just put something like this: if(month<12) { answer = new DateTime(year,month+1,1,0,0,0); answer = answer.minusDays(1); } else answer = 31; – Abraham Maldonado Barrios Nov 07 '18 at 14:15
1
An old question, but a top google result when I was looking for this.
If someone needs the actual last day as an int
instead using JodaTime you can do this:
public static final int JANUARY = 1;
public static final int DECEMBER = 12;
public static final int FIRST_OF_THE_MONTH = 1;
public final int getLastDayOfMonth(final int month, final int year) {
int lastDay = 0;
if ((month >= JANUARY) && (month <= DECEMBER)) {
LocalDate aDate = new LocalDate(year, month, FIRST_OF_THE_MONTH);
lastDay = aDate.dayOfMonth().getMaximumValue();
}
return lastDay;
}

wiredniko
- 2,491
- 3
- 19
- 37
-
1I would've preferred a slightly cleaner answer, like: public static int getLastDayOfMonth(int year, int month){ LocalDate date = new LocalDate(year, month, 1 return date.dayOfMonth().getMaximumValue(); } But your answer is super helpful, even if slightly cluttered, so +1 ;) – jumps4fun Oct 18 '18 at 08:40
-1
Using JodaTime,we can do this :
public static final Integer CURRENT_YEAR = DateTime.now().getYear(); public static final Integer CURRENT_MONTH = DateTime.now().getMonthOfYear(); public static final Integer LAST_DAY_OF_CURRENT_MONTH = DateTime.now() .dayOfMonth().getMaximumValue(); public static final Integer LAST_HOUR_OF_CURRENT_DAY = DateTime.now() .hourOfDay().getMaximumValue(); public static final Integer LAST_MINUTE_OF_CURRENT_HOUR = DateTime.now().minuteOfHour().getMaximumValue(); public static final Integer LAST_SECOND_OF_CURRENT_MINUTE = DateTime.now().secondOfMinute().getMaximumValue(); public static DateTime getLastDateOfMonth() { return new DateTime(CURRENT_YEAR, CURRENT_MONTH, LAST_DAY_OF_CURRENT_MONTH, LAST_HOUR_OF_CURRENT_DAY, LAST_MINUTE_OF_CURRENT_HOUR, LAST_SECOND_OF_CURRENT_MINUTE); }
As describe here in my small gist on github : A JodaTime and java.util.Date Util Class with a lot of usefull functions.

Orleando Dassi
- 454
- 6
- 17