MonthDay isValidYear() Method in Java

Java 8Object Oriented ProgrammingProgramming

It can be checked if a year is valid or not for a MonthDay object using the isValidYear() method in the MonthDay class in Java. This method requires a single parameter i.e. the year which is to be checked. Also, it returns true if the year is valid for a MonthDay object and false if the year is not valid for a MonthDay object.

A program that demonstrates this is given as follows

Example

 Live Demo

import java.time.*;
public class Demo {
   public static void main(String[] args) {
      MonthDay md = MonthDay.parse("--02-21");
      System.out.println("The MonthDay is: " + md);
      System.out.println("Year 2019 is valid for the MonthDay? " + md.isValidYear(2019));
   }
}

Output

The MonthDay is: --02-21
Year 2019 is valid for the MonthDay? true

Now let us understand the above program.

First the current MonthDay object is displayed. Then it is checked if a year is valid or not for the MonthDay object using the isValidYear() method. The returned value is printed. A code snippet that demonstrates this is as follows:

MonthDay md = MonthDay.parse("--02-21");
System.out.println("The MonthDay is: " + md);
System.out.println("Year 2019 is valid for the MonthDay? " + md.isValidYear(2019));
raja
Updated on 30-Jul-2019 22:30:25

Advertisements