KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dinamica > ValidatorUtil


1 package dinamica;
2
3 import java.util.Date JavaDoc;
4
5 /**
6  * Utility class to validate String values against data types
7  * in order to test if the value represents de data type (dates, numbers, etc.)
8  * <br>
9  * Creation date: 25/10/2003<br>
10  * Last Update: 25/10/2003<br>
11  * (c) 2003 Martin Cordova<br>
12  * This code is released under the LGPL license<br>
13  * @author Martin Cordova
14  */

15 public class ValidatorUtil
16 {
17
18     /**
19      * Test a String value to check if it does represent
20      * a valid date according to a specific format
21      * @param dateValue Value to test
22      * @param dateFormat Date format used to represent a date in value
23      * @return NULL if value can't be converted
24      */

25     public static Date JavaDoc testDate(String JavaDoc dateValue, String JavaDoc dateFormat)
26     {
27         
28         try
29         {
30             Date JavaDoc d = StringUtil.getDateObject(dateValue, dateFormat);
31             return d;
32         }
33         catch (Throwable JavaDoc e)
34         {
35             return null;
36         }
37
38     }
39
40     /**
41      * Test a String value to check if it does represent
42      * a valid double number
43      * @param doubleValue Value to test
44      * @return NULL if value can't be converted
45      */

46     public static Double JavaDoc testDouble(String JavaDoc doubleValue)
47     {
48
49         try
50         {
51             double d = Double.parseDouble(doubleValue);
52             return new Double JavaDoc(d);
53         }
54         catch (Throwable JavaDoc e)
55         {
56             return null;
57         }
58
59     }
60
61     /**
62      * Test a String value to check if it does represent
63      * a valid integer number
64      * @param integerValue Value to test
65      * @return NULL if value can't be converted
66      */

67     public static Integer JavaDoc testInteger(String JavaDoc integerValue)
68     {
69
70         try
71         {
72             int i = Integer.parseInt(integerValue);
73             return new Integer JavaDoc(i);
74         }
75         catch (Throwable JavaDoc e)
76         {
77             return null;
78         }
79
80     }
81
82 }
83
Popular Tags