Javatpoint Logo
Javatpoint Logo

Getting First Date of Month in Java

Java is a versatile programming language that offers a wide range of features and functionalities. One of the useful functionalities that Java offers is the ability to get the first date of the month. This can be helpful in many scenarios where you need to perform some operations based on the start of the month, such as generating reports, calculating payroll, or scheduling tasks. In this article, we will explore the different ways to get the first date of the month in Java, using different approaches and libraries.

Method 1: Using the Calendar class

The Calendar class is a built-in Java class that provides methods to manipulate date and time. It is available in the java.util package and can be used to get the first date of the month.

Here is an example code that shows how to get the first date of the current month using the Calendar class:

FirstDateOfMonth.java

Output:

Wed Apr 01 00:00:00 IST 2023

In this code, we first create a Calendar object using the getInstance() method of the Calendar class. This returns a Calendar object initialized with the current date and time. We then set the value of the DAY_OF_MONTH field of the calendar object to 1, which represents the first day of the month. Finally, we use the getTime() method to get the date object corresponding to the first day of the month.

Method 2: Using the LocalDate class

Java 8 introduced the LocalDate class as a part of the new date and time API. The LocalDate class provides a convenient way to work with dates, without the need for a Calendar object.

Here is an example code that shows how to get the first date of the current month using the LocalDate class:

FirstDateOfMonth1.java

Output:

2023-04-01

In this code, we first create a LocalDate object using the now() method of the LocalDate class. This returns a LocalDate object initialized with the current date. We then use the withDayOfMonth() method to set the day of the month to 1, which gives us the first day of the month. Finally, we print the value of the LocalDate object.

Method 3: Using the SimpleDateFormat class

The SimpleDateFormat class is a part of the java.text package and provides methods to format and parse dates. We can use this class to get the first date of the month in a specific format.

Here is an example code that shows how to get the first date of the current month using the SimpleDateFormat class:

FirstDateOfMonth2.java

Output:

01/04/2023

In this code, we first create a Calendar object using the getInstance() method of the Calendar class. We then set the value of the DAY_OF_MONTH field of the calendar object to 1, which represents the first day of the month. We then create a SimpleDateFormat object and specify the date format using the dd/MM/yyyy pattern. This pattern specifies that we want the date in the format of "day/month/year". Finally, we use the format() method of the SimpleDateFormat class to format the date as a string and print it.

Method 4: Using the java.time.Month class

Another way to get the first date of the month is to use the java.time.Month class. The Month class is an enum that represents the twelve months of the year.

Here is an example code that shows how to get the first date of the current month using the Month class:

FirstDateOfMonth3.java

Output:

2023-04-01

In this code, we first create a LocalDate object using the now() method of the LocalDate class. We then use the getMonth() method to get the month of the current date as a Month enum. We also get the year of the current date using the getYear() method. Finally, we create a LocalDate object using the of() method of the LocalDate class, passing in the year, month, and day of the month as arguments.

In summary, getting the first date of the month in Java is a straightforward task, and can be achieved using different methods. By understanding the different approaches and libraries available, you can choose the one that best suits your needs and requirements.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA