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;

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(2019, 7, 22);
        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("2019-07-22");
        System.out.println("date = " + date);

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

The result of our code snippet:

date = 2019-07-22T00:00:00.000+08:00
date = 2019-07-22T00:00:00.000+08:00
date = 2019-07-22T00:00:00.000+08:00
date = 2019-07-22T00:00:00.000+08:00
date = 2019-07-22T00:00:00.000+08:00
date = 2019-07-22T00:00:00.000+08:00

Maven Dependencies

<!-- https://search.maven.org/remotecontent?filepath=joda-time/joda-time/2.10.3/joda-time-2.10.3.jar -->
<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.10.3</version>
</dependency>

Maven Central

Wayan Saryada

Founder at Kode Java Org
I am a programmer, a runner, a recreational diver, currently live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. If this post help, you can buy me a coffee or tea. Thank you 🥳

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.