How to Check If Date Is Within Range in Java


This tutorial would help us check whether a date is in a given range in Java.

Source Code

package com.beginner.examples;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class CheckDateExample {

    /**
     * @param args
     */
    public static void main(String[] args) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date d1 = sdf.parse("2017-11-21");
        Date d2 = sdf.parse("2015-11-02");
        Date d3 = sdf.parse("2019-04-09");
        System.out.println(Validate(d1, d2, d3));

    }

    static boolean Validate(Date d1, Date d2, Date d3) {

        if (d1.after(d2) && d1.before(d3)) {
            return true;
        }
        return false;
    }

}

Output:

true

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments