Check a given date is first day of month using Joda API

Introduction

In this post, I will show you how to check whether a given date is first day of the month using Joda API. Joda API is a date time API which provides useful methods for data and time management in Java applications.

Prerequisites

Java 8 or 12, Joda API 2.10.3 (or 2.7), Junit 4.12

Build using Gradle

If you want to build application using gradle script – build.gradle, then you can use the following build script.

In the below build script we have included Joda API and Junit API for date time and running test cases respectively.

plugins {
    id 'java-library'
}
sourceCompatibility = 12
targetCompatibility = 12
repositories {
	mavenCentral()
    jcenter()
}
dependencies {
    implementation 'joda-time:joda-time:2.10.3'
    testImplementation 'junit:junit:4.12'
}

Build using Maven

If you want to build the application using maven file – pom.xml, then you can use the following build file.

<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-joda-api</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.10.3</joda.version>
        <junit.version>4.12</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>

Checking Day of Month

Create a utility class with utility method to check a given date is first day of month using Joda API.

package com.roytuts.joda;
import org.joda.time.DateTime;
public final class JodaDateUtils {
	public static boolean isDateFirstDayOfTheMonth(final DateTime dateTime) {
		return dateTime.getDayOfMonth() == 1;
	}
}

Junit Testing

Create Junit test case to test the above 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));
	}
}

Testing the Application

Run the above Junit class, you will get the below output:

false
true
true
false

Source Code

You can download source code.

Thanks for reading.

Leave a Reply

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