Home » Java programs » Java date, time programs

Java program to print the months in different formats

In this java program, we will print the different format of month, in which we can write a month and this will be done using formatter class and calendar class in JAVA.
Submitted by IncludeHelp, on December 04, 2017

We are taking an example to learn different format, for this we are taking two different packages formatter and calendar.

Here, we are printing the month in different formats like, “November”, “Nov”, “11”.


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

public class ExMonthFormates
	public static void main(String args[])
		// create object of date formatter class.
		Formatter fmt = new Formatter();
		// create object of calendar class.
		Calendar cal = Calendar.getInstance();
		fmt = new Formatter();
		// print month in different ways.
		fmt.format("%tB %tb %tm", cal, cal, cal);


November Nov 11

We are creating a main class ExMonthFormates, but here we are using two different packages java.util.Calendar and java.util.Formatter for using methods of these class. After that create an object fmt using formatter class because formatter class provides justification and alignment of formats like numeric, string and date/time. Next, we have to create the calendar class object to convert the instant in time and set of calendar field.

Now, assigning the value into the object fmt (fmt = new Formatter()).

Displaying the format by using %tB %tb %tm, these display the date month name and number and System.out.println(fmt) will print this format on the same line.

Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.

Comments and Discussions

We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.