Java .after()

frhad_'s avatar
Published Jul 11, 2023
Contribute to Docs

The Date.after() method checks whether a date occurs after a specified date and returns a boolean value.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
    • Beginner Friendly.
      17 hours

Syntax

firstDate.after(secondDate)

The Date.after() takes only one parameter:

  • secondDate: A Date object to be compared chronologically with the first date.

It returns true if the firstDate is after the secondDate and returns false if the firstDate is before the secondDate. However, if the secondDate is null, it will raise a NullPointerException.

Example

The following example shows the implementation of Date.after():

import java.util.Date;
public class Main {
public static void main(String[] args) {
Date firstDate = new Date(2020, 1, 18);
Date secondDate = new Date(2020, 1, 19);
Date thirdDate = null;
System.out.println("Is secondDate after firstDate: " + secondDate.after(firstDate));
System.out.println("Is firstDate after secondDate: " + firstDate.after(secondDate));
System.out.println("Is thirdDate after firstDate:" + firstDate.after(thirdDate));
}
}

The output will be:

Is secondDate after firstDate: true
Is firstDate after secondDate: false
Exception in thread "main" java.lang.NullPointerException
at java.base/java.util.Date.getMillisOf(Date.java:956)
at java.base/java.util.Date.after(Date.java:929)
at Main.main(Main.java:12)

All contributors

Contribute to Docs

Learn Java on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
    • Beginner Friendly.
      17 hours