In this post, I will show you how to check whether a given date is first day of the month using joda date time API.

Prerequisites

The following configurations are required in order to run the application

Eclipse
JDK 1.8
Have maven installed and configured
Joda date API dependency in pom.xml

Now we will see the below steps how to create a maven based Java project in Eclipse

Step 1. Create a standalone maven project in Eclipse

Go to File -> New -> Other. On popup window under Maven select Maven Project. Then click on Next. Select the workspace location – either default or browse the location. Click on Next. Now in next window select the row as highlighted from the below list of archtypes and click on Next button.

maven-arctype-quickstart

Now enter the required fields (Group Id, Artifact Id) as shown below

Group Id : com.roytuts
Artifact Id : java

Step 2. Modify the pom.xml file as shown below.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.roytuts</groupId>
    <artifactId>java</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>java</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <jdk.version>1.8</jdk.version>
        <joda.version>2.7</joda.version>
        <junit.version>4.11</junit.version>
    </properties>

    <dependencies>
        <!-- joda date time -->
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>${joda.version}</version>
        </dependency>
        <!-- junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Step 4. Create a utility class to create utility method

package com.roytuts.joda;

import org.joda.time.DateTime;

public class JodaDateUtils {

    public static boolean isDateFirstDayOfTheMonth(final DateTime dateTime) {
        return dateTime.getDayOfMonth() == 1;
    }

}

Step 5. Create Junit test case to test the method

package com.roytuts.joda;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.junit.Test;

public class JodaDateUtilsTest {

    private final DateTimeFormatter dtf = DateTimeFormat.forPattern("dd-MM-yyyy");

    @Test
    public void testIsDateFirstDayOfTheMonth() {
        DateTime dt = new DateTime();
        System.out.println(JodaDateUtils.isDateFirstDayOfTheMonth(dt));
        dt = dtf.parseDateTime("01-02-2016");
        System.out.println(JodaDateUtils.isDateFirstDayOfTheMonth(dt));
        dt = dtf.parseDateTime("01-01-2016");
        System.out.println(JodaDateUtils.isDateFirstDayOfTheMonth(dt));
        dt = dtf.parseDateTime("05-02-2016");
        System.out.println(JodaDateUtils.isDateFirstDayOfTheMonth(dt));
    }

}

Step 6. Run the above Junit, you will get the below output

false
true
true
false

Thanks for reading.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials | TwitterFacebook Google PlusLinkedin | Reddit | Email Me

Leave a Reply

Your email address will not be published. Required fields are marked *