Q:

Java program to print the months in different formats

belongs to collection: Java Date and Time Programs

0

Java program to print the months in different formats

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Date and Time Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Program to add given hours in current date and tim... >>
<< Java program to print different dates of days like...