1 23 24 package org.infoglue.cms.applications.common; 25 26 import java.text.SimpleDateFormat ; 27 import java.util.Date ; 28 29 import org.infoglue.cms.exception.ConstraintException; 30 31 35 public class ValueConverter 36 { 37 42 45 private ValueConverter() {} 46 47 48 49 51 54 public static final boolean isDate(String value) { 55 return toDate(value) != null; 56 } 57 58 61 public static final Date toDate(String value) 62 { 63 return new Date (); 64 } 65 66 67 70 public static final Date getDate(String dateString, String fieldName) throws ConstraintException 71 { 72 Date publishDate = null; 73 try 74 { 75 publishDate = toDate(dateString, "yyyy-MM-dd"); 76 } 77 catch(Exception e) 78 { 79 throw new ConstraintException(fieldName, "305"); 80 } 81 return publishDate; 82 } 83 84 87 88 public static final Date toDate(String dateString, String pattern) throws Exception 89 { 90 if(dateString == null || dateString.length() == 0) 91 return null; 92 93 SimpleDateFormat formatter = new SimpleDateFormat (pattern); 94 return formatter.parse(dateString); 95 } 96 97 100 public static final boolean isBoolean(String value) { 101 return toBoolean(value) != null; 102 } 103 104 107 public static final Boolean toBoolean(String value) { 108 return Boolean.valueOf(value); 109 } 110 111 114 public static final boolean isNonNegativeInteger(String value) { 115 return toNonNegativeInteger(value) != null; 116 } 117 118 121 public static final Integer toNonNegativeInteger(String value) { 122 try { 123 final int intValue = Integer.parseInt(value); 124 return (intValue < 0) ? null : new Integer (intValue); 125 } catch(NumberFormatException e) { 126 return null; 127 } 128 } 129 130 133 public static final boolean isNonNegativeFloat(String value) { 134 return toNonNegativeFloat(value) != null; 135 } 136 137 140 public static final Float toNonNegativeFloat(String value) { 141 try { 142 final float floatValue = Float.parseFloat(value); 143 return (floatValue < 0.0) ? null : new Float (floatValue); 144 } catch(NumberFormatException e) { 145 return null; 146 } 147 } 148 149 150 151 } | Popular Tags |