Java Program to get current date, year and month



Import the following package for using Calendar class,

import java.util.Calendar;

Create a calendar object as shown below −

Calendar cal = Calendar.getInstance();

Let us now get the current date, year and month.

cal.get(Calendar.YEAR)
cal.get(Calendar.MONTH) + 1
cal.get(Calendar.DATE)

The following is the final example.

Example

 Live Demo

import java.util.Calendar;
public class Demo {
   public static void main(String[] args) {
      Calendar cal = Calendar.getInstance();
      System.out.println("Year = " + cal.get(Calendar.YEAR));
      System.out.println("Month = " + (cal.get(Calendar.MONTH) + 1));
      System.out.println("Date = " + cal.get(Calendar.DATE));
   }
}

Output

Year = 2018
Month = 11
Date = 19
Samual Sam
Samual Sam

Learning faster. Every day.


Advertisements