.after()
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.
Syntax
firstDate.after(secondDate)
The Date.after()
takes only one parameter:
secondDate
: ADate
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: trueIs firstDate after secondDate: falseException in thread "main" java.lang.NullPointerExceptionat 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)
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Java
Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.Beginner Friendly17 hours