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

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. MutableDateTime is a datetime class whose value can be modified.

In the code snippet we start by creating an instance of MutableDateTime class. We add 1 month from the current date using the addMonths() method. 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.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("Current date            = " + 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.
        dateTime.addMonths(1);
        dateTime.setDayOfMonth(1);
        dateTime.setMillisOfDay(0);
        System.out.println("First day of next month = " + dateTime);
    }
}

Here is an example result of the program:

Current date            = 2021-10-29T06:40:17.373+08:00
First day of next month = 2021-11-01T00:00:00.000+08:00

Maven Dependencies

<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.12.5</version>
</dependency>

Maven Central

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

This example show us how to use Joda-Time’s DateTime class to get the last day of the month. To get the last day of the month we first need to find out the last date of the month. To do this, create a new instance of a DateTime. From the DateTime object get the day-of-the-month property by calling the dayOfMonth() method. Then call the withMaximumValue() method which will give us the last date of a month.

To get the day name, we call the DateTime‘s dayOfWeek() method followed by a call to the getAsText() method to get the name of the day. Let’s see the code snippet below:

package org.kodejava.joda;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;

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

        // 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("Last date of the month = " + lastDate);
        System.out.println("Last day of the month  = " +
                lastDate.dayOfWeek().getAsText());

        // If you know the last date of the month you can simply parse the
        // date string and get the name of the last day of the month.
        String day = LocalDate.parse("2021-10-31").dayOfWeek().getAsText();
        System.out.println("Day  = " + day);
    }
}

If you know the last date of the month you can simply parse the date string to create a DateTime object or a LocalDate object and call the dayOfWeek() method and get the name of the day using getAsText() method.

The example output is:

Last date of the month = 2021-10-31T06:37:18.069+08:00
Last day of the month  = Sunday
Day  = Sunday

Maven Dependencies

<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.12.5</version>
</dependency>

Maven Central

How do I create DateTime object in Joda-Time?

The following example show you a various way to create an instance of Joda-Time’s DateTime class. By using the default constructor we will create an object with the current system date time. We can also create the object by passing the information like year, month, day, hour, minutes and second.

Joda can also use an instance from JDK’s java.util.Date and java.util.Calendar to create the DateTime. This means that the date object of JDK and Joda can be used to work together in our application.

package org.kodejava.joda;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;

import java.util.Calendar;
import java.util.Date;

public class DateTimeDemo {

    public static void main(String[] args) {
        // Creates DateTime object using the default constructor will
        // give you the current system date.
        DateTime date = new DateTime();
        System.out.println("date = " + date);

        // Or simply calling the now() method.
        date = DateTime.now();
        System.out.println("date = " + date);

        // Creates DateTime object with information like year, month,
        // day, hour, minute, second and milliseconds
        date = new DateTime(2021, 10, 29, 0, 0, 0, 0);
        System.out.println("date = " + date);

        // Create DateTime object from milliseconds.
        date = new DateTime(System.currentTimeMillis());
        System.out.println("date = " + date);

        // Create DateTime object from Date object.
        date = new DateTime(new Date());
        System.out.println("date = " + date);

        // Create DateTime object from Calendar object.
        Calendar calendar = Calendar.getInstance();
        date = new DateTime(calendar);
        System.out.println("date = " + date);

        // Create DateTime object from string. The format of the
        // string  should be precise.
        date = new DateTime("2021-10-29T06:30:00.000+08:00");
        System.out.println("date = " + date);
        date = DateTime.parse("2021-10-29");
        System.out.println("date = " + date);
        date = DateTime.parse("29/10/2021",
                DateTimeFormat.forPattern("dd/MM/yyyy"));
        System.out.println("date = " + date);
    }
}

The result of our code snippet:

date = 2021-10-29T06:30:01.068+08:00
date = 2021-10-29T06:30:01.147+08:00
date = 2021-10-29T00:00:00.000+08:00
date = 2021-10-29T06:30:01.148+08:00
date = 2021-10-29T06:30:01.148+08:00
date = 2021-10-29T06:30:01.166+08:00
date = 2021-10-29T06:30:00.000+08:00
date = 2021-10-29T00:00:00.000+08:00
date = 2021-10-29T00:00:00.000+08:00

Maven Dependencies

<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.12.5</version>
</dependency>

Maven Central

How do I use Joda-Time’s DateMidnight class?

The org.joda.time.DateMidnight class represent a date time information with the time value set to midnight. The following snippet show you how to instantiate this class.

package org.kodejava.joda;

import org.joda.time.DateMidnight;
import org.joda.time.format.DateTimeFormat;

public class DateMidnightDemo {
    public static void main(String[] args) {
        // Create DateMidnight object of the current system date.
        DateMidnight date = new DateMidnight();
        System.out.println("date = " + date);

        // Or using the now().
        date = DateMidnight.now();
        System.out.println("date = " + date);

        // Create DateMidnight object by year, month and day.
        date = new DateMidnight(2021, 10, 29);
        System.out.println("date = " + date);

        // Create DateMidnight object from milliseconds.
        date = new DateMidnight(System.currentTimeMillis());
        System.out.println("date = " + date);

        // Parse a date from string.
        date = DateMidnight.parse("2021-10-29");
        System.out.println("date = " + date);

        // Parse a date from string of specified patter.
        date = DateMidnight.parse("29/10/2021", 
                DateTimeFormat.forPattern("dd/MM/yyyy"));
        System.out.println("date = " + date);
    }
}

The result of our code snippet:

date = 2021-10-29T00:00:00.000+08:00
date = 2021-10-29T00:00:00.000+08:00
date = 2021-10-29T00:00:00.000+08:00
date = 2021-10-29T00:00:00.000+08:00
date = 2021-10-29T00:00:00.000+08:00
date = 2021-10-29T00:00:00.000+08:00

Maven Dependencies

<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.12.5</version>
</dependency>

Maven Central