Display Month of Year using Java Calendar


For using Calendar class, import the following package.

import java.util.Calendar;

Using the Calendar class, create an object.

Calendar calendar = Calendar.getInstance();

Now, create a string array of the month names.

String[] month = new String[] {"January", "February", "March", "April", "May", "June", "July", "August","September", "October", "November", "December" };

Display the month name.

month[calendar.get(Calendar.MONTH)]

The following is an example.

Example

 Live Demo

import java.util.Calendar;
public class Demo {
   public static void main(String[] args) {
      Calendar calendar = Calendar.getInstance();
      String[] month = new String[] {"January", "February", "March", "April", "May", "June", "July", "August","September", "October", "November", "December" };
      System.out.println("Current Month = " + month[calendar.get(Calendar.MONTH)]);
   }
}

Output

The following is the output −

Current Month = Dec

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 27-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements