• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

how to validate a string like "20060503" is DATE

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how to validate a string like "20060503" is DATE?how to validate this string are all number, not charactor?
Thanks.
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regular Expressions Tutorial
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 257
Hibernate Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...)
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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."
 
Oh. Hi guys! Look at this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic