
In the previous article we looked at the DayOfWeek Enum from the Java 8 Date-Time API. Now let us take a look at the Month's enum.
The Month enum contains the singleton instances of the 12 months of the year as below,
- JANUARY :int value = 1
- FEBRUARY :int value = 2
- MARCH :int value = 3
- APRIL :int value = 4
- MAY :int value = 5
- JUNE :int value = 6
- JULY :int value = 7
- AUGUST :int value = 8
- SEPTEMBER :int value = 9
- OCTOBER :int value = 10
- NOVEMBER :int value = 11
- DECEMBER :int value = 12
Month january = Month.JANUARY;
Month march = Month.MARCH;
Month december = Month.DECEMBER;
System.out.println(january);
System.out.println(march);
System.out.println(december);
Output:
JANUARY
MARCH
DECEMBER
Example 2: Get the month of year numeric value
int january = Month.JANUARY.getValue();
int march = Month.MARCH.getValue();
int december = Month.DECEMBER.getValue();
System.out.println(january);
System.out.println(march);
System.out.println(december);
Output:
1
3
12
Example 3: Get number of days in a month
To get the number of days in a month we can make use of length(), minLength() and maxLength()
int january = Month.JANUARY.maxLength();
int february = Month.FEBRUARY.maxLength();
int february1 = Month.FEBRUARY.minLength();
int february2 = Month.FEBRUARY.length(true);
int february3 = Month.FEBRUARY.length(false);
System.out.println(january);
System.out.println(february);
System.out.println(february1);
System.out.println(february1);
System.out.println(february2);
System.out.println(february3);
Output:
31
29
28
28
29
28
Example 4: Get Month names with TypeStyle:
import java.time.Month;
import java.time.format.TextStyle;
import java.util.Locale;
public class Example {
public static void main(String... args) {
getMonthWithType(Month.JANUARY,Locale.US);
getMonthWithType(Month.FEBRUARY,Locale.US);
getMonthWithType(Month.MARCH,Locale.US);
getMonthWithType(Month.APRIL,Locale.US);
getMonthWithType(Month.MAY,Locale.US);
getMonthWithType(Month.JUNE,Locale.US);
getMonthWithType(Month.JULY,Locale.US);
getMonthWithType(Month.AUGUST,Locale.US);
getMonthWithType(Month.SEPTEMBER,Locale.US);
getMonthWithType(Month.OCTOBER,Locale.US);
getMonthWithType(Month.NOVEMBER,Locale.US);
getMonthWithType(Month.DECEMBER,Locale.US);
}
private static void getMonthWithType(Month month, Locale locale) {
String monthName = "Month" + month.name() +"\n";
String fullName = "FULL: "+ month.getDisplayName(TextStyle.FULL,locale) +"\n";
String fullStandAloneName = "FULL_STANDALONE: "+ month.getDisplayName(TextStyle.FULL_STANDALONE,locale) +"\n";
String shortName = "SHORT: "+ month.getDisplayName(TextStyle.SHORT,locale) +"\n";
String shortStandaloneName = "SHORT_STANDALONE: "+ month.getDisplayName(TextStyle.SHORT_STANDALONE,locale) +"\n";
String narrowName = "NARROW: "+ month.getDisplayName(TextStyle.NARROW,locale) +"\n";
String narrowStandAloneName = "NARROW_STANDALONE: "+ month.getDisplayName(TextStyle.NARROW_STANDALONE,locale) +"\n";
System.out.println(monthName + fullName + fullStandAloneName + shortName + shortStandaloneName + narrowName + narrowStandAloneName);
}
}
Output:
MonthJANUARY
FULL: January
FULL_STANDALONE: January
SHORT: Jan
SHORT_STANDALONE: Jan
NARROW: J
NARROW_STANDALONE: J
MonthFEBRUARY
FULL: February
FULL_STANDALONE: February
SHORT: Feb
SHORT_STANDALONE: Feb
NARROW: F
NARROW_STANDALONE: F
MonthMARCH
FULL: March
FULL_STANDALONE: March
SHORT: Mar
SHORT_STANDALONE: Mar
NARROW: M
NARROW_STANDALONE: M
MonthAPRIL
FULL: April
FULL_STANDALONE: April
SHORT: Apr
SHORT_STANDALONE: Apr
NARROW: A
NARROW_STANDALONE: A
MonthMAY
FULL: May
FULL_STANDALONE: May
SHORT: May
SHORT_STANDALONE: May
NARROW: M
NARROW_STANDALONE: M
MonthJUNE
FULL: June
FULL_STANDALONE: June
SHORT: Jun
SHORT_STANDALONE: Jun
NARROW: J
NARROW_STANDALONE: J
MonthJULY
FULL: July
FULL_STANDALONE: July
SHORT: Jul
SHORT_STANDALONE: Jul
NARROW: J
NARROW_STANDALONE: J
MonthAUGUST
FULL: August
FULL_STANDALONE: August
SHORT: Aug
SHORT_STANDALONE: Aug
NARROW: A
NARROW_STANDALONE: A
MonthSEPTEMBER
FULL: September
FULL_STANDALONE: September
SHORT: Sep
SHORT_STANDALONE: Sep
NARROW: S
NARROW_STANDALONE: S
MonthOCTOBER
FULL: October
FULL_STANDALONE: October
SHORT: Oct
SHORT_STANDALONE: Oct
NARROW: O
NARROW_STANDALONE: O
MonthNOVEMBER
FULL: November
FULL_STANDALONE: November
SHORT: Nov
SHORT_STANDALONE: Nov
NARROW: N
NARROW_STANDALONE: N
MonthDECEMBER
FULL: December
FULL_STANDALONE: December
SHORT: Dec
SHORT_STANDALONE: Dec
NARROW: D
NARROW_STANDALONE: D
Example 5: Locale example with Month:
Locale.FRANCE
Month: JANUARY
FULL: janvier
FULL_STANDALONE: janvier
SHORT: janv.
SHORT_STANDALONE: janv.
NARROW: J
NARROW_STANDALONE: J
Month: FEBRUARY
FULL: février
FULL_STANDALONE: février
SHORT: févr.
SHORT_STANDALONE: févr.
NARROW: F
NARROW_STANDALONE: F
Month: MARCH
FULL: mars
FULL_STANDALONE: mars
SHORT: mars
SHORT_STANDALONE: mars
NARROW: M
NARROW_STANDALONE: M
Month: APRIL
FULL: avril
FULL_STANDALONE: avril
SHORT: avr.
SHORT_STANDALONE: avr.
NARROW: A
NARROW_STANDALONE: A
Month: MAY
FULL: mai
FULL_STANDALONE: mai
SHORT: mai
SHORT_STANDALONE: mai
NARROW: M
NARROW_STANDALONE: M
Month: JUNE
FULL: juin
FULL_STANDALONE: juin
SHORT: juin
SHORT_STANDALONE: juin
NARROW: J
NARROW_STANDALONE: J
Month: JULY
FULL: juillet
FULL_STANDALONE: juillet
SHORT: juil.
SHORT_STANDALONE: juil.
NARROW: J
NARROW_STANDALONE: J
Month: AUGUST
FULL: août
FULL_STANDALONE: août
SHORT: août
SHORT_STANDALONE: août
NARROW: A
NARROW_STANDALONE: A
Month: SEPTEMBER
FULL: septembre
FULL_STANDALONE: septembre
SHORT: sept.
SHORT_STANDALONE: sept.
NARROW: S
NARROW_STANDALONE: S
Month: OCTOBER
FULL: octobre
FULL_STANDALONE: octobre
SHORT: oct.
SHORT_STANDALONE: oct.
NARROW: O
NARROW_STANDALONE: O
Month: NOVEMBER
FULL: novembre
FULL_STANDALONE: novembre
SHORT: nov.
SHORT_STANDALONE: nov.
NARROW: N
NARROW_STANDALONE: N
Month: DECEMBER
FULL: décembre
FULL_STANDALONE: décembre
SHORT: déc.
SHORT_STANDALONE: déc.
NARROW: D
NARROW_STANDALONE: D
Locale.CHINESE
Month: JANUARY
FULL: 一月
FULL_STANDALONE: 一月
SHORT: 1月
SHORT_STANDALONE: 1月
NARROW: 1
NARROW_STANDALONE: 1
Month: FEBRUARY
FULL: 二月
FULL_STANDALONE: 二月
SHORT: 2月
SHORT_STANDALONE: 2月
NARROW: 2
NARROW_STANDALONE: 2
Month: MARCH
FULL: 三月
FULL_STANDALONE: 三月
SHORT: 3月
SHORT_STANDALONE: 3月
NARROW: 3
NARROW_STANDALONE: 3
Month: APRIL
FULL: 四月
FULL_STANDALONE: 四月
SHORT: 4月
SHORT_STANDALONE: 4月
NARROW: 4
NARROW_STANDALONE: 4
Month: MAY
FULL: 五月
FULL_STANDALONE: 五月
SHORT: 5月
SHORT_STANDALONE: 5月
NARROW: 5
NARROW_STANDALONE: 5
Month: JUNE
FULL: 六月
FULL_STANDALONE: 六月
SHORT: 6月
SHORT_STANDALONE: 6月
NARROW: 6
NARROW_STANDALONE: 6
Month: JULY
FULL: 七月
FULL_STANDALONE: 七月
SHORT: 7月
SHORT_STANDALONE: 7月
NARROW: 7
NARROW_STANDALONE: 7
Month: AUGUST
FULL: 八月
FULL_STANDALONE: 八月
SHORT: 8月
SHORT_STANDALONE: 8月
NARROW: 8
NARROW_STANDALONE: 8
Month: SEPTEMBER
FULL: 九月
FULL_STANDALONE: 九月
SHORT: 9月
SHORT_STANDALONE: 9月
NARROW: 9
NARROW_STANDALONE: 9
Month: OCTOBER
FULL: 十月
FULL_STANDALONE: 十月
SHORT: 10月
SHORT_STANDALONE: 10月
NARROW: 10
NARROW_STANDALONE: 10
Month: NOVEMBER
FULL: 十一月
FULL_STANDALONE: 十一月
SHORT: 11月
SHORT_STANDALONE: 11月
NARROW: 11
NARROW_STANDALONE: 11
Month: DECEMBER
FULL: 十二月
FULL_STANDALONE: 十二月
SHORT: 12月
SHORT_STANDALONE: 12月
NARROW: 12
NARROW_STANDALONE: 12
Locale.GERMAN
Month: JANUARY
FULL: Januar
FULL_STANDALONE: Januar
SHORT: Jan.
SHORT_STANDALONE: Jan
NARROW: J
NARROW_STANDALONE: J
Month: FEBRUARY
FULL: Februar
FULL_STANDALONE: Februar
SHORT: Feb.
SHORT_STANDALONE: Feb
NARROW: F
NARROW_STANDALONE: F
Month: MARCH
FULL: März
FULL_STANDALONE: März
SHORT: März
SHORT_STANDALONE: Mär
NARROW: M
NARROW_STANDALONE: M
Month: APRIL
FULL: April
FULL_STANDALONE: April
SHORT: Apr.
SHORT_STANDALONE: Apr
NARROW: A
NARROW_STANDALONE: A
Month: MAY
FULL: Mai
FULL_STANDALONE: Mai
SHORT: Mai
SHORT_STANDALONE: Mai
NARROW: M
NARROW_STANDALONE: M
Month: JUNE
FULL: Juni
FULL_STANDALONE: Juni
SHORT: Juni
SHORT_STANDALONE: Jun
NARROW: J
NARROW_STANDALONE: J
Month: JULY
FULL: Juli
FULL_STANDALONE: Juli
SHORT: Juli
SHORT_STANDALONE: Jul
NARROW: J
NARROW_STANDALONE: J
Month: AUGUST
FULL: August
FULL_STANDALONE: August
SHORT: Aug.
SHORT_STANDALONE: Aug
NARROW: A
NARROW_STANDALONE: A
Month: SEPTEMBER
FULL: September
FULL_STANDALONE: September
SHORT: Sep.
SHORT_STANDALONE: Sep
NARROW: S
NARROW_STANDALONE: S
Month: OCTOBER
FULL: Oktober
FULL_STANDALONE: Oktober
SHORT: Okt.
SHORT_STANDALONE: Okt
NARROW: O
NARROW_STANDALONE: O
Month: NOVEMBER
FULL: November
FULL_STANDALONE: November
SHORT: Nov.
SHORT_STANDALONE: Nov
NARROW: N
NARROW_STANDALONE: N
Month: DECEMBER
FULL: Dezember
FULL_STANDALONE: Dezember
SHORT: Dez.
SHORT_STANDALONE: Dez
NARROW: D
NARROW_STANDALONE: D
More Posts related to Java,
- How to Get List of All Country Codes in Java Using Locale Class
- Unsupported major.minor version 52.0 in java
- Java - How to set custom thread name?
- Get the current timestamp in Java
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- [fix] NullPointerException Cannot Invoke findById because Repository is null - Java Spring
- java: unclosed string literal [Error]
- Convert Java Byte Array to String with code examples
- Error: Can not find the tag library descriptor for
- Java 8 - Convert List to Map Examples
- Java - Calculate time taken for the code to execute in milliseconds or nanoseconds
- Fix java.net.ProtocolException: Invalid HTTP method
- Java: Convert Stream to List
- Java equals method - Tutorial
- List of Java JDBC Database Driver Jars, Classes and URLs Details
- Read YAML file Java Jackson Library
- How to display Java Date Time timezone GMT/UTC offset using SimpleDateFormat
- List of Java Keywords
- Enable JSON Pretty Print in Java Jackson
- How to Word-Warp Console logs in IntelliJ
- Convert Map to List in Java 8 using Stream API
- Create a Directory using Java Code
- Ways to Convert Integer or int to Long in Java
- [Program] How to read three different values using Scanner in Java
- Java JDBC Example with Oracle Database Driver Connection
More Posts:
- Python range() function examples - Python
- [fix] Java JDBC SQLSyntaxErrorException: Unknown database - Java
- SharePoint Server 2016 error - Failed to connect to the configuration database - SharePoint
- Android : DeviceMonitor] Sending Tracking request failed! Error - Android
- Unable to find image docker latest locally - Docker
- Error code - 7: There's a more permanent way to sign in to Microsoft Teams - Teams
- How to view the desktop when using macOS Stage Manager? - MacOS
- New Mac? How to install Native Chrome on M1/M2 Mac - Chrome