File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Checking Date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Checking Date" Watch "Checking Date" New topic

Checking Date

red red

Joined: Nov 16, 2005
Posts: 1
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 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

Joined: Oct 03, 2005
Posts: 118
Is ur name Really RED RED
Anyway provide a calendar. U dont have to worry about the way a user enters.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20276

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:

How To Ask Questions How To Answer Questions
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15092

(Let's help the moderators a bit...)


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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Jaime M. Tovar
Ranch Hand

Joined: Mar 28, 2005
Posts: 133
I recomend to use:


just after creating the simpledateformat. This option checks that the string precisely match this object's format

She will remember your heart when men are fairy tales in books written by rabbits.<br /> As long as there is duct tape... there is also hope.
praveen Shangunathan
Ranch Hand

Joined: Sep 06, 2005
Posts: 65
if you choose to provide a calendar, you can find some here

then you could make the date into any format you wish.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20276

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 ]
I agree. Here's the link:
subject: Checking Date
It's not a secret anymore!