
- Example - Home
- Example - Environment
- Example - Strings
- Example - Arrays
- Example - Date & Time
- Example - Methods
- Example - Files
- Example - Directories
- Example - Exceptions
- Example - Data Structure
- Example - Collections
- Example - Networking
- Example - Threading
- Example - Applets
- Example - Simple GUI
- Example - JDBC
- Example - Regular Exp
- Example - Apache PDF Box
- Example - Apache POI PPT
- Example - Apache POI Excel
- Example - Apache POI Word
- Example - OpenCV
- Example - Apache Tika
- Example - iText
- Java Useful Resources
- Java - Quick Guide
- Java - Useful Resources
How to Display Name of a Month in (MMM) Format using Java
In Java, the month name can be displayed in the abbreviated format (MMM) using the SimpleDateFormat class or through the Calendar and Formatter classes. This helps in formatting the month as a short text, such as "Oct" for October.
Using Calendar and Formatter
Calendar getInstance(): The Java Calendar getInstance() method gets a calendar using the current time zone and locale.
syntax
The following is the syntax for Calendar getInstance()
public static Calendar getInstance()
We will display the current month in the (MMM) format with the help of Calender.getInstance() method of Calender class and fmt.format() method of Formatter class.
Step 1. Initialize Formatter object: Creates a Formatter object named fmt to handle string formatting.
Formatter fmt = new Formatter();
Step 2. Get current date and time: Obtains the current date and time using the Calendar class.
Calendar cal = Calendar.getInstance();
-
Step 3. Format the date: Uses the format method to format the month in different ways
%tB: Full month name.
%tb: Abbreviated month name.
%tm: Month as a two-digit number.
fmt.format("%tB %tb %tm", cal, cal, cal);
Example
The following program uses Calendar to get the current date and Formatter to format the month in both full (%tB) and short (%tb) formats
import java.util.Calendar; import java.util.Formatter; public class MainClass { public static void main(String args[]) { Formatter fmt = new Formatter(); Calendar cal = Calendar.getInstance(); fmt = new Formatter(); fmt.format("%tB %tb %tm", cal, cal, cal); System.out.println(fmt); } }
Output
October Oct 10
Using SimpleDateFormat
SimpleDateFormat: SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting.
Example
The following program uses SimpleDateFormat to format the month
import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class HelloWorld { public static void main(String[] args) { SimpleDateFormat f = new SimpleDateFormat("MMM"); SimpleDateFormat f1 = new SimpleDateFormat("dd"); SimpleDateFormat f2 = new SimpleDateFormat("a"); int h; if(Calendar.getInstance().get(Calendar.HOUR)== 0)h = 12; else h = Calendar.getInstance().get(Calendar.HOUR); String filename="Current Date is :" +f1.format(new Date()) +f.format(new Date()) +h+f2.format(new Date()); System.out.println(filename); } }
Output
Current Date is :11Nov5AM