Display month by name and number in Java

Java 8Object Oriented ProgrammingProgramming

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 −

Example

 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 \nMonth Number = %tm", cal, cal));
   }
}

Output

Current date and time: Mon Nov 26 07:07:31 UTC 2018
Month = Nov
Month Number = 11
raja
Published on 26-Dec-2018 07:00:50
Advertisements