How to get Day, Month and Year from Date in Java
Last Updated :
01 Feb, 2023
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
// 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);
}
}
OutputDay: 18
Month: JULY
Year: 2020
Time Complexity: O(1), since the program has only one loop and it only executes one time, its time complexity is O(1).
Auxiliary Space: O(1), the program only uses constant space to store the variables, hence the space complexity is O(1).
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
// 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);
}
}
OutputDay: 18
Month: 7
Year: 2020
Time Complexity: O(1)
Auxiliary Space: O(1)
The time and space complexity of the above program is O(1) as it takes constant time and space to execute.
Method 3: Using String.split() in Java:
- The idea is to use the split() method of String class.
- It splits a string according to the pattern provided and returns an array of string.
Below is the implementation of the above approach:
Java
// Java program for the above approach
class GFG {
// Function to get day, month, and
// year from date
public static void findDate(String date)
{
// Splitting 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);
}
}
OutputDay: 18
Month: 07
Year: 2020
By using "java.time package" we can get current date and time in very simple way:
Simple java program to Print current Date Month and Year using Calendar-
Java
import java.util.Calendar;
class Easy
{
public static void main(String[] args)
{
Calendar cal=Calendar.getInstance();
System.out.println("Current Date:"+cal.get(Calendar.DATE));
System.out.println("Current Month:"+(cal.get(Calendar.MONTH)+1));
System.out.println("Current Year:"+cal.get(Calendar.YEAR));
}
}
OutputCurrent Date:22
Current Month:1
Current Year:2023
Time Complexity: O(1), As there is no loop or recursive function call present, the time complexity of the above program is O(1).
Auxiliary Space: O(1), As no new data structure is used, the space complexity of the program is O(1).
Similar Reads
Java Program to Get Year From Date
Java is the most powerful programming language, by which we can perform many tasks and Java is an industry preferable language. So it is filled with a huge amount of features. Here we are going to discuss one of the best features of Java, which is how to get a year from date using Java. Methods: The
4 min read
How to Calculate the Week Number of the Year for a Given Date in Java?
In Java, we have the Calendar class and the newer LocalDate class from java.time package. We can use these classes to find which week number of the year for a given date. The java.time package offers better functionality than the older date and time classes such as Calendar class. In this article, w
3 min read
How to Add Time to Date in Java?
In Java, You can add the time to a Date object and it can be part of the java.util.Calendar package and another approach be used to newer java.time package. I can explain both approaches adding the time to a Date in Java programming. In this article, we are going to learn, how to add time to date in
3 min read
Java Program to Display Dates of a Calendar Year in Different Format
As different countries do opt for different formats. So here the goal is simply to print dates of calendar in different years. The generic symbolic notation opted across the globe is: Symbolic NotationDepictsyyearMmonth in year dday in month Eday of week Concept: Whenever it comes down to the date a
4 min read
Java Program to Display Name of Months of Calendar Year in Short Format
As we know that in a calendar year, there are a total of 12 Months. In order to convert the name of months to a shorter format, there are basically two ways, ie we can either make use of DateFormatSymbols class or SimpleDateFormat class. These classes have methods that are used to convert the names
3 min read
How to Create a Date Object using the Calendar Class in Java
In Java, the Calendar class can provide a flexible way to handle the dates and times. This article demonstrates how to create the Date object using the Calendar class by setting the specific date and time and converting it to the Date object and printing the exact date and time. This approach offers
2 min read
How to Find Which Week of the Year in Java?
In Java, we have the Calendar class and the newer LocalDate class from the java.time package which we can use to find which week of the year it is. The java.time package offers better functionality and ease of use than older date and time classes such as Calendar class. In this article, we will lear
4 min read
How to convert Date to String in Java
Given a date, the task is to write a Java program to convert the given date into a string. Examples: Input: date = "2020-07-27" Output: 2020-07-27 Input: date = "2018-02-17" Output: 2018-02-17 Method 1: Using DateFormat.format() method Approach: Get the date to be converted.Create an instance of Sim
3 min read
How to Convert a String to a LocalDate in Java?
Converting a String to a LocalDate in Java is a common operation when you are dealing with date inputs from users. Java provides the LocalDate class in the java.time package for representing a date without time information. LocalDate class is part of the java.time package introduced in Java 8. Strin
2 min read
Java Program to Display Name of a Month in (MMM) Format
Java is a most powerful programming language, by which we can do many things and Java is an industry preferable language. So it has a huge field of features. Here we discuss one of the best features of Java, that is how we can represent Month in a short form or MMM Format. There are two ways to do t
3 min read