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 More infoAdvertise with us Next Article Date before() method in Java with Examples T Twinkl Bajaj Follow Improve Article Tags : Misc Java Java - util package Java-Functions Java-util-Date +1 More Practice Tags : JavaMisc Similar Reads Date compareTo() method in Java with examples The compareTo() method of Java Date class compares two dates and sort them for order. Syntax: public int compareTo(Date anotherDate) Parameters: The function accepts a single parameter anotherDate which specifies the date to be compared . Return Value: The function gives three return values specifie 2 min read Date from() method in Java with Examples The from(Instant inst) method of Java Date class returns an instance of date which is obtained from an Instant object. Syntax: public static Date from(Instant inst) Parameters: The method takes one parameter inst of Instant type which is required to be converted. Return Value: The method returns a d 2 min read Date equals() method in Java with Examples The equals() method of Java Date class checks if two Dates are equal, based on millisecond difference. Syntax: public boolean equals(Object obj) Parameters: The function accepts a single parameter obj which specifies the object to be compared with. Return Value: The function gives 2 return values sp 2 min read DateFormat format() Method in Java with Examples DateFormat class present inside java.text package is an abstract class that is used to format and parse dates for any locale. It allows us to format date to text and parse text to date. DateFormat class provides many functionalities to obtain, format, parse default date/time. DateFormat class extend 2 min read Date getTime() method in Java with Examples The getTime() method of Java Date class returns the number of milliseconds since January 1, 1970, 00:00:00 GTM which is represented by Date object. Syntax: public long getTime() Parameters: The function does not accept any parameter. Return Value: It returns the number of milliseconds since January 2 min read Like