In the previous article, we have discussed about Java Program to Display Current Time in AM/PM Format
In this article we are going to see how to display current month in the (MMM) format in Java along with suitable examples.
Java Program to Display Current Month in the (MMM) Format
Let see the programs.
Method-1: Java Program to Display Current Month in the (MMM) Format By Using SimpleDateFormat Class
The java.text.SimpleDateFormat
class provides inbuild methods to format the date and time in java. There are 2 patterns which we can use in SimpleDateFormat to display the month.
- MMM – used for displaying month in 3 letters.
- MMMM – used for displaying month in complete abbreviation.
Approach:
- Create an object of
SimpleDateFormat
as ‘s1
’ with the argument as ‘MMM
’. - Declare a string variable as ‘
month1
’ and initialize it to the current date and time using an inbuild method ofSimpleDateFormat
ass1.format(new Date())
- Print the result.
- Create another object of
SimpleDateFormat
as ‘s2
’ with the argument as ‘MMMM
’. - Declare a string variable as ‘
month2
’ and initialize it to the current date and time using an inbuild method ofSimpleDateFormat
ass2.format(new Date())
- Print the result.
Program:
import java.text.SimpleDateFormat; import java.util.*; public class Main { public static void main(String[] args) { //create an object of SimpleDateFormat as ‘s1’ with the argument as ‘MMM’. SimpleDateFormat s1 = new SimpleDateFormat("MMM"); // Declare a string variable as ‘month1’ and initialize it to the current date and time using an inbuild method of SimpleDateFormat as s1.format(new Date()) String month1= s1.format(new Date()); //Print the result in MMM format System.out.println("Month in MMM format = "+month1); //create an object of SimpleDateFormat as ‘s2’ with the argument as ‘MMMM’. SimpleDateFormat s2 = new SimpleDateFormat("MMMM"); // Declare a string variable as ‘month2’ and initialize it to the current date and time using an inbuild method of SimpleDateFormat as s2.format(new Date()) String month2 = s2.format(new Date()); //Print the result in MMMM format System.out.println("Month in MMMM format = "+month2); } }
Output: Month in MMM format = Jun Month in MMMM format = June
Method-2: Java Program to Display Current Month in the (MMM) Format By Using Calendar and Formatter class
The java.util.Formatter
class provides inbuild “.format()
” methods to format the month in java.
The java.util.Calendar
class provides an inbuild method “.getInstance()
” method to get the instant date-time-month from the system calendar.
There are 3 patterns which we can use in Formatter to display the month.
- %tb – used for displaying month in 3 letters.
- %tB – used for displaying month in complete abbreviation.
- %tm – used for displaying month number.
Approach:
- Declare a variable ‘
cal
’ of typeCalendar
and initialize it to get the system date-time usingCalender.getInstance()
method. - Create object of Formatter as ‘
f1
’, ‘f2
’, ‘f3
’ and initialize it to ‘f1.format("%tb", cal)
’, ‘f2.format("%tB",cal)
’, ‘f3.format("%tm",cal)
’ respectively to store the month in 3 different format. - Print the result separately calling each formatter variable ‘
f1
’, ‘f2
’, ‘f3
’.
Program:
import java.util.*; public class Main { public static void main(String[] args) { //declare a variable ‘cal’ of type Calendar //and initialize it to get the system date time month using Calender.getInstance() method. Calendar cal = Calendar.getInstance(); //create an object of Formatter as ‘f1’ Formatter f1 = new Formatter(); //store the mon format in f1 variable f1.format("%tb", cal); //Print the result System.out.println("MMM format: "+f1); //create an object of Formatter as ‘f2’ Formatter f2 = new Formatter(); //store the month format in f2 variable f2.format("%tB",cal); //Print the result System.out.println("month format: "+f2); //create an object of Formatter as ‘f3’ Formatter f3 = new Formatter(); //store the month number format in f3 variable f3.format("%tm",cal); //Print the result System.out.println("month number format: "+f3); } }
Output: MMM format: Jun month format: June month number format: 06
Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.
Related Java Programs: