All TalkersCode Topics

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

Get Month And Year From Date In Java

Last Updated : Mar 11, 2024

Get Month And Year From Date In Java

In this article we will show you the solution of get month and year from date in java, the LocalDate class in Java has methods for getting a date's month and year components. You can use the getMonth() function to determine the month of a LocalDate object, which returns a Month enum that indicates the month of the date.

Calling the getYear() method on a LocalDate object will return an int value that represents the year of the date.

Both methods are instance methods, thus any LocalDate object is able to call either one.

With the help of these methods, you can quickly extract and manipulate the month and year parts of a date in Java.

We'll talk about the Java concept of getting the month as well as the year from a date.

Step By Step Guide On Get Month And Year From Date In Java :-

import java.util.Date;
import java.time.Month;
import java.time.LocalDate;
class TK {
 public static void
 getDayMonthYear(String date)
  LocalDate currentDate
   = LocalDate.parse(date);
  int day = currentDate.getDayOfMonth();
  Month month = currentDate.getMonth();
  int year = currentDate.getYear();
  System.out.println("Day: " + day);
  System.out.println("Month: " + month);
  System.out.println("Year: " + year);
 public static void main(String args[])
  String date = "2023-01-01";
  1. You can see that in this example, we create a Java programme that makes use of the Java 8 Date/Time API to retrieve the month, year, and day values from a provided date string.
  2. A java.time package, which offers classes for working with times and dates in Java 8, and the java.util.Date class are imported at the beginning of the code.
  3. Then, we declare TK as a public class.
  4. Next, we declare the public static function getDayMonthYear, which accepts a String argument called date.
  5. By using the parse method on the LocalDate class and offering the date parameter inside the getDayMonthYear method, a new LocalDate object will be created.
  6. A date object is created by parsing the input string.
  7. The day value is then extracted as an int variable named day using the LocalDate object's getDayOfMonth function.
  8. In order to obtain the month value as a month enum variable named month, we then use the LocalDate object's getMonth method.
  9. The year value is then extracted as an int variable called year using the LocalDate object's getYear function.
  10. Then, using the System.out.println method & string concatenation, we print the day, month, year, and other values to the console.
  11. Then we declare the public static function main, which takes a String[] argument called args.
  12. We define the String variable date and assign its value to "2023-01-01" inside the main procedure.
  13. We then use the date variable as an input when calling the getDayMonthYear function.
  14. The values of the entered date's day, month, as well as the year are printed to the console in this manner.

Conclusion :-

Thus, we were able to understand how to retrieve the year and month from a date in Java.

We also learned how to extract particular date components, including the day, month, & year, format them, and display them to the console using the Java 8 Date/Time API.

Additionally, it demonstrates how to turn a date representation in a string into a LocalDate object using the parse method.

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

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪