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
import java.util.Date;
import java.time.Month;
import java.time.LocalDate;
class GFG {
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 = "2020-07-18" ;
getDayMonthYear(date);
}
}
|
Output
Day: 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
import java.util.*;
class GFG {
public static void main(String args[])
{
Calendar cal
= new GregorianCalendar(
2020 , 07 , 18 );
int day = cal.get(Calendar.DAY_OF_MONTH);
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);
System.out.println( "Day: " + day);
System.out.println( "Month: " + month);
System.out.println( "Year: " + year);
}
}
|
Output
Day: 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
class GFG {
public static void findDate(String date)
{
String dateParts[] = date.split( "-" );
String day = dateParts[ 0 ];
String month = dateParts[ 1 ];
String year = dateParts[ 2 ];
System.out.println( "Day: " + day);
System.out.println( "Month: " + month);
System.out.println( "Year: " + year);
}
public static void
main(String args[])
{
String date = "18-07-2020" ;
findDate(date);
}
}
|
Output
Day: 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));
}
}
|
Output
Current 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).
Last Updated :
01 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...