I am new to webMethods and I have a issue regarding the date format
I want to know whether there is any java code or a service which can validate the date…I mean if I am passing a date in yyyyMMdd format it should check the value(valid dateformat) and return a dateStatus(InvalidDate if date foramt is wrong or Ok if it is correct). Also it should check for some junk data…I mean if there is a value like 00000000 or 11111111…(exactly as yyyyMMdd) it should return the dateStatus as invalidDate.
If anybody has any info on this please let me know…
Thanks in advance…
You will need to write a custom piece of java code to do your thing. Take the pattern and the dateString as an input into a java service and based on the pattern, substring the dateString into month (MM), date (dd) and year (yyyy or yy). Once you have these three different variables, you will have to build a logic to do a check if the date is valid for the given month and so on.
Do a search on google for this java code and I am sure you will find a bunch of results with exactly what you are looking for… All you will need to do is to convert the code to webM java service. Let me know if you can’t find the java code on google and may be I can help you a bit with that.
I tried with the java code given by Roger but I could not resolve the issue. I am actually getting zeros for the date field(00000000). I tried to find some java codes but failed to find a proper code.If you have any code on this issue please let me know.Thank you for the reply
Syam (& Roger?),
The code that Roger has given will work… all you need to do is add one line - after the sdf is instantiated, add:
By default the instances of Calendar, DataFormat etc. created are set to Lenient, ie, the date rolls up to the next valid date. Thus, if you try to parse 02/29/2005 using the pattern MM/dd/YYYY, and without setting Lenient to false, the date will roll up to the next day and you will get 03/01/2005 as the result.
In your case, I believe it would be better to do a null check on dateOut and then put a String variable isValidDate in the out pipeline - set the value of isValidDate to true if the dateOut is not null and vise-versa.
Hope this helps,
I missed out a very important point and realized it when I read your original post once again. If you are looking for treating dates before a certain period (that you call junk data) as invalid dates, then you are gonna have to write a parsing service as I mentioned in my first reply. Even though for your business requirement a date 11/11/1111 may be an invalid date, however, for Java, this is a perfectly valid date. So the code above may not be able to take care of all your business requirements.
Let’s say your requirement says that any date before 01/01/1800 is invalid, Id recommend that you use the pattern to parse the dateString to get MM, dd & yyyy (or yy), and then perform individual checks on each of the fields (ie MM, dd, yyyy), to filter out the unwanted dates. Makes sense?
I don’t see you doing anything different in the code you pasted. The steps for validating the input (checking for null and 0 length strings) have nothing to do with the functionality that Roger and I were trying to help you with. The input validation should be a standard and an integral part of any service that you write. In fact you can perform the input validation (for null and blank) even before the code execution begins by setting the Required and Allow Null properties of the input variables.
Any way, I am glad that you were able to resolve the problem
I am sorry for not trying to understand Roger’s and your code. I atually tested with different values with the Rogers code but I was getting the same problem. I could even spend somemore time in researching but I had to resolve this issue at any cost to load the data into the tables. So I tried for some more resources. I am thankful to Roger and you for giving me valuble suggestions and hope you continue to give the same support.
No probs… you did what was necessary to get the problem resolved n Im glad you made it work.
I’m of the school that preaches “Avoid Java codes whenever possible”. That is, I think they’re too often used when people are too lazy to find built-in services that can accomplish the same thing (only much easier). Of course, some Java codes are unavoidable, especially relating to integrating backend systems, but in general, we really shouldn’t overuse them.
I think Java codes are harder to maintain (think deployment!), and require a different skill set (Java programming skils).
Okay, so with all that rumbling, how would one accomplish this with services?
- How do we check if date is after a certain date? (or if it’s within a range).
- Easy enough. Just compare with a branch (something like: “%inputDate% > ‘20041231’”).
- How to check it’s a valid date? Okay, pub.date:dateTimeFormat has a unique property of “rolling-over” dates. That is, it’ll accept 20051232 as a valid substitution for 20060101. But we’ll use this property to our advantage when checking for a valid date:
- pub.date:dateTimeFormat. currentPattern = “yyyyMMdd”, newPattern = “yyyyMMdd”.
Huh? Did I type that right?
Well, actually, yes. You just compared input and output values. If the date did roll-over, then they aren’t equal. That didn’t take much effort, did it? 8)