Java Get First Date of Current Month

Tags: Java Date DateUtil

In this Java tutorial, we learn how to write Java program to get the first day of the current month and return it as a Date object with the time set to start of the day.

How to get first Date of current month in Java

In this first step, we create a new Java class named DateUtil, and implement the static method named getFirstDayOfCurrentMonth() to return the first day of current month with the hour, minute, second set to start of the day as following Java code.

DateUtil.java

import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    /**
     * This method to get the first day of current month and return as a Date object
     * @return the first Date of current month
     */
    public static Date getFirstDayOfCurrentMonth() {
        Calendar calendar = Calendar.getInstance();

        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMinimum(Calendar.HOUR_OF_DAY));
        calendar.set(Calendar.MINUTE, calendar.getActualMinimum(Calendar.MINUTE));
        calendar.set(Calendar.SECOND, calendar.getActualMinimum(Calendar.SECOND));
        calendar.set(Calendar.MILLISECOND, calendar.getActualMinimum(Calendar.MILLISECOND));

        return calendar.getTime();
    }
}

In the following Java program, we learn how to use the DateUtil class above to get the first Date of current month.

GetFirstDayOfCurrentMonthExample.java

import java.util.Date;

public class GetFirstDayOfCurrentMonthExample {
    public static void main(String... args) {
        // Get First Date of Current Month
        Date firstDateOfMonth = DateUtil.getFirstDayOfCurrentMonth();

        System.out.println("Today: " + new Date());
        System.out.println("First day of current month: " + firstDateOfMonth);
    }
}
The output as below.
Today: Tue Aug 16 20:59:06 ICT 2022
First day of current month: Mon Aug 01 00:00:00 ICT 2022

Happy Coding 😊

Java Convert String to Date

Java Convert Date to Start of Day Time

Java Convert Date to End of Day Time

Java Get First Date of Current Year

Java Get Same Date in Last Month

Java Get First Day of Month from Specified Date

Java Get Yesterday Date

Java Get Tomorrow Date

Java Get Last Date of Current Month

Java Get Last Date of Current Year

Java Get Last Date of Specified Month

Java Get Last Date of Specified Year

Java Check if Calendar is Week Day or Weekend Day

Java Check if Date is Week Day or Weekend Day

Java Check if Today is Week Day or Weekend Day

Java Get First Date of Current Week

Java Get Last Date of Current Week

Java Get First LocalDate of Current Week

Java Get Last LocalDate of Current Week

Java Get First LocalDate of Current Month

Java Get Last LocalDate of Current Month

Java Get First LocalDate of Current Year

Java Get Last LocalDate of Current Year