Write a Java Program to Parse individual components of date from the current date


This Java program obtains the current date and time using the Date class and then converts it to a LocalDate object using the java.time.LocalDate class. It then extracts the day, month, and year from the LocalDate object and prints them to the console. Here's an explanation of the code:

  • import java.util.Date;: This imports the java.util.Date class, which is used to obtain the current date and time.
  • import java.time.Month;: This imports the java.time.Month enum, which represents the months of the year.
  • import java.time.LocalDate;: This imports the java.time.LocalDate class, which represents a date without time information.
  • import java.text.*;: This imports the java.text package, which contains classes for formatting and parsing dates.
  • class Main: This defines a class named Main.
  • public static void main(String args[]): This is the entry point of the program. It declares a public static method named main that takes an array of strings as an argument.
  • Date dt = new Date();: This creates a new Date object representing the current date and time.
  • DateFormat dtformat = new SimpleDateFormat("yyyy-MM-dd");: This creates a new SimpleDateFormat object that formats dates as "yyyy-MM-dd".
  • final String str_date = dtformat.format(dt);: This formats the Date object as a string using the SimpleDateFormat object and stores it in the str_date variable.
  • LocalDate lDate = LocalDate.parse(str_date);: This parses the str_date string as a LocalDate object.
  • int dd = lDate.getDayOfMonth();: This obtains the day of the month from the LocalDate object and stores it in the dd variable.
  • Month mon = lDate.getMonth();: This obtains the month of the year from the LocalDate object as a Month enum value and stores it in the mon variable.
  • int y = lDate.getYear();: This obtains the year from the LocalDate object and stores it in the y variable.
  • System.out.println("Day : " + dd); : This prints the string "Day : " followed by the value of the dd variable to the console.
  • System.out.println("Month : " + mon); : This prints the string "Month : " followed by the value of the mon variable to the console.
  • System.out.println("Year : " + y); : This prints the string "Year : " followed by the value of the y variable to the console.

Overall, this program obtains the current date and time, formats it as a string, parses it as a LocalDate object, extracts the day, month, and year, and then prints them to the console.

Source Code

import java.util.Date;
import java.time.Month;
import java.time.LocalDate;
import java.text.*;
class Main
{
	public static void main(String args[])
	{
		Date dt = new Date();
		DateFormat dtformat = new SimpleDateFormat("yyyy-MM-dd");
 
		final String str_date = dtformat.format(dt);
		LocalDate lDate = LocalDate.parse(str_date);
 
		int dd = lDate.getDayOfMonth();
		Month mon = lDate.getMonth();
		int y = lDate.getYear();
 
		System.out.println("Day   : " + dd);
		System.out.println("Month : " + mon);
		System.out.println("Year  : " + y);
	}
}

Output

Day   : 17
Month : DECEMBER
Year  : 2022

Example Programs