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 </dream.in.code>. It reads a date string, validate it and print the number of date difference between today and the input date.

DateCounter.java

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");
                        return;
                }

                // 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.");
                        return;
                }

                /* 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;
                dateDiff++;

                // 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 DateCounter.java and prompt for a date input in shell.

run_date_counter.sh

#!/bin/sh

# Complile the DateCounter.java
javac DateCounter.java

# 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
sh run_date_counter.sh

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.

About these ads

One thought on “Java – Date validation using SimpleDateFormat

  1. Pingback: Java – Convert Date to Formatted String | Eureka!

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s