File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes how to validate a string like Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to validate a string like "20060503" is DATE" Watch "how to validate a string like "20060503" is DATE" New topic
Author

how to validate a string like "20060503" is DATE

lijun wang
Greenhorn

Joined: Jan 20, 2006
Posts: 28
how to validate a string like "20060503" is DATE?how to validate this string are all number, not charactor?
Thanks.
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Regular Expressions Tutorial


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Definetly regular expressions is the way to go. Nevertheless, another options could be:

1. Actually parsing the String



2. You could use Character.isDigit() to test every character in the String.

3. If you are using Jakarta Commons Lang Package you can use NumberUtils class.

Srinivas Kalvala
Ranch Hand

Joined: Oct 20, 2005
Posts: 257

Originally posted by lijun wang:
how to validate a string like "20060503" is DATE?how to validate this string are all number, not charactor?
Thanks.


Hi,

The regular expression is the way to solve that one.

Or

you can write your own syntax checker thats all.

The Regular Expression to check your case is,

For example assume you need to check year ragne from 1900 to 2999 then

[[1][9][0-9[0-9]|[2][0-9][0-9][0-9]][[0][1-9]|[1][0|1|2|]]

This is only for YYYYMM Now you can try for DD ok.


Thank you.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
I hope you realize that asking if the string is a valid date is very different from asking whether it consists of only numbers!!!

For the date question, definitely use a SimpleDateFormat -- rolling your own would be silly. (Leap years, the start of the Gregorian calendar...)


There is no emoticon for what I am feeling!
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by Jeff Albertson:
I hope you realize that asking if the string is a valid date is very different from asking whether it consists of only numbers!!!

For the date question, definitely use a SimpleDateFormat -- rolling your own would be silly. (Leap years, the start of the Gregorian calendar...)


Out of curiousity, how hard is it to parse all of these date formats using SimpleDateFormat?

MMDDYYYY
MM.DD.YYYY
M.DD.YYYY
MM.D.YYYY

MMDDYY
MM.DD.YY
M.DD.YY
MM.D.YY

YYYYMMDD
YYYY.MM.DD
YYYY.M.DD
YYYY.MM.D

YYMMDD
YY.MM.DD
YY.M.DD
YY.MM.D

Where . is any separator \/.-? I ask because we do it in about 100 lines, but it seems overly complex. I just don't see any easy way to accomplish that without a horribly absurd number of SimpleDateFormats.
lijun wang
Greenhorn

Joined: Jan 20, 2006
Posts: 28
DateFormat formatter = new SimpleDateFormat("yyyyMMdd");
Date aTrueDate = null;
try {
aTrueDate = formatter.parse("2006050f");
} catch (ParseException e) {
e.printStackTrace();
}

I tried this code, but it didn't throw a ParseException, it display 2006/05/01, and formatter.parse("200605ff") throw a ParseException.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Ken, are you trying to parse them all simultaneously? Accept one string as input and decide which of the available formats it fits? What about something like "060708"? Is that MMDDYY or YYMMDD? Or am I misunderstanding your goal here?


"I'm not back." - Bill Harding, Twister
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
If it can be either it would default to MDY. Of course 990505 would fail an MDY parse because 99 is not a valid month and then get parsed as YMD.
[ May 19, 2006: Message edited by: Ken Blair ]
lijun wang
Greenhorn

Joined: Jan 20, 2006
Posts: 28
can anybody explain why this code print "Wed Mar 01 00:00:00 MST 2006" instead of throws a ParseException?

-----------------------------------------------
public class test {
private void testDate(){
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyyMMdd");
try {
Date invoiceDate = dateFormatter.parse("2006031f");
System.out.println(invoiceDate);
} catch (ParseException pe) {
pe.printStackTrace();
}
}
public static void main(String[] args){
test test1 = new test();
test1.testDate();
}
}
--------------------------------------------------
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Parses text from the beginning of the given string to produce a date. The method may not use the entire text of the given string.

...more information in parse(String, ParsePosition)

By default, parsing is lenient: If the input is not in the form used by this object's format method but can still be parsed as a date, then the parse succeeds. Clients may insist on strict adherence to the format by calling setLenient(false).


It's not parsing the f, it's looking at it as if it were 2006031 and assuming it's in YYYYMMD format. If you want to force it to look at the entire thing then do setLenient(false) prior to formatting it.
[ May 19, 2006: Message edited by: Ken Blair ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Surprisingly, setLenient(false) isn't good enough here. This API was written long ago by somone who preferred to hide errors rather than make them obvious; I have little patience for such code. However, this works:
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Originally posted by Ken Blair:
If it can be either it would default to MDY. Of course 990505 would fail an MDY parse because 99 is not a valid month and then get parsed as YMD.

Oh, oh, oh, oh!
That reminds me of some code which (seemed to) work(ed) well on 0712 but failed on 0713.


http://home.arcor.de/hirnstrom/bewerbung
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
You're right. I misunderstood that part of the documentation, it only throws an exception if it errors out on the very first character. That seems horribly illogical. Why would I want an exception when it fails on the first character but not if it fails on any other?
Adam Richards
Ranch Hand

Joined: Nov 03, 2005
Posts: 135
You can also try http://jakarta.apache.org/commons/lang/api-release/org/apache/commons/lang/time/DateUtils.html. Their parseDate() method is said to parse "a string representing a date by trying a variety of different parsers."
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to validate a string like "20060503" is DATE