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 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 date and time fields of DateTime object in Joda-Time?

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.

package org.kodejava.joda;

import org.joda.time.DateTime;

public class DateTimeFieldDemo {
    public static void main(String[] args) {
        DateTime dateTime = new DateTime();
        System.out.println("DateTime          = " + dateTime);

        // Get day of year, day of month, day of week and week of
        // year of a date.
        System.out.println("Day of Year       = " + dateTime.getDayOfYear());
        System.out.println("Day of Month      = " + dateTime.getDayOfMonth());
        System.out.println("Day of Week       = " + dateTime.getDayOfWeek());
        System.out.println("Week of Week year = " + dateTime.getWeekOfWeekyear());

        // Get hour of day, minute of hour and second of minute.
        System.out.println("Hour of Day       = " + dateTime.getHourOfDay());
        System.out.println("Minute of Hour    = " + dateTime.getMinuteOfHour());
        System.out.println("Second of Minute  = " + dateTime.getSecondOfMinute());

        // Get minute of day and second of day.
        System.out.println("Minute of Day     = " + dateTime.getMinuteOfDay());
        System.out.println("Second of Day     = " + dateTime.getSecondOfDay());
    }
}

The outputs of our program are:

DateTime          = 2021-10-29T06:46:49.259+08:00
Day of Year       = 302
Day of Month      = 29
Day of Week       = 5
Week of Week year = 43
Hour of Day       = 6
Minute of Hour    = 46
Second of Minute  = 49
Minute of Day     = 406
Second of Day     = 24409

Maven Dependencies

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

Maven Central

How do I format DateTime object in Joda-Time?

The example show you how to format the string representation of a date. In Joda we can use the DateTime‘s class toString() method. The method accept the pattern of the date format and the locale information.

package org.kodejava.joda;

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

import java.util.Locale;

public class FormattingDemo {
    // Define the date format pattern.
    private static final String pattern = "E MM/dd/yyyy HH:mm:ss.SSS";

    public static void main(String[] args) {
        // Creates a new instance of DateTime object.
        DateTime dt = DateTime.now();

        // Print out the date as a formatted string using the defined
        // Locale information.
        System.out.println("Pattern  = " + pattern);
        System.out.println("Default  = " + dt.toString(pattern));
        System.out.println("Germany  = " + dt.toString(pattern, Locale.GERMANY));
        System.out.println("French   = " + dt.toString(pattern, Locale.FRENCH));
        System.out.println("Japanese = " + dt.toString(pattern, Locale.JAPANESE));

        // Using predefined format from DateTimeFormat class.
        System.out.println("fullDate   = " + dt.toString(DateTimeFormat.fullDate()));
        System.out.println("longDate   = " + dt.toString(DateTimeFormat.longDate()));
        System.out.println("mediumDate = " + dt.toString(DateTimeFormat.mediumDate()));
        System.out.println("shortDate  = " + dt.toString(DateTimeFormat.shortDate()));
        System.out.println("dd/MM/yyyy = " + dt.toString(DateTimeFormat.forPattern("dd/MM/yyyy")));
    }
}

Here are the example result of our program:

Pattern  = E MM/dd/yyyy HH:mm:ss.SSS
Default  = Fri 10/29/2021 06:49:32.491
Germany  = Fr. 10/29/2021 06:49:32.491
French   = ven. 10/29/2021 06:49:32.491
Japanese = 金 10/29/2021 06:49:32.491
fullDate   = Friday, October 29, 2021
longDate   = October 29, 2021
mediumDate = Oct 29, 2021
shortDate  = 10/29/21
dd/MM/yyyy = 29/10/2021

Maven Dependencies

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

Maven Central

How do I do a date calculations in Joda-Time?

Using the Joda-Time library can simplify the date calculation process. For instance, we can add or subtract some days, weeks, months or year easily. There are a plus and minus method for this operation. For instance if you want to add 9 weeks to date object you can do something like date.plusWeeks(9).

package org.kodejava.joda;

import org.joda.time.LocalDate;

public class DateCalculationDemo {
    public static void main(String[] args) {
        // Creates an instance of LocalDate where we are going to do
        // some date calculations.
        LocalDate date = new LocalDate(2021, 10, 29);
        System.out.println("Date           = " + date);

        // Add days, weeks, months, year value into the date object.
        System.out.println("plusDays(10)   = " + date.plusDays(10));
        System.out.println("plusWeeks(9)   = " + date.plusWeeks(9));
        System.out.println("plusMonths(2)  = " + date.plusMonths(2));
        System.out.println("plusYears(1)   = " + date.plusYears(1));

        // Subtract days, weeks, months, year value from date object.
        System.out.println("minusDays(10)  = " + date.minusDays(10));
        System.out.println("minusWeeks(9)  = " + date.minusWeeks(9));
        System.out.println("minusMonths(2) = " + date.minusMonths(2));
        System.out.println("minusYears(1)  = " + date.minusYears(1));
    }
}

The output of the program above are:

Date           = 2021-10-29
plusDays(10)   = 2021-11-08
plusWeeks(9)   = 2021-12-31
plusMonths(2)  = 2021-12-29
plusYears(1)   = 2022-10-29
minusDays(10)  = 2021-10-19
minusWeeks(9)  = 2021-08-27
minusMonths(2) = 2021-08-29
minusYears(1)  = 2020-10-29

Maven Dependencies

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

Maven Central