Java – Date validation using SimpleDateFormat

I need to write a program which reads a date string. The program then archives all the data on the server with the input date as an cut off date.

The following java program is based on the tutorial in </>. It reads a date string, validate it and print the number of date difference between today and the input date.

import java.util.*;
import java.text.*;

public class DateCounter {

        public static void main(String[] args) {
                SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
                Date inputDate = null;

                // parse the input date string
                try {
                        inputDate = sdf.parse(args[0]);
                } catch (ParseException e) {
                        // Incorrect input date format. Program exits.
                        System.out.println("Incorrect date format: " + args[0]);
                        System.out.println("Valid Date format:     dd/mm/yyyy");

                // Check whether the date is a valid date
                if (!sdf.format(inputDate).equals(args[0])) {
                        // Invalid input date. Program exits.
                        System.out.println("Invalid date input: " + args[0]);
                        System.out.println("Please correct the above input date.");

                /* Validation passed */

                // Get today date
                Date today = new Date();
                System.out.println("Today:     " + sdf.format(today));
                System.out.println("InputDate: " + sdf.format(inputDate));

                // Calculate the Date difference
                long dateDiff = (inputDate.getTime() - today.getTime()) / 86400000;

                // Print the difference
                System.out.println("Still have " + dateDiff + " day(s) to go.");

                /* and do whatever you want ... */

And i also write a shell script which will compiles the and prompt for a date input in shell.


# Complile the

# Get the date input from user
echo "****************"
echo "* Date Counter *"
echo "****************"
echo "Date Format: dd/mm/yyyy"
echo "Please input a date for calculation: \c"
read _inputDate

# Run the DateCounter
java DateCounter $_inputDate

To run this little program, place them in the same folder and enter

For example, i would like to know how many days i have before Tinyan’s birthday.

* Date Counter *
Date Format: dd/mm/yyyy
Please input a date for calculation: 07/11/2009
Today: 21/10/2009
InputDate: 07/11/2009
Still have 17 day(s) to go.


One thought on “Java – Date validation using SimpleDateFormat”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.