Date before() method in Java with Examples
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 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)); } } |
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 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); } } } |
Date 1: Wed Jan 02 08:15:06 UTC 2019 Date 1: null On checking these 2 dates: Exception: java.lang.NullPointerException
Recommended Posts:
- Date from() method in Java with Examples
- Date setTime() method in Java with Examples
- Date compareTo() method in Java with examples
- Date getTime() method in Java with Examples
- Date hashCode() method in Java with Examples
- Date clone() method in Java with Examples
- Date toInstant() Method in Java with Examples
- Date equals() method in Java with Examples
- Date toString() method in Java with Examples
- Date class in Java (With Examples)
- util.date class methods in Java with Examples
- Date after() method in Java
- DateFormat format(Date obj) method in Java
- Java lang.Long.highestOneBit() method in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



