Monday, November 18, 2024
Google search engine
HomeLanguagesJavajava.time.YearMonth Class in Java

java.time.YearMonth Class in Java

Java YearMonth class provides the output of the format “year-month”. This class is an immutable class which means that it defines objects which, once created, never change their value. Any field which will be derived from a year and month, like quarter-of-year, are often obtained. This class does not store or represent a day, time or time-zone. For example, it would not show the value “November-2006-12:00” rather than it will show “2007-11”. It inherits the thing class and implements the Comparable interface.

Java YearMonth class implements Temporal, TemporalAdjuster, Comparable<YearMonth>, Serializable

public final class YearMonth extends Object    

implements Temporal, TemporalAdjuster, Comparable<YearMonth>, Serializable  

Method Description
adjustInto(Temporal temporal) This method adjusts the specified temporal object to have this year-month.
atDay(int dayOfMonth) This method combines this year-month with a day-of-month to create a LocalDate.
atEndOfMonth() This method returns a LocalDate at the end of the month.
compareTo(YearMonth other) This method compares this year-month to another year-month.
equals(Object obj) This method checks if this year-month is equal to another year-month.
format(DateTimeFormatter formatter) This method Formats this year-month using the specified formatter.
from(TemporalAccessor temporal) This method obtains an instance of YearMonth from a temporal object.
get(TemporalField field) This method gets the value of the specified field from this year-month as an int.
 getLong(TemporalField field) This method gets the value of the specified field from this year-month as along.
getMonth() This method gets the month-of-year field using the Month enum.
 getMonthValue() This method gets the month-of-year field from 1 to 12.
getYear() This method gets the year field.
hashCode() A hash code for this year-month.
isAfter(YearMonth other) This method checks if this year-month is after the specified year-month.
isBefore(YearMonth other) This method checks if this year-month is before the specified year-month.
isLeapYear() This method checks if the year is a leap year, according to the ISO proleptic calendar system rules.
 isSupported(TemporalField field) This method checks if the specified field is supported.
isSupported(TemporalUnit unit) This method checks if the specified unit is supported.
isValidDay(int dayOfMonth) This method checks if the day-of-month is valid for this year-month.
lengthOfMonth() This method returns the length of the month, taking account of the year.
lengthOfYear() This method returns the length of the year.
minus(long amountToSubtract, TemporalUnit unit) This method returns a copy of this year-month with the specified amount subtracted.
minus(TemporalAmount amountToSubtract) This method returns a copy of this year-month with the specified amount subtracted.
minusMonths(long monthsToSubtract) This method returns a copy of this YearMonth with the specified number of months subtracted.
minusYears(long yearsToSubtract) This method returns a copy of this YearMonth with the specified number of years subtracted.
now() This method obtains the current year-month from the system clock in the default time-zone.
now(Clock clock) This method obtains the current year-month from the specified clock.
now(ZoneId zone) This method obtains the current year-month from the system clock in the specified time-zone.
of(int year, int month) This method obtains an instance of YearMonth from a year and month.
of(int year, Month month) This method obtains an instance of YearMonth from a year and month.
parse(CharSequence text) This method obtains an instance of YearMonth from a text string such as 2007-12.
parse(CharSequence text, DateTimeFormatter formatter) This method obtains an instance of YearMonth from a text string using a specific formatter.
 plus(long amountToAdd, TemporalUnit unit) This method returns a copy of this year-month with the specified amount added.
 plus(TemporalAmount amountToAdd) This method returns a copy of this year-month with the specified amount added.
plusMonths(long monthsToAdd) This method returns a copy of this YearMonth with the specified number of months added.
plusYears(long yearsToAdd) This method returns a copy of this YearMonth with the specified number of years added.
query(TemporalQuery<R> query) This method queries this year-month using the specified query.
range(TemporalField field) This method gets the range of valid values for the specified field.
toString() This method outputs this year-month as a String, such as 2007-12.
until(Temporal endExclusive, TemporalUnit unit) This method calculates the amount of time until another year-month in terms of the specified unit.
with(TemporalAdjuster adjuster) This method returns an adjusted copy of this year-month.
with(TemporalField field, long newValue) This method returns a copy of this year-month with the specified field set to a new value.
withMonth(int month) This method returns a copy of this YearMonth with the month-of-year altered.
withYear(int year) This method returns a copy of this YearMonth with the year altered.

Below is the implementation of some methods of the Java YearMonth class:

1. plus(): Returns a replica of this year-month with the required amount added.

Java




// plus() Method implementation
import java.time.*;
public class Example {
    public static void main(String[] args)
    {
        YearMonth obj1 = YearMonth.now();
  
        // plus(Period.ofYears(int)) will add
        // no.of years to the existing year
        YearMonth obj2 = obj1.plus(Period.ofYears(0));
        System.out.println(obj2);
    }
}


Output

2021-02

2. minus(): Returns a replica of this year-month with the required amount subtracted.

Java




// minus() method implementation
import java.time.*;
public class Example {
    public static void main(String[] args)
    {
        YearMonth obj1 = YearMonth.now();
  
        //.minus(Period.ofYears(int)) will subtract
        // no. ofyears from the existing year
        YearMonth obj2 = obj1.minus(Period.ofYears(3));
        System.out.println(obj2);
    }
}


Output

2018-02
Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments