package com.chenxin.authority.common.utils;
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;
/**
* 类型转换辅助工具类<br>
*
* @author XiongChun
* @since 2009-07-06
*/
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());
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Spring+mybatis+SpringMVC+Ext+maven的项目 (1249个子文件)
.classpath 926B
org.eclipse.wst.common.component 714B
org.eclipse.wst.jsdt.ui.superType.container 44B
ext-all.css 136KB
ext-all-notheme.css 98KB
xtheme-access.css 40KB
xtheme-blue.css 38KB
xtheme-gray.css 38KB
yourtheme.css 37KB
ux-all.css 17KB
idealforms.css 14KB
form.css 11KB
grid.css 11KB
idealforms-min.css 11KB
normalize.css 8KB
button.css 8KB
panel.css 8KB
tabs.css 7KB
grid.css 6KB
grid.css 6KB
grid.css 6KB
panel-reset.css 6KB
GroupTab.css 6KB
core.css 5KB
toolbar.css 5KB
layout.css 5KB
date-picker.css 5KB
tree.css 4KB
menu.css 4KB
tree.css 4KB
form.css 4KB
window.css 3KB
tree.css 3KB
tree.css 3KB
button.css 3KB
icons.css 3KB
toolbar.css 3KB
tabs.css 3KB
date-picker.css 3KB
editor.css 3KB
tabs.css 3KB
resizable.css 3KB
date-picker.css 3KB
date-picker.css 3KB
statusbar.css 3KB
form.css 3KB
tabs.css 3KB
toolbar.css 3KB
toolbar.css 3KB
qtips.css 3KB
form.css 2KB
button.css 2KB
button.css 2KB
normalize-min.css 2KB
panel.css 2KB
panel.css 2KB
slider.css 2KB
panel.css 2KB
window.css 2KB
menu.css 2KB
window.css 2KB
window.css 2KB
core.css 2KB
resizable.css 2KB
resizable.css 2KB
core.css 2KB
resizable.css 2KB
menu.css 2KB
list-view.css 2KB
core.css 1KB
RowEditor.css 1KB
box.css 1KB
box.css 1KB
box.css 1KB
box.css 1KB
menu.css 1KB
Spinner.css 1KB
ColumnNodeUI.css 1KB
layout.css 1KB
layout.css 1KB
layout.css 1KB
pivotgrid.css 1KB
qtips.css 1KB
qtips.css 1KB
qtips.css 1KB
GridFilters.css 1KB
dialog.css 1KB
borders.css 1KB
dd.css 1006B
treegrid.css 940B
debug.css 917B
combo.css 895B
combo.css 889B
list-view.css 878B
combo.css 877B
dialog.css 811B
dialog.css 807B
dialog.css 800B
combo.css 787B
pivotgrid.css 781B
共 1249 条
- 1
- 2
- 3
- 4
- 5
- 6
- 13
资源评论
- xihuishi50412017-09-26你们能解压吗
- qq_162653152015-02-21具有一些参考意义
- 深蓝色的风2015-06-16项目结构比较清晰,只是版本不是最新的,需要自己手动修改pom.xml! 还是非常感谢楼主!
- gzntzxj2015-07-17需要自己配置Maven,结构还算清晰!
iushnauh
- 粉丝: 6
- 资源: 14
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- springboot-mavenBaseDemo 内容包含:springboot的maven基础状态,1.8JDK可以直接运行
- otis rsl远程串行接口协议标准.pdf
- buildx构建镜像时所需的镜像文件
- F103-霸道开发板2.8寸电阻触摸屏例程.rar
- Google(高德)地图瓦片python代码下载
- Python实现输出杨辉三角形
- polsarpro官方教程、操作说明 PolSARpro v5.0 Software Training Course
- STM32 TouchGFX的使用二图片显示
- buildx镜像文件,也可以通过网上其他方式获取
- 【中级软件设计师】上午题12-软件工程(2):单元测试、黑盒测试、白盒测试、软件运行与维护
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功