Sindhu Kodoor wrote:Thanks for the reply, but I have already tried using the SimpleDateFormat, but my problem is more complex. It should get the input string date, and check its format and as I said before it should allow only the formats yyyy-MM-dd HH:mm:ss:S , YYYY-MM-DD, YYYY-MM-DDThh:mm:ssTZD, YYYY-MM-DDThh:mm:ss.sTZD, YYYY-MM-DDThh:mm:ss.ssTZD, DD Month YYYY any other formats for example even if it is of the format DD-MM-YYYY it should output an empty string. I tried using the regex but I have another requirement to be checked , that is it should be valid date (DD) should be within 1 and 31 , Month ( MM ) should be within 1-12 and YYYY should not start with a 0. Can you please help me out?
OCPJP 6
Plus there's an Apache Commons class/method that'll take a number of date formats and do all this checking
Sindhu Kodoor wrote:
Thanks for the reply, but I have already tried using the SimpleDateFormat, but my problem is more complex. It should get the input string date, and check its format and as I said before it should allow only the formats yyyy-MM-dd HH:mm:ss:S , YYYY-MM-DD, YYYY-MM-DDThh:mm:ssTZD, YYYY-MM-DDThh:mm:ss.sTZD, YYYY-MM-DDThh:mm:ss.ssTZD, DD Month YYYY any other formats for example even if it is of the format DD-MM-YYYY it should output an empty string. I tried using the regex but I have another requirement to be checked , that is it should be valid date (DD) should be within 1 and 31 , Month ( MM ) should be within 1-12 and YYYY should not start with a 0. Can you please help me out?
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Matthew Brown wrote:Don't bother with the regular expression - you're just repeating the work that SimpleDateFormat is going to do anyway. Just try to parse with the SimpleDateFormat object. Catch the ParseException that's thrown, and move onto the next format. If a ParseException isn't thrown, then the date was in the right format.
OCPJP 6
Matthew Brown wrote:I'm not so worried about the performance - more about the maintenance. Why write a bunch of complicated regular expressions when there's no need for them? So "easier" - no.
I'm sure any (vaguely sensible) approach will be fast enough unless the method is really performance critical.
Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Raymond Tong wrote:Remember, Java is case sensitive.
Remember, Java is case sensitive.
- Marimuthu Madasamy
What does a metric clock look like? I bet it is nothing like this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|