How To Get Month Name From Date In Java
Last Updated : Mar 11, 2024
In this article we will show you the solution of how to get month name from date in java, the phrase "how to get month name from date in Java" refers to how to use the Java programming language to get the name of the current month from a given date.
A date string must be converted into a Date object in order to accomplish this.
The name of the month must then be extracted from the Date object using a date formatting class, such as SimpleDateFormat.
The extracted month name can then be used in the Java programme for additional processing or returned as a string.
We will now discuss the Java concept of getting the month name from the date.
Step By Step Guide On How To Get Month Name From Date In Java :-
import java.text.ParseException; import java.text.SimpleDateFormat; import java.text.ParseException; import java.util.Date; import java.util.Scanner; public class Main { public static void main(String[] args) throws ParseException { Scanner sc = new Scanner(System.in); String s1 = sc.nextLine(); System.out.println(calculateBornDay(s1)); } public static String calculateBornDay(String s1) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yy"); SimpleDateFormat sdf1 = new SimpleDateFormat("MMMM"); Date d = sdf.parse(s1); String s = sdf1.format(d); return s.toUpperCase(); } }
- Here, you can see how to create a Java programme that accepts a date in the form dd-MM-yy (day-month-year) as input and outputs the year's month in uppercase.
- The essential packages are imported at the beginning of the code.
- For the purpose of parsing the date input & formatting the output, the java.text package is required.
- The java.util package is required in order to collect user input.
- Next, we provide the primary class.
- The main method, which serves as the program's entry point, is contained in the main class.
- Because it invokes the calculateBornDay method, which also raises a ParseException, it throws a ParseException.
- Next, a scanner is created to receive input from the user.
- By doing this, a fresh scanner object is created that can read data from the main input stream.
- After that, read the user's input.
- With this, the user's input is retrieved and saved in the string variable s1.
- The month of the year is then determined by calling the calculateBornDay method after that.
- With s1 as the input string, this executes the calculateBornDay method and publishes the outcome to the console.
- The calculateBornDay method is then defined.
- The date input for this procedure is represented by the string parameter s1 in the form of dd-MM-yy.
- Because it invokes the SimpleDateFormat class's parse method, which is capable of throwing a ParseException, it raises a ParseException.
- Then, two SimpleDateFormat objects are defined.
- The output string is formatted using these objects after the input string has been parsed.
- The input string is parsed into a Date object using the first one, and the Date object is formatted into a string that represents the year's month using the second one.
- the input string should then be parsed into a Date object.
- Using the SimpleDateFormat object sdf, it converts the input string s1 into a Date object.
- Create a string from the Date object that represents the current month of the year.
- Using the SimpleDateFormat object sdf1, it formats the Date object d into a string that represents the month of the year.
- After that, the final return a formatted text in uppercase returns a string in that format.
Conclusion :-
As a result, we have successfully learned how to retrieve the month name from the date in Java.
Additionally, we discovered that it computes the month of the year and provides it in uppercase format given a date in the format dd-MM-yy.
The programme formats the output after parsing the date input using the java.text package.
In order to receive user input, it also makes use of the java.util package.
I hope this article on how to get month name from date in java helps you and the steps and method mentioned above are easy to follow and implement.