Display month by name and number in Java

Use the ‘b’ conversion character for month name in Java.

Use the ‘m’ conversion character for month number in Java.

Here, we are using Formatter and Calendar class, therefore import the following packages.

import java.util.Calendar;
import java.util.Formatter;

The following is an example to display month name and number −


 Live Demo

import java.util.Calendar;
import java.util.Formatter;
public class Demo {
   public static void main(String args[]) {
      Formatter f = new Formatter();
      Calendar cal = Calendar.getInstance();
      System.out.println("Current date and time: "+cal.getTime());
      f = new Formatter();
      System.out.println(f.format("Month = %tb 
Month Number = %tm", cal, cal)); } }


Current date and time: Mon Nov 26 07:07:31 UTC 2018
Month = Nov
Month Number = 11
Samual Sam
Samual Sam

Learning faster. Every day.