Java Tutorials


Java 8 Date Time Class

YearMonth class Usage

   YearMonth class represents Represents Month and Day of the Month Fields only, It doesnt represent Year Field, Actually ISO-8601 Calendar System.

Creating a YearMonth Object.
Using static methods
  • from
  • now
  • of
  • parse
Create a YearMonth using static method now
      YearMonth md = YearMonth.now()
	System.out.println(md)
	"--02-08"
	i.e February Month 8th Day. 
      
Create a YearMonth using static method of
jshell> YearMonth md=YearMonth.of(Month.JULY, 12)
md ==> --07-12

jshell> YearMonth md=YearMonth.of(7, 12)
md ==> --07-12
      
      
Create a YearMonth using static method from
jshell> YearMonth md = YearMonth.from(LocalDate.now())
md ==> --02-08

jshell> YearMonth md = YearMonth.from(LocalDateTime.now())
md ==> --02-08

      
Create a YearMonth from String using parse method
      		//YearMonth format should be --MonthNumber-day
      		
		String strMD = "--11-06";
		
		YearMonth md  =  YearMonth.parse(strMD);
		
		Month m = md.getMonth();
		int month = md.getMonthValue();
		
		int day = md.getDayOfMonth();
		
		System.out.println("Month:"+m+" month value:"+month+" day:"+day);

		Month:NOVEMBER month value:11 day:6
      


Create LocalDate object using atYear method

     YearMonth object represents Month and Day Fields, It require Year Field to generate LocalDate object. So atYear(int year) method takes year as input. and returns LocalDate object.

		YearMonth md  =  YearMonth.of( 7, 12);
		System.out.println(md);
		"--07-12"
		LocalDate dt = md.atYear(1973)
		System.out.println(dt);
		"1973-07-12"


Query Month and Day from YearMonth object using get method

    YearMonth Object represents Month and Day of the Month Fields only, so it can be Queried using ChronoField enums. as shown below ChronoField has 2 enums MONTH_OF_YEAR, DAY_OF_MONTH to query Month and Day of the Month Respectively.

	import java.time.*;
	import java.time.temporal.*;
	
	YearMonth md =  YearMonth.of(11, 11);
	System.out.println(md);
	"--11-11"
	
	int month = md.get(ChronoField.MONTH_OF_YEAR);
	int day = md.get(ChronoField.DAY_OF_MONTH);
	
	System.out.println("Month Of Year: "+month+" Day of the Month: "+day);
	"Month Of Year: 11 Day of the Month: 11"

Convert YearMonth to String Object using format method

     DateTimeFormatter class localted in java.time.format package, ofPattern static method takes format required to convert YearMonth object into a String

YearMonth has only two fields Month and Day
  	MM  --> numeric month from 1 to 12
  	MMM --> Represents short Month, i.e first 3 letter of the Month, JAN,JUL,SEP,DEC etc.,
  	MMMM --> Full name of the Month
  
import java.time.format.*;

String str = md.format(DateTimeFormatter.ofPattern("MM-dd"))

System.out.println("Month Number-day");
"02-08"

String str = md.format(DateTimeFormatter.ofPattern("MMM-dd"))

System.out.println("Month Short Form-Day")

"Feb-08"

String str = md.format(DateTimeFormatter.ofPattern("MMMM-dd"))

System.out.println("Month Full Name-Day");
"February-08"


ADS