Actually i'm planning to make a user registration page over swings and i decided that there should be a constraint that people with age below 21 cannot register. I want to make a validation of the date input of the user by calcualting the age and checking with if loop.
For example :
if my users input is: 21/06/2006
the code should extract the year from the input and from the current date, calculate the age and verify it with If loop.

Is it possible to exrtract the year from the input...
Any Built-in functions??? Pls help in this context my fellow Daniweb users...

Uhm how about the strings substring(int startIndex,int endIndex) or the lastIndexOf(String s) methods. This is really simple, could you show some code you have tried? A quick Google

Use SimpleDateFormat to parse the user input into a Date object. You can then subtract that from today's date to see if the difference is >= 21 years.

commented: nice +8

first, learn the basics. an if statement is not a loop.
also, be beware that anyone can lie in forms like that, so don't consider it to be "fool proof". for the rest, all the above kind off sums it up.

Ya i agree If statement is a condition.
But When you make an aplication there needs to be certain logics that are to be viewed. Even in some websites i myself had lied when i was a kid.

JamesCherrill: I did use the SimpleDateFormat and get the work done. Thanks for your response.But the exception is not getting raised. I tried debugging and came to know that the age is accepted only if >21. At Present my problem is the exception.. And I'm sorry for not posting my code as I am working on my office project, I'm not suppose to post it.

just saying: the Exception is the problem, doesn't tell us anything.
there are lots of Exceptions, not to mention you could easily have created your own. what does your stack trace say?

package mypackage;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class datedifference {
     public static void main(String[] args) throws ParseException {

    Date userdate = new SimpleDateFormat("dd/MM/yyyy").parse("21/06/2011");
    Date    today = new SimpleDateFormat("dd/MM/yyyy").parse("21/06/2012");

    Calendar gc1 = new GregorianCalendar();
    gc1.setTime(today);

    Calendar gc2 = new GregorianCalendar();
    gc2.setTime(userdate);

    long daysdiff = (gc1.getTimeInMillis() - gc2.getTimeInMillis())/(1000*3600*24);
    System.out.println("age in days - " + daysdiff);

     }

}

@nikolaos: no need to go via GregorianCalendar, just use date.getTime();

Calendars are mutable and better for date manipulation.

you need to compare dates, not manipulate them.

Generally speaking , not for this specific case

my point is to create a custom exception stating "Enter a valid Date of Birth" after checking the condition for
"a<=21"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.