Easy learning with example program codes

Java 8 YearMonth class

The java.time.YearMonth class is an immutable date-time object that represents the combination of a year and month.

Java YearMonth class methods

Method Description
Temporal adjustInto(Temporal temporal) It is used to adjust the specified temporal object to have this year-month.
String format(DateTimeFormatter formatter) It is used to format this year-month using the specified formatter.
int get(TemporalField field) It is used to get the value of the specified field from this year-month as an int.
boolean isLeapYear() It is used to check if the year is a leap year, according to the ISO proleptic calendar system rules.
static YearMonth now() It is used to obtain the current year-month from the system clock in the default time zone.
static YearMonth of(int year, int month) It is used to obtain an instance of YearMonth from a year and month.
YearMonth plus(TemporalAmount amountToAdd) It is used to return a copy of this year-month with the specified amount added.
YearMonth minus (TemporalAmount amountToSubtract) It is used to return a copy of this year-month with the specified amount subtracted.


package com.codesjava;
import java.time.Period;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
public class TestExample {
	public static void main(String args[]){
		YearMonth yearMonth =;  
		//format() method
		System.out.println(yearMonth.format(DateTimeFormatter.ofPattern("MM yyyy")));
		//plus() method
		//minus() method


04 2018
Posted in Date   

Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at


We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on

Copyright © 2019 CodesJava Protection Status SiteMap Reference: Java Wiki