MonthDay isBefore() Method in Java

Java 8Object Oriented ProgrammingProgramming

It can be checked if a particular MonthDay is before the other MonthDay in a timeline using the isBefore() method in the MonthDay class in Java. This method requires a single parameter i.e. the MonthDay object that is to be compared. It returns true if the MonthDay object is before the other MonthDay object and false otherwise.

A program that demonstrates this is given as follows

Example

 Live Demo

import java.time.*;
public class Main {
   public static void main(String[] args) {
      MonthDay md1 = MonthDay.parse("--02-15");
      MonthDay md2 = MonthDay.parse("--02-21");
      System.out.println("The MonthDay md1 is: " + md1);
      System.out.println("The MonthDay md2 is: " + md2);
      boolean flag = md1.isBefore(md2);
      if(flag)
         System.out.println("\nMonthDay object md1 is before MonthDay object md2");
      else
         System.out.println("\nMonthDay object md1 is after MonthDay object md2");
   }
}

output

The MonthDay md1 is: --02-15
The MonthDay md2 is: --02-21
MonthDay object md1 is before MonthDayobject md2

Now let us understand the above program.

The two MonthDay objects md1 and md2 are displayed. It is checked if the MonthDay object md1 is before the MonthDay object md2 in the timeline using the isBefore() method. The returned value is displayed using an if statement. A code snippet that demonstrates this is as follows:

MonthDay md1 = MonthDay.parse("--02-15");
MonthDay md2 = MonthDay.parse("--02-21");
System.out.println("The MonthDay md1 is: " + md1);
System.out.println("The MonthDay md2 is: " + md2);
boolean flag = md1.isBefore(md2);
if(flag)
   System.out.println("\nMonthDay object md1 is before MonthDay object md2");
else
   System.out.println("\nMonthDay object md1 is after MonthDay object md2");
raja
Updated on 30-Jul-2019 22:30:25

Advertisements