How to Validate Date Format

I'm developing a user detail submition form in which I have to get users date of birth.
How to validate date format so that nobody can enter invalid date like 30 feb, 31 apr etc. in textfield or combobox. I'm also developing a online testing system in jsp.  If a user not attempt all question is shows a error. How to over come this problem.

Gaurav Jain
 

I've attached the file to check for invalid date.. lets say the date format s mm/dd/yyyy...
with this as assumption, I've given you the sample code..hope this helps you out..

With reagards to the second prob:::: what I have understood is that, if the user does not attempt any quesiton u want to throw an error,...is this what you want to handle?? with this assumption, I want to get an info from you...

Do you have a checkbox to indicate if the question has been attempted or not?? Or is there any other way by which you say if the question is attempted or not??  Based on this I think we can try to get the question collection... if it is a checkbox try to iterate thro' that collection and check if the checkbox is set to true for all questions and then throw an error msg..

Do get back if you got any doubts..

 public static boolean testDate(String str) {
        int Jan = 31;
        int Mar = 31;
        int Apr = 30;
        int May = 31;
        int Jun = 30;
        int Jul = 31;
        int Aug = 31;
        int Sep = 30;
        int Oct = 31;
        int Nov = 30;
        int Dec = 31;

        boolean flag = true;
        StringTokenizer st = new StringTokenizer(str, "/");

        int month = Integer.parseInt(st.nextToken());
        int day = Integer.parseInt(st.nextToken());
        int year = Integer.parseInt(st.nextToken());

        if ((month > 12) && (month < 100)) {
            flag = false;
        }

        switch (month) {
        case 1:

            if (day > Jan) {
                flag = false;
            }

            break;

        case 2:

            if ((year % 4) == 0) {
                if (day > 29) {
                    flag = false;
                }
            } else {
                if (day > 28) {
                    flag = false;
                }
            }

        case 3:

            if (day > Mar) {
                flag = false;
            }

            break;

        case 4:

            if (day > Apr) {
                flag = false;
            }

            break;

        case 5:

            if (day > May) {
                flag = false;
            }

            break;

        case 6:

            if (day > Jun) {
                flag = false;
            }

            break;

        case 7:

            if (day > Jul) {
                flag = false;
            }

            break;

        case 8:

            if (day > Aug) {
                flag = false;
            }

            break;

        case 9:

            if (day > Sep) {
                flag = false;
            }

            break;

        case 10:

            if (day > Oct) {
                flag = false;
            }

            break;

        case 11:

            if (day > Nov) {
                flag = false;
            }

            break;

        case 12:

            if (day > Dec) {
                flag = false;
            }

            break;
        }

        return flag;
    }
Kiruthiga

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.