How to get the last date of a particular month with JodaTime?

jodatime get first day of month
java get last day of month
joda time get previous month
joda datetime get number of days in month
get first and last date of month in java 8
android get end of month
localdate get last day of month
get last month date in java

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?

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();

How to get the last date of a particular month with JodaTime?, How about: LocalDate endOfMonth = date.dayOfMonth().withMaximumValue();. dayOfMonth() returns a LocalDate.Property which represents  Questions: 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

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);

How to get the last date of a particular month with JodaTime?, get first day and last day of month using jodatime. gistfile1.java. DateTimeFormatter formatterFecha = DateTimeFormat.forPattern("dd/MM/yyyy");. DateTime  In Joda-Time the date and time information are divided into fields. The following example show you some fields that can be obtained from the DateTime object. For example to get the day of the year we use the getDayOfYear() method and to get the day of week we can use the getDayOfWeek() method.

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;
}

get first day and last day of month using jodatime · GitHub, Date calculations; Fetching first and last day of the month; Fetching the first and the last This class is used to fetch the current time for the specified time zone. Returns a copy of this month-day plus the specified number of months. This month-day instance is immutable and unaffected by this method call. The month will wrap at the end of the year from December to January.

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.

7.4. Date Operations (Joda Time), How do I obtain the amount of days of a given month with Jodatime? public static int Calcite expects "DATE" types to be number of days from the epoch to the UTC date matching the local time fields. Get Last Friday of Month in Java. I'm trying add month with JodaTime. I have a payment plan with some months, example: one month, two month, three month and six month. When I add six month at DateTime doesn't work and return an exception. I'm trying this.

org.joda.time.DateTime.dayOfMonth java code examples, Constructs an instance set to the specified date and time using ISOChronology Get the day of month property which provides access to advanced functionality. To translate a Joda-Time DateTime instance to a java.util.Date instance, simply call toDate method. No need for constructor on Date. No need for constructor on Date. Going the other way, if you hava a java.util.Date and want a Joda-Time DateTime, simply pass the Date to the constructor of DateTime along with the desired DateTimeZone object.

LocalDate (Joda time 2.2 API), Last Published: 2013-03-08 This is the quick introduction guide to Joda-Time and the features on offer. LocalTime - Immutable class representing a time without a date (no time zone) For example, to access the month you can use: In addition to the basic get methods, each datetime class provides property methods  The 2nd argument (months) of the EOMONTH function allows you to get the last day of the month in future or past months. When you use zero for months, EOMONTH will return the last day of the month in the same month.

Java date and time API - Quick start guide, A partial in Joda-Time is a partial date and time representation. A partial does not fully specify a single point in the datetime continuum, but instead may YearMonth - An immutable implementation that represents a year and month, useful for toDateTime(time); // extract the date fields from someDT DateTime someDT = . There is a function called DateTimeLastOfMonth(), which will return the last day of the current month. The issue is that there are no additional parameters for this particular function, such as the last day of a particular month.

Comments
  • Just a heads-up, this also works with DateTime types :)
  • @Jon Skeet How to get this using Java 8's new Date and Time API?
  • @WarrenM.Nocos: I'd use dt.with(TemporalAdjusters.lastDayOfMonth())
  • what happens then if your month is = 12?
  • The JodaTime API is a sophisticated, fully-loaded, and convenient piece of work. There are many other, more correct ways of accomplishing this.
  • 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;
  • I 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 ;)