1 40 package com.mvnforum.admin.importexport; 41 42 import java.sql.Timestamp ; 43 import java.text.*; 44 import java.util.Locale ; 45 46 import net.myvietnam.mvncore.util.DateUtil; 47 48 55 public class XMLUtil { 56 57 private XMLUtil() { 58 } 59 60 72 public static int stringToIntDef(String value, int defaultValue) 73 throws NumberFormatException { 74 if (value==null) return defaultValue; 75 else return Integer.parseInt(value); 76 } 77 78 public static int stringToGender(String s) { 80 if (s.equalsIgnoreCase("male") || s.equals("1")) { 81 return 1; 82 } else if (s.equalsIgnoreCase("female") || s.equals("0")) { 83 return 0; 84 } else throw new IllegalArgumentException ("Illegal gender string format."); 85 } 86 87 public static int stringToGenderDef(String value, int defaultValue) { 88 if (value==null) return defaultValue; 89 else return stringToGender(value); 90 } 91 92 public static String genderToString(int gender) { 93 if (gender==0) return "0"; else if (gender==1) return "1"; else throw new IllegalArgumentException ("Illegal gender value."); 96 } 97 98 100 public static boolean stringToBoolean(String s) { 101 if (s.equalsIgnoreCase("true") || s.equalsIgnoreCase("yes") || s.equals("1")) { 102 return true; 103 } else if (s.equalsIgnoreCase("false") || s.equalsIgnoreCase("no") || s.equals("0")) { 104 return false; 105 } else throw new IllegalArgumentException ("Illegal boolean format."); 106 } 107 108 public static boolean stringToBooleanDef(String value, boolean defaultValue) { 109 if (value==null) return defaultValue; 110 else return stringToBoolean(value); 111 } 112 113 public static String booleanToString(boolean value) { 114 if (value) return "true"; 115 else return "false"; 116 } 117 118 public static java.sql.Date stringToSqlDate(String s) { 119 125 if (s==null) throw new java.lang.IllegalArgumentException ("null string"); 126 s=s.trim(); 127 128 try { 133 SimpleDateFormat f=new SimpleDateFormat("yyyyMMdd", Locale.US); 135 if (s.indexOf('/')>0) f=new SimpleDateFormat("yyyy/MM/dd", Locale.US); 136 else if (s.indexOf('-')>0) f=new SimpleDateFormat("yyyy-MM-dd", Locale.US); 137 else if (s.indexOf(' ')>0) f=new SimpleDateFormat("EEE MMM dd yyyy", Locale.US); 138 java.util.Date d=f.parse(s); 139 return new java.sql.Date (d.getTime()); 140 } catch (ParseException e) { 141 throw new java.lang.IllegalArgumentException ("Invalid date format: \""+s+"\""); 142 } 143 } 144 145 public static java.sql.Date stringToSqlDateDef(String value, java.sql.Date defaultValue) { 146 if (value==null) return defaultValue; 147 else return stringToSqlDate(value); 148 } 149 150 public static java.sql.Date stringToSqlDateDefNow(String value) { 151 Timestamp now = DateUtil.getCurrentGMTTimestamp(); 152 return stringToSqlDateDef(value, new java.sql.Date (now.getTime())); 153 } 154 155 public static java.sql.Date stringToSqlDateDefNull(String value) { 156 165 return stringToSqlDateDefNow(value); 166 } 167 168 public static String sqlDateToString(java.sql.Date value) { 169 DateFormat frm = new SimpleDateFormat("yyyy/MM/dd", Locale.US); 170 return frm.format(value); 171 } 172 173 public static String sqlDateToStringDefNow(java.sql.Date value) { 174 Timestamp now = DateUtil.getCurrentGMTTimestamp(); 175 if (value==null) return sqlDateToString(new java.sql.Date (now.getTime())); 176 else return sqlDateToString(value); 177 } 178 179 public static String sqlDateToStringDefEmpty(java.sql.Date value) { 180 if (value==null) return ""; 182 else return sqlDateToString(value); 183 } 184 185 public static java.sql.Timestamp stringToSqlTimestamp(String s) { 186 193 if (s==null) throw new java.lang.IllegalArgumentException ("null string"); 194 s=s.trim(); 195 196 try { 201 SimpleDateFormat f=new SimpleDateFormat("yyyyMMddHHmmss", Locale.US); 203 if (s.indexOf('/')>0) { 204 s=s.substring(0, 19); f=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.US); 206 } else if (s.indexOf('-')>0) { 207 s=s.substring(0, 19); f=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US); 209 } else if (s.indexOf(' ')>0) { 210 f=new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US); 211 } else { 212 s=s.substring(0, 14); f=new SimpleDateFormat("yyyyMMddHHmmss", Locale.US); 214 } 215 java.util.Date d=f.parse(s); 216 return new Timestamp (d.getTime()); 217 } catch (StringIndexOutOfBoundsException e) { 218 throw new java.lang.IllegalArgumentException ("Invalid timestamp format: \""+s+"\""); 219 } catch (ParseException e) { 220 throw new java.lang.IllegalArgumentException ("Invalid timestamp format: \""+s+"\""); 221 } 222 } 223 224 public static java.sql.Timestamp stringToSqlTimestampDef(String value, java.sql.Timestamp defaultValue) { 225 if (value==null) return defaultValue; 226 else return stringToSqlTimestamp(value); 227 } 228 229 public static java.sql.Timestamp stringToSqlTimestampDefNow(String value) { 230 Timestamp now = DateUtil.getCurrentGMTTimestamp(); 231 return stringToSqlTimestampDef(value, now); 232 } 233 234 public static java.sql.Timestamp stringToSqlTimestampDefNull(String value) { 235 244 return stringToSqlTimestampDefNow(value); 245 } 246 247 public static String sqlTimestampToString(java.sql.Timestamp value) { 248 DateFormat frm = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.US); 249 return frm.format(value); 250 } 251 252 public static String sqlTimestampToStringDefNow(java.sql.Timestamp value) { 253 Timestamp now = DateUtil.getCurrentGMTTimestamp(); 254 if (value==null) return sqlTimestampToString(now); 255 else return sqlTimestampToString(value); 256 } 257 258 public static String sqlTimestampToStringDefEmpty(java.sql.Timestamp value) { 259 if (value==null) return ""; 260 else return sqlTimestampToString(value); 261 } 262 263 264 } 265
| Popular Tags
|