aspose file tools*
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
Author

Checking Date

red red
Greenhorn

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

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
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

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:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14352
    
  22

(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.


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:

setLenient(false);

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 http://mattkruse.com/javascript/calendarpopup/

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

Joined: Oct 27, 2005
Posts: 19762
    
  20

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: http://aspose.com/file-tools
 
subject: Checking Date