How to validate MM/DD/YYYY date

G

GIMME

Yes, I've seen the doc on SimpleDateFormatter but it would help
if some kind sole out there would write out the lines which validate
that a string is a valid MM/DD/YYYY date.

Thanks.
 
N

nos

GIMME said:
Yes, I've seen the doc on SimpleDateFormatter but it would help
if some kind sole out there would write out the lines which validate
that a string is a valid MM/DD/YYYY date.

Thanks.

Are you talking about syntax only? Or
do you want to check the data represents an actual
date on some calendar (there have been many since year 0)
 
A

Andrew Thompson

"GIMME"
| Yes, I've seen the doc on SimpleDateFormatter but it would help
| if some kind sole out there would write out the lines which
validate
| that a string is a valid MM/DD/YYYY date.

And it might help you learn if you posted
how you _thought_ it might be done and
asked for comments and corrections too...

You young folks.
All gimme, gimme, gimme..
 
M

Michael Dunn

: Yes, I've seen the doc on SimpleDateFormatter but it would help
: if some kind sole out there would write out the lines which validate
: that a string is a valid MM/DD/YYYY date.
:
: Thanks


Something to play around with


import java.util.Calendar;
import java.text.SimpleDateFormat;

class DateValidator
{
public DateValidator(String dateString)
{
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Calendar cal = Calendar.getInstance();
String message = "";
try
{
cal.setTime(sdf.parse(dateString));
if(dateString.equals(sdf.format(cal.getTime()))) message = "valid date";
else message = "invalid date, or format";
}
catch(Exception e) {message = "Invalid format, or characters";}
System.out.println(message);
System.exit(0);
}
public static void main(String[] args){new DateValidator(args[0]);}
}.
 
T

Tony Morris

Well, you "oldies" DO have a spoon don't you ?

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
 
J

JavaJunkie

/*
* DateValidation.java
*
* Created on January 19, 2004, 11:35 PM
*/

/**
*
* @author Owner
*/

import java.text.*;

public class DateValidation {

/** Creates a new instance of DateValidation */
public DateValidation() {
}

public boolean validateDate(String theDate) {
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
java.util.Date d;

try {
df.setLenient(false);
d = df.parse(theDate);
} catch (ParseException e) {
return false;
}

return true;
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

DateValidation dateValidation = new DateValidation();
String dateMMDDYYYY = "";

//test for valid date
dateMMDDYYYY = "12/25/2003";
if (dateValidation.validateDate(dateMMDDYYYY)) {
System.out.println("Date " + dateMMDDYYYY + " is valid");
} else {
System.out.println("Date " + dateMMDDYYYY + " is invalid");
}

//test for invalid date
dateMMDDYYYY = "25/12/2003";
if (dateValidation.validateDate(dateMMDDYYYY)) {
System.out.println("Date " + dateMMDDYYYY + " is valid");
} else {
System.out.println("Date " + dateMMDDYYYY + " is invalid");
}
}

}
 
A

Andrew Thompson

| Well, you "oldies" DO have a spoon don't you ?

I have several, I also have some knives,
forks, plates, cups etc. ..but that is getting
off topic..
 
T

Tony Morris

I was implying that it is intended that you "spoon-feed" those who request
it.

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
 
B

Brandon Blackmoor

GIMME said:
if some kind sole out there would write out the lines
which validate that a string is a valid MM/DD/YYYY date.

You'll make your life easier and your data more meaningful if you use
ISO 8601 international standard date format: YYYY-MM-DD.

(e-mail address removed)
2004-01-21
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top