wood burning stoves*
The moose likes Java in General and the fly likes taking date input from user in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "taking date input from user in java" Watch "taking date input from user in java" New topic
Author

taking date input from user in java

thejwal pavithran
Ranch Hand

Joined: Feb 11, 2012
Posts: 118
hi,

i am writing a program which involves taking the date of birth of users..so what i have done is :

1> first ask the user to input year(check if it is between 1900 and 3000)
2>then ask the user to input month(check if it is between 1 to 12).
3>finally ask him to enter the date(check if in the range between 1 - 30 or 1-31(based on the month));
4> then concatenate in dd/mm/yyyy format( using "+" operator for string concatenation) and store into the String variable dob

i was wondering if there was any better way to do it..like if a user inputs some char when scanner.nextInt() is waiting to read an integer, an exception is thrown..is there something similar where the user can be asked to input the date in some format like dd/mm/yyyy and if the input format is incorrect, throw an exception like inputMismatchException... please help.


on job hunt
Wesleigh Pieters
Ranch Hand

Joined: Sep 04, 2012
Posts: 81
couldn't you ask them to input a date in the format dd/mm/yyyy, then either use a split or else substring etc and valid and it can print out where they were out of range and if so make them re-enter.
thejwal pavithran
Ranch Hand

Joined: Feb 11, 2012
Posts: 118
Wesleigh Pieters wrote:couldn't you ask them to input a date in the format dd/mm/yyyy, then either use a split or else substring etc and valid and it can print out where they were out of range and if so make them re-enter.


hey thank you for the idea.... ill make use of it..but i was wondering if i could make the compiler check if the input is in a valid date format or not..like ensuring somebody doesn't type their dob as 31 february..or something like that..of course coding can be used for such validation..is there any existing class i can make use of?
Wesleigh Pieters
Ranch Hand

Joined: Sep 04, 2012
Posts: 81
I just did some googling, If you get an instance of Calender, set its leniency to false then when you retrieve it it will throw an exception if it is invalid. I havent tried it myself though.

http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html - API

after some more searching I found this and it works well:



you could always modify it to return a boolean and put it inside a method etc

here is the source of it with comments etc, I just re-entered it to test it out.

http://www.dreamincode.net/forums/topic/14886-date-validation-using-simpledateformat/
thejwal pavithran
Ranch Hand

Joined: Feb 11, 2012
Posts: 118
very helpful
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: taking date input from user in java