Java Tutorials


Java 8 Date Time Class

Month Enum class Usage

   Month Enum class represents

Creating a Month Enum Object by assigning one of the predefined constants or it can can be created by using static methods. Month enum has 12 fixed values, each enum represents a Month in the Gregorian Calendar.
      APRIL       AUGUST      DECEMBER    FEBRUARY    JANUARY     JULY        JUNE        MARCH       
	MAY         NOVEMBER    OCTOBER     SEPTEMBER
	
static methods are
  • from
  • of
  • valueOf


Creating Month Object using of,from,valueOf methods or Various was to create a Month Object
 
//using enum constant. 
      		// From JANUARY to DECEMBER, assign one of the value to Create Month Object
               Month  m =  Month.JANUARY;
               
 //using of method
               // of Method in Month class accepts a Number Ranging from 1 to 12
               //Range exceeds these values throws java.time.DateTimeException 
               
               //12 represents DECEMBER month, JANUARY has 1, FEBRUARY has value 2, JULY has value 7 , NOVEMBER Has value 11 etc.,
               Month m = Month.of(12)
               DECEMBER
               
 //using TemporalAcceor,i.e classes which implements TemporalAcceor interface, 
               //like LocalDate, LocalDateTime,ZoneDateTime etc.,
               //Any class in java.time package has Month Field in it, it can be used in from static method
 from               
              Month m = Month.from(LocalDateTime.of(2022,1,1,11,30,00)  //extracts month field only
              // 1 represents  JANUARY
              JANUARY
              
 //using string ,(case sensitive)
              //Each enum constant object can be converted into String,  If you know the String Value of enum constant
              //  pass as param to valueOf method
              Month m =  Month.valueOf("JULY")
              JULY
      

name ,ordinal() & getValue() methods

     Each enum constant in Month class name and ordinal number assigned by Java Compiler.
Usually name is same constant symbol in String form. for ex: Month.JANUARY.name() gives "JANUARY" as String etc.,
Ordinal value assigned from 0 to N-1, First element gets value 0 , next element is 1, last element is N-1 . So JANUARY =0 , FEBRUARY = 1, MARCH =2 ... NOVEMBER = 10, DECEMBER = 11.

		Month m = Month.NOVEMBER;
		
		System.out.println(""m.name());  "NOVEMBER"
		System.out.println(""m.ordinal());  10

	

getValue method in Month Enum class, returns a Number. That represents Month number in the Calendar System. i.e for JANUARY = 1 FEBRUARY = 2 .... NOVEMBER = 11 DECEMBER = 12

Note:While Creating any object like LocalDate, LocalDateTime getValue() method is Used. Not ordinal () method.



firstMonthOfQuarter firstDayOfYear methods

    
Every Year divided into four Quarters, each Quarter has 3 months. so 4 Quarters x 3 months = 12 months

1 st Quarter --> JANUARY, FEBRUARY, MARCH
2 nd Quarter --> APRIL, MAY, JUNE
3 rd Quarter --> JULY, AUGUST, SEPTEMBER
4 th Quarter --> OCTOBER, NOVEMBER, DECEMBER

	   firstMonthOfQuarter()  method returns JANUARY or APRIL or JULY or OCTOBER
	   		
	   	for ex:  
	   		
	   	Month Object represents JANUARY
	   	Month m = Month.JANUARY
	   	Month Quarter = m.firstMonthOfQuarter();//  returns Month Object as JANUARY
	   	
	   	m = Month.JUNE
	   	Quarter =  m.firstMonthOfQuarter;   JUNE falls on second Quarter, so starting Month of second
	   	Quarter is APRIL,  so Quarter variable represents APRIL month
	   		          
	   	similary for other months.
	   


firstDayOfYear(boolean leap)

     Every Year has 365 days, if it is leap year ,number of days are 366.

	 
	 	So Month m  =  Month.MARCH ,
	 	int days = m.firstDayOfYear(false); // days = 60 means
	 	
	 	// JAN month has 31 days + February has 28 Days ,March 1 day so 31 + 28 + 1 = 60 days
	 	
	 	For example Month object represents  DECEMBER
	 	
	 	         m  =  Month.DECEMVBER
	 	         
	 	         JAN = 31 Days, FEB = 28 MAR = 31 APR 30 May 31 
	 	         Jun 30 JUL 31  AUG 31 SEP 30 OCT 31 NOV 30 DEC 31  Days
	 	         
                       So You need to sum all days from JAN to NOVEMBER 	 	         
                       
                           31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 +30 + 31 +30  = 334 (for non leap Year) 
                           
                           + 1 (For December )
                           
                           = 335 days
                           
                           System.out.println(m.firstDayOfYear(false));
                           335 days
                           
	 		
	 

ADS