Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Checking Date

 
red red
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
i want to check the date, f.e. 1979.12.12 which is written from a user in to a grafical window.
It must be in this order yyyy.mm.dd because it's met to be written in a jdbc (mysql) database.
How can i check that the date is written right (numbers, not letters and in right oder)?
 
Sri Ram
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is ur name Really RED RED
Anyway provide a calendar. U dont have to worry about the way a user enters.
 
Rob Spoor
Sheriff
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another possibility is using a Format (SimpleDateFormat should work for you) and create a JFormattedTextField with it.

You can also use the Format itself to check your date:
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(Let's help the moderators a bit...)

Hi,

Welcome to JavaRanch!

First, a bit of business: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. Joke names and "handles" aren't acceptable. You'll find that many Ranchers won't be interested in helping you until you follow this rule.

You can change your display name
here. Thanks for your cooperation.
 
Jaime M. Tovar
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I recomend to use:

setLenient(false);

just after creating the simpledateformat. This option checks that the string precisely match this object's format
 
praveen Shangunathan
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you choose to provide a calendar, you can find some here http://mattkruse.com/javascript/calendarpopup/

then you could make the date into any format you wish.
 
Rob Spoor
Sheriff
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Praveen, that site lists JavaScript calendars, not Java.


I've written a DateChooser class once, similar to javax.swing.ColorChooser (you need JGoodies Forms for the layout - feel free to edit if you don't want the library):
Alternatively, but this does not work with all formats (unfortunately yyyy.MM.dd is not supported), you can use a JSpinner for it:If you double click on the year, month or date part of this spinner you can use the spinner's buttons to change only that part.
With yyyy.MM.dd it can only change the year.

[ November 16, 2005: Message edited by: Rob Spoor ]
[ November 16, 2005: Message edited by: Rob Spoor ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic