All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Get Month Name From Date In Java

Last Updated : Mar 11, 2024

How To Get Month Name From Date In Java

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();
  }
}
  1. 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.
  2. The essential packages are imported at the beginning of the code.
  3. For the purpose of parsing the date input & formatting the output, the java.text package is required.
  4. The java.util package is required in order to collect user input.
  5. Next, we provide the primary class.
  6. The main method, which serves as the program's entry point, is contained in the main class.
  7. Because it invokes the calculateBornDay method, which also raises a ParseException, it throws a ParseException.
  8. Next, a scanner is created to receive input from the user.
  9. By doing this, a fresh scanner object is created that can read data from the main input stream.
  10. After that, read the user's input.
  11. With this, the user's input is retrieved and saved in the string variable s1.
  12. The month of the year is then determined by calling the calculateBornDay method after that.
  13. With s1 as the input string, this executes the calculateBornDay method and publishes the outcome to the console.
  14. The calculateBornDay method is then defined.
  15. The date input for this procedure is represented by the string parameter s1 in the form of dd-MM-yy.
  16. Because it invokes the SimpleDateFormat class's parse method, which is capable of throwing a ParseException, it raises a ParseException.
  17. Then, two SimpleDateFormat objects are defined.
  18. The output string is formatted using these objects after the input string has been parsed.
  19. 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.
  20. the input string should then be parsed into a Date object.
  21. Using the SimpleDateFormat object sdf, it converts the input string s1 into a Date object.
  22. Create a string from the Date object that represents the current month of the year.
  23. Using the SimpleDateFormat object sdf1, it formats the Date object d into a string that represents the month of the year.
  24. 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.

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 🡪