Java Tutorials


Java 8 Date Time Class

MonthDay class Usage

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

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

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

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

      
Create a MonthDay from String using parse method
      		//MonthDay format should be --MonthNumber-day
      		
		String strMD = "--11-06";
		
		MonthDay md  =  MonthDay.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

     MonthDay 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.

		MonthDay md  =  MonthDay.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 MonthDay object using get method

    MonthDay 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.*;
	
	MonthDay md =  MonthDay.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 MonthDay to String Object using format method

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

MonthDay 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