Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need some help rearding date validation

Status
Not open for further replies.

frag

Programmer
Dec 7, 2000
321
GB
Hi all,

I looking for a way to validate a date which has to have the format 'yyyymmdd'. I found something in the internet:

Code:
import java.util.*;
import java.text.*;

 public class jtest {
  public static void main(String args[]) {
   String dt = "991223";
   String fdt = "yyMMdd";
   try {
     SimpleDateFormat sdf = new SimpleDateFormat(fdt);
     sdf.setLenient(false);
     Date dt2 = sdf.parse(dt);
     System.out.println("Date is ok = " + dt2 + "(" + dt + ")");
     }
   catch (ParseException e) {
     System.out.println(e.getMessage());
     }
   catch (IllegalArgumentException e) {
     System.out.println("Invalid date");
     }
   }
 }

But this is not exactly what I am looking for:
1. the IllegalArgumentException is never thrown (at least I have not found the 'proper' date format for getting one)
2. the example will take '9' as a valid date because it takes the 9 as: Fri Jan 09 00:00:00 CET 1970

Why do I have a SimpleDateFormat with 'yyMMdd' if the example doesn't care about the presence of all elements of 'yyMMdd' ??

Can anyone help me please?

Cheers
frag

patrick.metz@epost.de
 
frag,

When i run your posted code setting the input date string to "9" it throws a parse exception.

-pete
 
Here is what I would do: (I think the problem in work code what that you set
Code:
sdf.setLenient(false);
to false, it should be true...This will make sure that the date is correct)

Code:
import java.text.SimpleDateFormat;
import java.util.Date;
import java.text.ParseException;
import java.text.DateFormat;

class DateParser
{
	public static void main(String[] args) throws Exception
	{
		String someStringDate = "991223";

    	DateFormat formater = new SimpleDateFormat("yyyymmdd");
    	//only accept exact values
    	formater.setLenient(true);


    	try
    	{
			Date date = formater.parse(someStringDate);

			System.out.println("The date entered is correct: " + formater.format(date));
		}
		catch(ParseException p)
		{
			System.out.println("This is not a valid date. The Date must be in the following format: two digit for the" +
			 "month, two digit for the year and four digit for the year. Please correct and resubmit.");
		}
	}
}

The output is:
This is not a valid date. The Date must be in the following format: two digit for themonth, two digi
t for the year and four digit for the year. Please correct and resubmit.
 
@palbano: that's strange. i really get a correct date... which os are you using? mine is solaris...

@JVZ: even more strange... your code seams to work fine. if i change the false to true in my version from above the "9" is still valid...

patrick.metz@epost.de
 
@JVZ: hm... your code does take the 9 as date as well... I guess i will have to check the length of the date manually and if that is ok i will put it in the parser...

patrick.metz@epost.de
 
Win2k JVM 1.4
"Write once run anywhere" [hammer]

Gotta like Java... but what a crock that is eh?

-pete
 
humm....that is funny...well I'll run my code on the Unix box (HP-11i) and see what happens.
 
Do you know what I hate in all programming languages I have seen? Yes, exactly, I hate date and time processing!! :)

Cheers

frag

P.s.: to make my infos complete: Solaris JVM 1.4

patrick.metz@epost.de
 
ok...I drop my code on the test HP-UNIX box (running JVM 1.4.x), and with that date = 991223, I got an Exception...Like I should...

 
You might look into using Regular Expressions to validate your Date strings.

>> I looking for a way to validate a date which has to
>> have the format 'yyyymmdd'

Why are you having to validate that string format? Modern application user interfaces have the ability to control the user input of dates rather than having them type in a string. If your receiving the data that was previously processed by some system it should already be valid before you receive it.

-pete
 
I need this because the user has to enter the date into a JTextField.

The only other way I know how to handle the input of a date is the usage of combo-boxes. I did not want to them because that would mean that I have to do even more checking (like "30 or 31 days in a month" and "leapyear").
... hm ... guess I am just to lazy *g*.
Perhapse I will give combo boxes a try... this will fanally inflate my definitions for the variables to a totally confusing mess. ;-)

@JVZ: you should have used 9 not instead of the 991223 to test the code ;-)

patrick.metz@epost.de
 
Frag,

There is a freeware "JCalendarCombo" at : (and at other places)
When I run your original posted code on jdk1.3.1, I get : "Date is ok = Fri Jan 09 00:00:00 PST 1970(9)
"
On jdk1.4.2, I get : Unparseable date: "9"
(On Windows2000)
PS : leave setLenient(false) !
 
@hologram:
So it seams like they changed the method :-(
Thank you for the link. I am considering to use it. ;-)

patrick.metz@epost.de
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top