Date before() method in Java with Examples Last Updated : 02 Jan, 2019 Comments Improve Suggest changes Like Article Like Report The before() method of Java Date class tests whether the date is before the specified date or not. Syntax: public boolean before(Date when) Parameters: The function accepts a single parameter when which specifies the date that has to be checked. Return Value: It returns a Boolean value. It will return true if this date is before the specified date otherwise false. Exception: The function throws a single exception that is NullPointerException if when is null. Program below demonstrates the above mentioned function: Java // Java code to demonstrate // before() function of Date class import java.util.Date; import java.util.Calendar; public class GfG { // main method public static void main(String[] args) { // creating a Calendar object Calendar c = Calendar.getInstance(); // set Month // MONTH starts with 0 i.e. ( 0 - Jan) c.set(Calendar.MONTH, 11); // set Date c.set(Calendar.DATE, 05); // set Year c.set(Calendar.YEAR, 1996); // creating a date object with specified time. Date dateOne = c.getTime(); System.out.println("Date 1: " + dateOne); // creating a date of object // storing the current date Date currentDate = new Date(); System.out.println("Date 2: " + currentDate); System.out.println("Is Date 2 before Date 1: " + currentDate.before(dateOne)); } } Output: Date 1: Thu Dec 05 08:15:01 UTC 1996 Date 2: Wed Jan 02 08:15:01 UTC 2019 Is Date 2 before Date 1: false Java // Java code to demonstrate // before() function of Date class import java.util.Date; public class GfG { // main method public static void main(String[] args) { // creating a date of object // storing the current date Date currentDate = new Date(); System.out.println("Date 1: " + currentDate); // specifiedDate is assigned to null. Date specifiedDate = null; System.out.println("Date 1: " + specifiedDate); System.out.println("On checking these 2 dates: "); try { // throws NullPointerException System.out.println(currentDate .before(specifiedDate)); } catch (Exception e) { System.out.println("Exception: " + e); } } } Output: Date 1: Wed Jan 02 08:15:06 UTC 2019 Date 1: null On checking these 2 dates: Exception: java.lang.NullPointerException Comment T Twinkl Bajaj Follow 0 Improve T Twinkl Bajaj Follow 0 Improve Article Tags : Misc Java Java - util package Java-Functions Java-util-Date +1 More Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings8 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java9 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface4 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like