
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Display Month by Name and Number in Java
Use the ‘b’ conversion character for month name in Java.
Use the ‘m’ conversion character for month number in Java.
Here, we are using Formatter and Calendar class, therefore import the following packages.
import java.util.Calendar; import java.util.Formatter;
The following is an example to display month name and number −
Example
import java.util.Calendar; import java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f = new Formatter(); Calendar cal = Calendar.getInstance(); System.out.println("Current date and time: "+cal.getTime()); f = new Formatter(); System.out.println(f.format("Month = %tb \nMonth Number = %tm", cal, cal)); } }
Output
Current date and time: Mon Nov 26 07:07:31 UTC 2018 Month = Nov Month Number = 11
Advertisements