Skip to content
Related Articles
Open in App
Not now

Related Articles

YearMonth isBefore() method in Java with Examples

Improve Article
Save Article
  • Last Updated : 30 Nov, 2018
Improve Article
Save Article

The isBefore() method of Year class in Java is used to check if this current YearMonth object is before the YearMonth specified as parameter to this method.

Syntax:

public boolean isBefore(Year otherYearMonth)

Parameter: It accepts a single parameter otherYearMonth with which the current YearMonth object is to be compared.

Return Value: It returns a boolean True value if this YearMonth object’s value is before the value of YearMonth object specified as a parameter to the method, otherwise it returns False.

Below programs illustrate the YearMonth.isBefore() method in Java:

Program 1:




// Program to illustrate the isBefore() method
  
import java.util.*;
import java.time.*;
  
public class GfG {
    public static void main(String[] args)
    {
        // Create first Year object
        YearMonth yearMonth1
            = YearMonth.of(2018, 3);
  
        // Create second Year object
        YearMonth yearMonth2
            = YearMonth.of(1997, 4);
  
        // Check if this year-month object's value is
        // before the specified Year-month or not
        System.out.println(yearMonth1
                               .isBefore(yearMonth2));
    }
}


Output:

false

Program 2:




// Program to illustrate the isBefore() method
  
import java.util.*;
import java.time.*;
  
public class GfG {
    public static void main(String[] args)
    {
        // Create first Year object
        YearMonth yearMonth1
            = YearMonth.of(1997, 3);
  
        // Create second Year object
        YearMonth yearMonth2
            = YearMonth.of(2018, 4);
  
        // Check if this year-month object's value is
        // before the specified Year-month or not
        System.out.println(yearMonth1
                               .isBefore(yearMonth2));
    }
}


Output:

true

Reference: https://docs.oracle.com/javase/8/docs/api/java/time/YearMonth.html#isBefore-java.time.YearMonth-


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!