File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

taking date input from user in java

 
thejwal pavithran
Ranch Hand
Posts: 119
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Wesleigh Pieters
Ranch Hand
Posts: 81
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 119
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 119
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
very helpful
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic