package sc.util;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;
/**
* 类型转换辅助工具
* @version:1.0
*/
public class TypeCaseHelper {
/**
* 转换核心实现方法
*
* @param obj
* @param type
* @param format
* @return Object
* @throws TypeCastException
*/
public static Object convert(Object obj, String type, String format) throws TypeCastException {
Locale locale = new Locale("zh", "CN", "");
if (obj == null)
return null;
if (obj.getClass().getName().equals(type))
return obj;
if ("Object".equals(type) || "java.lang.Object".equals(type))
return obj;
String fromType = null;
if (obj instanceof String) {
fromType = "String";
String str = (String) obj;
if ("String".equals(type) || "java.lang.String".equals(type))
return obj;
if (str.length() == 0)
return null;
if ("Boolean".equals(type) || "java.lang.Boolean".equals(type)) {
Boolean value = null;
if (str.equalsIgnoreCase("TRUE"))
value = new Boolean(true);
else
value = new Boolean(false);
return value;
}
if ("Double".equals(type) || "java.lang.Double".equals(type))
try {
Number tempNum = getNf(locale).parse(str);
return new Double(tempNum.doubleValue());
} catch (ParseException e) {
throw new TypeCastException("Could not convert " + str + " to " + type + ": ", e);
}
if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type))
try {
BigDecimal retBig = new BigDecimal(str);
int iscale = str.indexOf(".");
int keylen = str.length();
if (iscale > -1) {
iscale = keylen - (iscale + 1);
return retBig.setScale(iscale, 5);
} else {
return retBig.setScale(0, 5);
}
} catch (Exception e) {
throw new TypeCastException("Could not convert " + str + " to " + type + ": ", e);
}
if ("Float".equals(type) || "java.lang.Float".equals(type))
try {
Number tempNum = getNf(locale).parse(str);
return new Float(tempNum.floatValue());
} catch (ParseException e) {
throw new TypeCastException("Could not convert " + str + " to " + type + ": ", e);
}
if ("Long".equals(type) || "java.lang.Long".equals(type))
try {
NumberFormat nf = getNf(locale);
nf.setMaximumFractionDigits(0);
Number tempNum = nf.parse(str);
return new Long(tempNum.longValue());
} catch (ParseException e) {
throw new TypeCastException("Could not convert " + str + " to " + type + ": ", e);
}
if ("Integer".equals(type) || "java.lang.Integer".equals(type))
try {
NumberFormat nf = getNf(locale);
nf.setMaximumFractionDigits(0);
Number tempNum = nf.parse(str);
return new Integer(tempNum.intValue());
} catch (ParseException e) {
throw new TypeCastException("Could not convert " + str + " to " + type + ": ", e);
}
if ("Date".equals(type) || "java.sql.Date".equals(type)) {
if (format == null || format.length() == 0)
try {
return Date.valueOf(str);
} catch (Exception e) {
try {
DateFormat df = null;
if (locale != null)
df = DateFormat.getDateInstance(3, locale);
else
df = DateFormat.getDateInstance(3);
java.util.Date fieldDate = df.parse(str);
return new Date(fieldDate.getTime());
} catch (ParseException e1) {
throw new TypeCastException("Could not convert " + str + " to " + type + ": ", e);
}
}
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
java.util.Date fieldDate = sdf.parse(str);
return new Date(fieldDate.getTime());
} catch (ParseException e) {
throw new TypeCastException("Could not convert " + str + " to " + type + ": ", e);
}
}
if ("Timestamp".equals(type) || "java.sql.Timestamp".equals(type)) {
if (str.length() == 10)
str = str + " 00:00:00";
if (format == null || format.length() == 0)
try {
return Timestamp.valueOf(str);
} catch (Exception e) {
try {
DateFormat df = null;
if (locale != null)
df = DateFormat.getDateTimeInstance(3, 3, locale);
else
df = DateFormat.getDateTimeInstance(3, 3);
java.util.Date fieldDate = df.parse(str);
return new Timestamp(fieldDate.getTime());
} catch (ParseException e1) {
throw new TypeCastException("Could not convert " + str + " to " + type + ": ", e);
}
}
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
java.util.Date fieldDate = sdf.parse(str);
return new Timestamp(fieldDate.getTime());
} catch (ParseException e) {
throw new TypeCastException("Could not convert " + str + " to " + type + ": ", e);
}
} else {
throw new TypeCastException("Conversion from " + fromType + " to " + type + " not currently supported");
}
}
if (obj instanceof BigDecimal) {
fromType = "BigDecimal";
BigDecimal bigD = (BigDecimal) obj;
if ("String".equals(type))
return getNf(locale).format(bigD.doubleValue());
if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type))
return obj;
if ("Double".equals(type))
return new Double(bigD.doubleValue());
if ("Float".equals(type))
return new Float(bigD.floatValue());
if ("Long".equals(type))
return new Long(Math.round(bigD.doubleValue()));
if ("Integer".equals(type))
return new Integer((int) Math.round(bigD.doubleValue()));
else
throw new TypeCastException("Conversion from " + fromType + " to " + type + " not currently supported");
}
if (obj instanceof Double) {
fromType = "Double";
Double dbl = (Double) obj;
if ("String".equals(type) || "java.lang.String".equals(type))
return getNf(locale).format(dbl.doubleValue());
if ("Double".equals(type) || "java.lang.Double".equals(type))
return obj;
if ("Float".equals(type) || "java.lang.Float".equals(type))
return new Float(dbl.floatValue());
if ("Long".equals(type) || "java.lang.Long".equals(type))
return new Long(Math.round(dbl.doubleValue()));
if ("Integer".equals(type) || "java.lang.Integer".equals(type))
return new Integer((int) Math.round(dbl.doubleValue()));
if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type))
return new BigDecimal(dbl.toString());
else
throw new TypeCastException("Conversion from " + fromType + " to " + type + " not currently supported");
}
if (obj instanceof Float) {
fromType = "Float";
Float flt = (Float) obj;
if ("String".equals(type))
return getNf(locale).format(flt.doubleValue());
if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type))
return new BigDecimal(flt.doubleValue());
if ("Double".equals(type))
return new Double(flt.doubleValue());
if ("Float".equals(type))
return obj;
if ("Long".equals(type))
return new Long(Math.round(flt.doubleValue()));
if ("Integer".equals(type))
return new Integer((int) Math.round(flt.doubleValue()));
else
throw new TypeCastException("Conversion from " + fromType + " to " + type + " not currently supported");
}
if (obj instanceof Long) {
fromType = "Long";
Long lng = (Long) obj;
if ("String".equals(type) || "java.lang.String".equals(type))
return getNf(locale).format(lng.longValue());
if ("Double".equals(type) || "java.lang.Double".equals(type))
return new Double(lng.doubleValue());
if ("Float".equals(type) || "java.lang.Float".equals(type))
return new Float(lng.floatValue());
if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type))
return new BigDecimal(lng.toString());
if ("Long".equals(type) || "java.lang.Long".equals(type))
return obj;
if ("Integer".equals(type) || "java.lang.Integer".equals(type))
return new Int
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
python爬虫项目实战源码 (1959个子文件)
TypeCaseHelper.class 10KB
TypeCaseHelper.class 10KB
TypeCaseHelper.class 10KB
TypeCaseHelper.class 10KB
TypeCaseHelper.class 10KB
TypeCaseHelper.class 10KB
MovieIndex.class 10KB
MovieIndex.class 10KB
MovieIndex.class 10KB
MovieIndex.class 10KB
AdminMovieController.class 7KB
AdminMovieController.class 7KB
IdCardUtil.class 7KB
IdCardUtil.class 7KB
IdCardUtil.class 7KB
IdCardUtil.class 7KB
IdCardUtil.class 7KB
IdCardUtil.class 7KB
AQYSpider.class 7KB
AQYSpider.class 7KB
TXSpider.class 7KB
TXSpider.class 7KB
AdminMovieController.class 6KB
AdminMovieController.class 6KB
AdminMovieController.class 6KB
MovieController.class 6KB
MovieController.class 6KB
MovieController.class 6KB
MovieController.class 6KB
ExportExcelUtil.class 4KB
ExportExcelUtil.class 4KB
ExportExcelUtil.class 4KB
ExportExcelUtil.class 4KB
ExportExcelUtil.class 4KB
ImportExcelUtil.class 4KB
ImportExcelUtil.class 4KB
ImportExcelUtil.class 4KB
ImportExcelUtil.class 4KB
ImportExcelUtil.class 4KB
ImportExcelUtil.class 4KB
TxSpider.class 4KB
SessionTimeoutInterceptor.class 4KB
SessionTimeoutInterceptor.class 4KB
AQYSpider$1.class 4KB
AQYSpider$1.class 4KB
TXSpider$1.class 4KB
TXSpider$1.class 4KB
Movie.class 4KB
Movie.class 4KB
Movie.class 4KB
Movie.class 4KB
Movie.class 4KB
SessionTimeoutInterceptor.class 3KB
SessionTimeoutInterceptor.class 3KB
SessionTimeoutInterceptor.class 3KB
SessionTimeoutInterceptor.class 3KB
UserController.class 3KB
UserController.class 3KB
UserController.class 3KB
UserController.class 3KB
UserController.class 3KB
UserController.class 3KB
StringUtil.class 3KB
StringUtil.class 3KB
StringUtil.class 3KB
StringUtil.class 3KB
StringUtil.class 3KB
StringUtil.class 3KB
SystemAdminController.class 2KB
SystemAdminController.class 2KB
SystemAdminController.class 2KB
SystemAdminController.class 2KB
SystemAdminController.class 2KB
MyRealm.class 2KB
MyRealm.class 2KB
MyRealm.class 2KB
MyRealm.class 2KB
MyRealm.class 2KB
MyRealm.class 2KB
PageUtilMoblile.class 2KB
PageUtilMoblile.class 2KB
PageUtilMoblile.class 2KB
PageUtilMoblile.class 2KB
PageUtilMoblile.class 2KB
PageUtilMoblile.class 2KB
TypeCastException.class 2KB
TypeCastException.class 2KB
TypeCastException.class 2KB
TypeCastException.class 2KB
TypeCastException.class 2KB
TypeCastException.class 2KB
PageUtil.class 2KB
PageUtil.class 2KB
PageUtil.class 2KB
PageUtil.class 2KB
PageUtil.class 2KB
PageUtil.class 2KB
MovieServiceImpl.class 2KB
MovieServiceImpl.class 2KB
MovieServiceImpl.class 2KB
共 1959 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
python资深爱好者
- 粉丝: 503
- 资源: 178
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 瑞萨RL78系列MCU设备文件
- Java电子商务平台案例介绍:实现商品展示、购物车、订单管理和支付功能
- yolov8初始化权重文件
- Nacos控制台简介.pdf
- 基于STM32与ESP8266的智能家居系统,以STM32为核心控制单元,通过传感器监测室内环境,结合微信小程序实现数据传输与远
- ChineseCalendar-1.0.5-SNAPSHOT.jar
- 基于Bagging集成学习方法的情绪分类预测模型研究(数据集+代码).rar
- nacos-server-2.3.0-BETA.zip
- 双11活动数据分析-商品销售数据、促销活动的信息、用户行为数据
- 基于记忆与模型协同过滤的电影推荐系统研究与实践(数据集+代码).rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功