Java Program to Print the Months in Different Formats
For printing months in different formats, we are going to use two classes of java.util package. That is the first one Calendar class and another one is Formatter class. From Calendar class use getInstance() method to get instance (time and date information) of calendar according to the current time zone.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Input : 18-11-2020 Output: December Dec 12 Explaination: Here, month starts from 0. Input : 18-5-2019 Output: June Jun 06
public static Calendar getInstance()
Return Value: The method returns the calendar.
Formatter class in java is mainly used to displaying a number, string, time, date any format you like. Following are the conversion characters are used for formatting dates in our program.
- %tB- Full month name like “January” “March”.
- %tb-Abbreviated month name like “Jan”, “Feb”.
- %tm-Months formatted as two digits.
The format used in the below implementation:
"November" "NOV" "11"
Output: June Jun 06