常用的类型转换

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;


public class TypeHelper {


/**

* 功能:检查字符串是否为数字

* @param 需要检查的字符串

* @return boolean

*/

public static boolean isNumeric(String str) {

if (str == null || str == "")

return false;

for (int i = str.length(); --i >= 0;) {

if (!Character.isDigit(str.charAt(i))) {

return false;

}

}

return true;

}


/**

* 功能:检查字符是否为空

* @param 需要检查的字符串

* @return 非空返回本身 空则返回 ""

*/

public static String ReturnNull(String str) {

if (str == null) {

return "";

} else {

return str;

}

}


/**

* 功能:转化字符串为数字

* @param 需要转的字符串

* @return 返回字符串的数值,如果非数字格式则返回 0

*/

public static int getNumeric(String str) {

try {

return Integer.parseInt(str);

} catch (NumberFormatException e) {

return 0;

}

}


/**

* 功能:检查字符串是否为YYYY—MM-DD 格式

* @return boolean

*/

public static boolean isDateString(String s) {

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

if (null == s)

return false;

else {

try {

return s.equals(sdf.format(sdf.parse(s)));

} catch (ParseException e) {

return false;

}

}

}


/**

* 功能:过滤非法参数参数

* @return 新字符串

*/

public static String checkInj(String str) {

if (str == null)

return "";

String inj_str = "'|and|exec|insert|select|delete|update|count|*|%|chr|mid|master|truncate|char|declare|;|-|+|,";// 这里的东西还可以自己添加

String[] inj_stra = inj_str.split("\\|");

for (int i = 0; i < inj_stra.length; i++) {

if (str.indexOf(inj_stra[i]) >= 0) {

str = str.replace(inj_stra[i], "");

}

}

return str;

}


/**

* 功能:取当前日期字符串

* @param 需要检查的字符串

* @return 格式 YYYY-MM-dd

*/

public static String getTodayDate() {

return new java.text.SimpleDateFormat("yyyy-MM-dd").format(new Date());

}

}

//******************************************************************************************************************


public class TypeChange {



//change the string type to the int type 

public static int stringToInt(String intstr) 

Integer integer; 

integer = Integer.valueOf(intstr); 

return integer.intValue(); 

//change int type to the string type 

public static String intToString(int value) 

Integer integer = new Integer(value); 

return integer.toString(); 

//change the string type to the float type 

public static float stringToFloat(String floatstr) 

Float floatee; 

floatee = Float.valueOf(floatstr); 

return floatee.floatValue(); 

//change the float type to the string type 

public static String floatToString(float value) 

Float floatee = new Float(value); 

return floatee.toString(); 

//change the string type to the sqlDate type 

public static java.sql.Date stringToDate(String dateStr) 

return java.sql.Date.valueOf(dateStr); 

//change the sqlDate type to the string type 

public static String dateToString(java.sql.Date datee) 

return datee.toString(); 


public static void main(String[] args) 

java.sql.Date day ; 

day = TypeChange.stringToDate("2003-11-3"); 

String strday = TypeChange.dateToString(day); 

System.out.println(strday); 



}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值