How do I get the current month name?
Category: java.util, viewed: 8020 time(s).
To get the current month name from the system we can use java.util.Calendar class. The Calendar.get(Calendar.MONTH) returns the month value as an integer starting from 0 as the first month and 11 as the last month. This mean January equals to 0, February equals to 1 and December equals to 11.
Let's see the code below:
import java.util.Calendar; public class GetMonthNameExample { public static void main(String[] args) { String[] monthName = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; Calendar cal = Calendar.getInstance(); String month = monthName[cal.get(Calendar.MONTH)]; System.out.println("Month name: " + month); } } |
On the first line inside the main method we declare an array of string that keep our month names. Next we get the integer value of the current month and at the last step we look for the month name inside our previously defined array.
The example result of this program is:
Month name: January
Related Examples
- How do I convert milliseconds value to date?
- How do I split a string using Scanner class?
- How do I read file using Scanner class?
- How do I read / write data in Windows registry?
- How do I read user input from console using Scanner class?
- How do I use ResourceBundle for i18n?
- How do I convert time between timezone?
- How do I set a default Locale?
- How do I remove duplicate element from array?
- How do I convert array to Set?
|
|