×

Java Programs

Java Practice

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”.

Program

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);
    System.out.println(fmt);
  }
}

Output

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.

Java Date and Time Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.