Get the name of first and last day of a month in Java
In this tutorial, we will learn how to make a Java program to get the name of the first and last day of the month. This program will help you to develop more knowledge about packages in Java.
Before starting further we need to understand about java.util.* which is basically package in Java which contains a collection of classes.
Note: Remember there could be multiple ways to solve a particular problem as per your understanding.
Some important classes inside this package are-AbstractCollection, AbstractList, ArrayList, Arrays Calendar, GregorianCalendar, Locale and many more.
Java program to get the name of the first and last day of a month
Logic:
- Finding the current day using method getInstance(), let say the current date is 24-Jun-2019.
- And finding the maximum no days in a current month using getActualMaximum() method. For a given example, it is 30 as the maximum number of days in the current month is 30.
- For finding the first day we need to apply condition:
if(current_day not equal 1) then subtract (current_date) by (current_date-1) which will give us day 1 as we all know the first day of every month is 1 which is obtained by (24-23) for the given example. For subtracting days we will add() Method which is present inside the Calendar class.
else(Today_is_first_day). - Similarly, for last day month, we need to add (Current_day) to (Max_day-1). As the current day was 1 that we got above step and adding it (max_day-1) i.e 29 gives us day 30 for given example i.e Last_Day.
STEPS FOLLOWED ON PROGRAM:
1. The first step is to import the calendar class from java.util package.
//importing the calender class from java.util package import java.util.Calendar; public class First_last_day_of_Month { public static void main(String[] args) {
2. The second step is to create the object of the calendar where c is the reference variable. Also printing today’s date.
// Creating a calendar object Calendar c = Calendar.getInstance(); System.out.println("The Todays Date is:" + c.getTime());
3. Get the maximum number of days in a month and also the current date.
//getting the maximum number of days in Month on variable x int x= c.getActualMaximum(Calendar.DAY_OF_MONTH); //getting current date on variable z int z=c.get(Calendar.DATE);
4. Applying condition for getting the first day of the month.
//putting condition if(z!=1){ //holding current_day-1 on vaiable y int y =z-1; //subtracting (current_day)-(current_day-1) that gives first_day c.add(Calendar.DATE,-y); //printing first day System.out.println("First day of month is: "+ c.getTime()); } else{ //printing first day System.out.println("First day of month is:" + c.getTime()); }
5. Now, applying a condition for the last day of the month and printing the last_day.
// adding max_day-1 to current_day that gives us last day c.add(Calendar.DATE,x-1); //printing last day System.out.println("Last day of month is: "+ c.getTime()); } }
COMPLETE CODE:
//importing the calender class from java.util package import java.util.Calendar; public class First_last_day_of_Month { public static void main(String[] args) { // Creating a calendar object Calendar c = Calendar.getInstance(); System.out.println("The Todays Date is:" + c.getTime()); //getting maximum number of days in Month on variable x int x= c.getActualMaximum(Calendar.DAY_OF_MONTH); //getting current date on variable z int z=c.get(Calendar.DATE); //putting condition if(z!=1){ //holding current_day-1 on vaiable y int y =z-1; //subtracting (current_day)-(current_day-1) that gives first_day c.add(Calendar.DATE,-y); //printing first day System.out.println("First day of month is: "+ c.getTime()); } else{//printing first day System.out.println("First day of month is:" + c.getTime()); } // adding max_day-1 to current_day that gives us last day c.add(Calendar.DATE,x-1); //printing last day System.out.println("Last day of month is: "+ c.getTime()); } }
OUTPUT: The Todays Date is:Mon Jun 24 16:52:46 Ist 2019 First day of month is:Sat Jun 01 16:52:46 Ist 2019 Last day of month is:Sun Jun 24 16:52:46 Ist 2019
Hope this tutorial was helpful for you.
For similar program: How to make Java program to add some hours to the current time?
Thank you soo much,This answer saved me