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.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("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            = 2017-01-15T20:03:17.334+08:00
First day of next month = 2017-02-01T00:00:00.000+08:00

Maven Dependencies

<!-- http://repo1.maven.org/maven2/joda-time/joda-time/2.9.7/joda-time-2.9.7.jar -->
<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.9.7</version>
</dependency>

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.example.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("Date = " + lastDate.toString());
        System.out.println("Day  = " + 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("2017-01-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:

Date = 2017-01-31T15:00:24.838+08:00
Day  = Tuesday
Day  = Tuesday

Maven Dependencies

<!-- http://repo1.maven.org/maven2/joda-time/joda-time/2.9.7/joda-time-2.9.7.jar -->
<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.9.7</version>
</dependency>

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.example.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(2017, 1, 15, 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("2017-01-15T13:14:00.000+08:00");
        System.out.println("date = " + date);
        date = DateTime.parse("2017-01-15");
        System.out.println("date = " + date);
        date = DateTime.parse("15/01/2017", DateTimeFormat.forPattern("dd/MM/yyyy"));
        System.out.println("date = " + date);
    }
}

The result of our code snippet:

date = 2017-01-15T13:15:39.248+08:00
date = 2017-01-15T13:15:39.395+08:00
date = 2017-01-15T00:00:00.000+08:00
date = 2017-01-15T13:15:39.405+08:00
date = 2017-01-15T13:15:39.406+08:00
date = 2017-01-15T13:15:39.424+08:00
date = 2017-01-15T13:14:00.000+08:00
date = 2017-01-15T00:00:00.000+08:00
date = 2017-01-15T00:00:00.000+08:00

Maven Dependencies

<!-- http://repo1.maven.org/maven2/joda-time/joda-time/2.9.7/joda-time-2.9.7.jar -->
<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.9.7</version>
</dependency>

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.example.joda;

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

import java.util.Date;

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(2017, 1, 15);
        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("2017-01-15");
        System.out.println("date = " + date);

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

The result of our code snippet:

date = 2017-01-15T00:00:00.000+08:00
date = 2017-01-15T00:00:00.000+08:00
date = 2017-01-15T00:00:00.000+08:00
date = 2017-01-15T00:00:00.000+08:00
date = 2017-01-15T00:00:00.000+08:00
date = 2017-01-15T00:00:00.000+08:00

Maven Dependencies

<!-- http://repo1.maven.org/maven2/joda-time/joda-time/2.9.7/joda-time-2.9.7.jar -->
<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.9.7</version>
</dependency>