How do I get the last day of a month in Joda?

This example show you how to use Joda-Time’s DateTime class to get the last day of the month. We use the dayOfMonth() property of this object an get the maximum value using the withMaximumValue() method which give us the last day of a month.

package org.kodejava.example.joda;

import org.joda.time.DateTime;

public class LastDayOfTheMonth {
    public static void main(String[] args) {
        //
        // Creates an instance of DateTime.
        //
        DateTime dateTime = new DateTime();

        //
        // Get the last date of the month using the dayOfMonth property
        // and get the maximum value from it.
        //
        DateTime lastDate = dateTime.dayOfMonth().withMaximumValue();

        //
        // Print the date and day name.
        //
        System.out.println("Date = " + lastDate.toString());
        System.out.println("Day  = " + lastDate.dayOfWeek().getAsText());
    }
}

The example output is:

Date = 2012-02-29T23:41:12.231+08:00
Day  = Wednesday
Wayan Saryada

Wayan Saryada

A programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, JPA, etc. If you need help on Java programming you can hire me on Fiverr.
Wayan Saryada

Leave a Reply