Java Program to Parse Individual Components of Date From the Current Date

You are currently viewing Java Program to Parse Individual Components of Date From the Current Date

Java program to parse individual components of date from the current date

// Java program to parse individual components 
// of date from current date

import java.util.Date;
import java.time.Month;
import java.time.LocalDate;
import java.text.*;

class Main {
  public static void main(String args[]) {
    Date date = new Date();
    DateFormat dtFormat = new SimpleDateFormat("yyyy-MM-dd");

    final String strDate = dtFormat.format(date);
    LocalDate lDate = LocalDate.parse(strDate);

    int dd = lDate.getDayOfMonth();

    Month mon = lDate.getMonth();

    int yyyy = lDate.getYear();

    System.out.println("Day  : " + dd);
    System.out.println("Month: " + mon);
    System.out.println("Year : " + yyyy);
  }
}

Output

Day  : 1
Month: APRIL
Year : 2022

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply