How to get Day, Month and Year from Date in Java

Given a date in the form of a string, the task is to write a Java Program to get the day, month, and year from the given date.

Examples:

Input: date = “2020-07-18”
Output:
Day: 18
Month: July
Year: 2020
Explanation: The given date is ‘2020-07-18’, so the day is: 18, the month is: July, and the year is: 2020.

Input: date = “2018-05-10”
Output:
Day: 10
Month: May
Year: 2018
Explanation: The given date is ‘2018-05-10’, so the day is: 10, the month is: May, and the year is: 2018.

Method 1: Using LocalDate class in Java:



  • The idea is to use methods of LocalDate class to get the day, month, and year from the date.
  • The getDayOfMonth() method returns the day represented by the given date, getMonth() method returns the month represented by the given date, and getYear() method returns the year represented by the given date.

Below is the implementation of the above approach:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
import java.util.Date;
import java.time.Month;
import java.time.LocalDate;
  
class GFG {
  
    // Function to get day, month, and
    // year from date
    public static void
    getDayMonthYear(String date)
    {
  
        // Get an instance of LocalTime
        // from date
        LocalDate currentDate
            = LocalDate.parse(date);
  
        // Get day from date
        int day = currentDate.getDayOfMonth();
  
        // Get month from date
        Month month = currentDate.getMonth();
  
        // Get year from date
        int year = currentDate.getYear();
  
        // Print the day, month, and year
        System.out.println("Day: " + day);
        System.out.println("Month: " + month);
        System.out.println("Year: " + year);
    }
  
    // Driver Code
    public static void main(String args[])
    {
        // Given Date
        String date = "2020-07-18";
  
        // Function Call
        getDayMonthYear(date);
    }
}

chevron_right


Output:

Day: 18
Month: JULY
Year: 2020

Method 2: Using Calendar class in Java:

  • The idea is to use get() method of Calendar class to get the day, month, and year from the date.
  • The get() method takes one parameter of integer type and returns the value of the passed field from the given date.
  • It returns the month index instead of the month name.

Below is the implementation of the above approach:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
import java.util.*;
  
class GFG {
  
    // Driver Code
    public static void main(String args[])
    {
        // Creating a calendar object
        Calendar cal
            = new GregorianCalendar(
                2020, 07, 18);
  
        // Getting the values of day,
        // month, and year from calendar
        // object
        int day = cal.get(Calendar.DAY_OF_MONTH);
        int month = cal.get(Calendar.MONTH);
        int year = cal.get(Calendar.YEAR);
  
        // Printing the day, month, and year
        System.out.println("Day: " + day);
        System.out.println("Month: " + month);
        System.out.println("Year: " + year);
    }
}

chevron_right


Output:

Day: 18
Month: 7
Year: 2020

Method 3: Using String.split() in Java:

  1. The idea is to use the split() method of String class.
  2. It splits a string according to the pattern provided and returns an array of string.

Below is the implementation of the above approach:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
class GFG {
  
    // Function to get day, month, and
    // year from date
    public static void findDate(String date)
    {
        // Spiliting the given date by '-'
        String dateParts[] = date.split("-");
  
        // Getting day, month, and year
        // from date
        String day = dateParts[0];
        String month = dateParts[1];
        String year = dateParts[2];
  
        // Printing the day, month, and year
        System.out.println("Day: " + day);
        System.out.println("Month: " + month);
        System.out.println("Year: " + year);
    }
  
    // Driver Code
    public static void
    main(String args[])
    {
        // Given date
        String date = "18-07-2020";
  
        findDate(date);
    }
}

chevron_right


Output:

Day: 18
Month: 07
Year: 2020

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

External Technical Content Reviewer at GeeksforGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.