All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

How To Get Current Month In Java

Last Updated : Mar 11, 2024

How To Get Current Month In Java

In this article we will show you the solution of how to get current month in java, the process of collecting the information for the current month in the Java programming language is described by the phrase "How to get current month in java".

When working on applications that require quick responses or when using the current month as a basis for calculations or decisions, this is frequently required.

The Calendar & Date classes in Java, along with the new Java Time API that was introduced in Java 8, all offer several ways to obtain the current month.

Now let's talk about the idea of getting the current month in Java.

Step By Step Guide On How To Get Current Month In Java :-

import java.time.LocalDate;
import java.time.Month;
public class CurrentMonthExample {
    public static void main(String[] args) {
        LocalDate currentDate =;
        Month currentMonth = currentDate.getMonth();
        int currentMonthValue = currentDate.getMonthValue();
        String currentMonthName = currentMonth.toString();
        System.out.println("Current Month (Enum): " + currentMonth);
        System.out.println("Current Month (Value): " + currentMonthValue);
        System.out.println("Current Month (Name): " + currentMonthName);
  1. As you can see, we have written java code to retrieve the current month.
  2. Beginning with Java 8 and later versions, we import the appropriate classes into our code by using the java.time package.
  3. A date without a time is represented by the LocalDate class, while the months of a year are represented by the Month enum.
  4. Next, the main method is defined in a public class called CurrentMonthExample.
  5. The Java program's main method is where it starts & is executed when it is run.
  6. Once the current date has been obtained as an instance of the LocalDate class, the method will be called inside the main method.
  7. The now() static method returns the current date and time based on the system clock & the selected time zone.
  8. The current month is then obtained as a value for the Month enum by calling the getMonth() function on the currentDate object.
  9. Every month of the year is represented by a different enum value in the Month enum, which functions as a constant representation of the months of the year.
  10. The current month is obtained as an integer number by calling the getMonthValue() function on the currentDate object.
  11. An integer number between 1 and 12 that corresponds to the month of the year is returned by the getMonthValue() function.
  12. The current month name is then obtained as a string by calling the toString() function on the currentMonth object.
  13. The month name is returned as an uppercase string using the toString() method, such as "JANUARY", "FEBRUARY", etc.
  14. The System.out.println() function is used to print the output to the console.
  15. The current month's enum value is written first, then its integer value, and then the text that represents the name of the current month.
  16. When the code is run, the current month's details, including the month's enum value, integer value, and string representation of its name, are displayed on the console.

Conclusion :-

Thus, we have learned the Java concept of obtaining the current month.

We also discovered that the code calls the methods, getMonth(), getMonthValue(), and toString() to retrieve the current month using the java.time package.

The console prints the results.

I hope this article on how to get current month in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪