Java Get First LocalDate of Current Month

Tags: Java LocalDate DateUtil

In this Java tutorial, we learn how to get the first day of current month and return it as a LocalDate object in Java programming language.

How to get first LocalDate of current month in Java

Firstly, we create a new Java class named DateUtil, and implement a new static method named getFirstLocalDateOfCurrentMonth(), in this new method we follow these steps as below.

  • Step 1: get today date using the LocalDate.now() method.
  • Step 2: get the first day of current month based on today date using the LocalDate.withDayOfMonth(int dayOfMonth) method.

DateUtil.java

import java.time.LocalDate;

public class DateUtil {

    /**
     * This method to get first day of current month and return as a LocalDate object.
     * @return the first LocalDate object of current month.
     */
    public static LocalDate getFirstLocalDateOfCurrentMonth() {
        LocalDate today = LocalDate.now();
        LocalDate firstDayOfMonth = today.withDayOfMonth(1);
        return firstDayOfMonth;
    }
}

In the following example Java code, we learn how to use the above getFirstLocalDateOfCurrentMonth() static method to get the first day of current month as a LocalDate object in Java program.

GetFirstLocalDateOfCurrentMonthExample.java

import java.time.LocalDate;

public class GetFirstLocalDateOfCurrentMonthExample {
    public static void main(String... args) {
        // Get First LocalDate of Current Month
        LocalDate firstDayOfMonth = DateUtil.getFirstLocalDateOfCurrentMonth();

        System.out.println("Today: " + LocalDate.now());
        System.out.println("First day of current month: " + firstDayOfMonth);
    }
}
The output as below.
Today: 2022-08-20
First day of current month: 2022-08-01

Happy Coding 😊

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 Last LocalDate of Current Month

Java Get First LocalDate of Current Year

Java Get Last LocalDate of Current Year

Java Get First Day of Month from Specified Date

Java Get Last Date of Current Year

Java Get Last Date of Specified Month

Java Get Last Date of Specified Year

Java Get First Date of Current Year

Java Get First Date of Current Month

Java Get Same Date in Last Month