How do I get the first day of the next month in Joda?

This example will give you the first day of the next month from the current system date. In the example we use the MutableDateTime class. We add 1 month from the current date using the addMonths() method. Next we set the day of the month to the first day using the setDayOfMonth() method. To make sure it is the first day we set the time to midnight by calling the setMillisOfDay() method.

package org.kodejava.example.joda;

import org.joda.time.MutableDateTime;

public class FirstDayOfNextMonth {
    public static void main(String[] args) {
        // Creates an instance of MutableDateTime for the current
        // system date time.
        MutableDateTime dateTime = new MutableDateTime();
        System.out.println("dateTime = " + dateTime);

        // Find the first day of the next month can be done by:
        // 1. Add 1 month to the date
        // 2. Set the day of the month to 1
        // 3. Set the millis of day to 0.
        System.out.println("First day of next month = " + dateTime);

Here is an example result of the program:

dateTime = 2012-02-03T23:34:21.801+08:00
First day of next month = 2012-03-01T00:00:00.000+08:00

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.

Leave a Reply