FormatUtil类型格式转换
package cn.edu.hbcf.common.utils; import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; /**
* FormatUtil.class 字符串,数字之间的转换工具类。
*
* @author*/ public class FormatUtil { private static final Log log = LogFactory.getLog(FormatUtil.class); /**
* Integer转换为String,如果integer==null则返回null
*
* @param integer
* @return
*/
public static final String convertIntegerToString(Integer integer) {
if (integer == null) {
return null;
}
return integer.toString();
} // add by sxy:把一个字符串 前面加0直到8位,如时间9080808转成09080808
public static final String convertStringTo8DigitString(String str) {
if (str == null) {
return null;
}
String result = "";
int length = (8 - str.length());
for (int i = 0; i < length; i++) {
result = result + "0";
}
return result + str;
} /**
* Short转换为String,如果parashort==null则返回null
*
* @param parashort
* @return
*/
public static final String convertShortToString(Short parashort) {
if (parashort == null) {
return null;
}
return parashort.toString();
} /**
* CHECKBOX 转化
*
* @param parashort
* @return
*/
public static final String checkboxToString(String str) {
if (str == null) {
return "0";
} else {
return "1";
}
} /**
* BigDecimal转换为String,如果bigDecimal==null则返回null
*
* @param bigDecimal
* @return
*/
public static final String convertBigDecimalToString(BigDecimal bigDecimal) {
if (bigDecimal == null) {
return null;
}
return bigDecimal.toString();
} /**
* 字符串转换为BigDecimal,转换失败返回null.
*
* @param num
* @return
*/
public static final BigDecimal convertStringToDecimal(String num) {
BigDecimal bigDecimal = null;
try {
bigDecimal = new BigDecimal(num);
} catch (Exception e) {
log.warn("FormatUtil.convertStringToDecimal(" + num
+ ") failed with the errorMessage:" + e.getMessage());
}
return bigDecimal;
} /**
* 字符串转换为Integer,转换失败返回null.
*
* @param aStr
* @return
*/
public static final Integer StringToInt(String aStr) {
Integer integer = 0;
try {
if (!isEmpty(aStr)) {
integer = new Integer(aStr);
}
} catch (Exception e) {
log.warn("FormatUtil.StringToInt(" + aStr
+ ") failed with the errorMessage:" + e.getMessage());
}
return integer;
} /**
* 字符串是否为空:str==null 或者str为空串返回true;否则返回false
*
* @param str
* @return
*/
public static final boolean isEmpty(String str) {
return (str == null || str.trim().length() == 0) ? true : false;
} /**
* 字符串是否非空:str==null 或者str为空串返回false;否则返回true
*
* @param str
* @return
*/
public static final boolean isNotEmpty(String str) {
return !isEmpty(str);
} /**
* 如果字符串是空或者是空串,则返回null;否则返回str.trim()
*
* @param str
*/
public static final String trimNull(String str) {
return (isEmpty(str) ? null : str.trim());
} /**
* Integer 转换成BigDecimal:如果integer==null,返回null;否则返回转换后的BigDecimal.
*
* @param integer
* @return
*/
public static final BigDecimal convertIntegerToBigDecimal(Integer integer) {
return (integer == null ? null : BigDecimal.valueOf(integer));
} /**
* Long 转换成BigDecimal:如果Long==null,返回null;否则返回转换后的BigDecimal.
*
* @param Long
* @return
*/
public static final BigDecimal convertLongToBigDecimal(Long para) {
return (para == null ? null : BigDecimal.valueOf(para));
} /**
* Integer 转换成BigInteger:如果integer==null,返回null;否则返回转换后的BigInteger.
*
* @param integer
* @return
*/
public static final BigInteger convertIntegerToBigInteger(Integer integer) {
return (integer == null ? null : BigInteger.valueOf(integer));
} /**
* Short 转换成BigInteger:如果integer==null,返回null;否则返回转换后的BigInteger.
*
* @param integer
* @return
*/
public static final BigInteger convertShortToBigInteger(Short source) {
return (source == null ? null : BigInteger.valueOf(source));
} /**
* BigInteger 转换成Integer:如果bigInteger==null,返回null;否则返回转换后的Integer.
*
* @param bigInteger
*
* @return
*/
public static final Integer convertBigIntegerToInteger(BigInteger bigInteger) {
return (bigInteger == null ? null : bigInteger.intValue());
} public static final Integer convertBigDecimalToInteger(BigDecimal bigDecimal) {
if (bigDecimal == null) {
return null;
}
return bigDecimal.intValue();
} public static final Long StringToLong(String aStr) {
Long longTemp = null;
try {
longTemp = new Long(aStr);
} catch (Exception e) {
log.warn("FormatUtil.StringToLong(" + aStr
+ ") failed with the errorMessage:" + e.getMessage());
}
return longTemp;
} public static final Integer convertLongToInteger(Long para) {
return (para == null ? null : para.intValue());
} /**
* 将字符串str截取成最大maxBytesLength的字2节,如果str字节数小于maxBytesLength,则返回原始str
*/
public static String subString(String str, int maxBytesLength) {
if (isNotEmpty(str) && str.getBytes().length > maxBytesLength) {
byte[] bytesOut = new byte[maxBytesLength];
byte[] bytesIn = str.getBytes();
System.arraycopy(bytesIn, 0, bytesOut, 0, maxBytesLength);
str = new String(bytesOut);
}
return str;
} /**
* 将字符串str转化成double类型
*
*/
public static final Double convertStringToDouble(String str) {
Double doubleTemp = 0.0;
if (isEmpty(str)) {
return doubleTemp;
} else {
doubleTemp = Double.parseDouble(str);
return doubleTemp;
}
} /**
* 将字符串str转化成Float类型
*
*/
public static final Float convertStringToFloat(String str) {
Float floatTemp = new Float(0);
if (isEmpty(str)) {
return floatTemp;
} else {
floatTemp = Float.valueOf(str);
return floatTemp;
}
} public static final Long convertBigDecimalToLong(BigDecimal bigDecimal) {
String strTemp = convertBigDecimalToString(bigDecimal);
Long longTemp = StringToLong(strTemp);
return longTemp;
} public static final BigInteger convertBigDecimalToBigInteger(
BigDecimal bigDecimal) {
Integer intTemp = convertBigDecimalToInteger(bigDecimal);
BigInteger bigIntegerTemp = convertIntegerToBigInteger(intTemp);
return bigIntegerTemp;
} public static void main(String args[]) {
String result = convertStringTo8DigitString("22111111");
System.out.println(result);
} public static Map<String, Object> string2Map(String context) {
if (context.length() <= 2) {
return null;
}
Map<String, Object> map = new HashMap<String, Object>();
context = context.substring(1, context.length() - 1);
String[] contextArray = context.split(",");
for (String attribute : contextArray) {
String[] attr = attribute.split(":");
if (attr.length == 2 && attr[1].length() > 2) {
String value = attr[1].substring(1, attr[1].length() - 1);
map.put(attr[0], value);
}
}
return map;
}
}
FormatUtil类型格式转换的更多相关文章
- freemark声明变量,boolean,date,date日期格式转换成String类型的(五)
<br/>assign用来定义变量<#assign name="刘德华"><br/> 获取assign定义变量的值:${name} <br ...
- Gson格式转换Integer变为Double类型问题解决
问题描述 在前后端分离的开发模式下,前后端交互通常采用JSON格式数据.自然会涉及到json字符串与JAVA对象之间的转换.实现json字符串与Java对象相互转换的工具很多,常用的有Json.Gso ...
- mysql时间类型和格式转换
内容目录 简介mysql时间类型DATE_FORMAT()函数 简介 今天开发中,做一个功能需要对历史数据进行补充,相信大家也遇到过这样的情况,这个历史数据需要按月份和人的id进行区分,于是想到了my ...
- FFmpeg学习4:音频格式转换
前段时间,在学习试用FFmpeg播放音频的时候总是有杂音,网上的很多教程是基于之前版本的FFmpeg的,而新的FFmepg3中audio增加了平面(planar)格式,而SDL播放音频是不支持平面格式 ...
- 【VC++技术杂谈007】使用GDI+进行图片格式转换
本文主要介绍如何使用GDI+对图片进行格式转换,可以转换的图片格式为bmp.jpg.png. 1.加载GDI+库 GDI+是GDI图形库的一个增强版本,提供了一系列Visual C++ API.为了使 ...
- Oracle日期格式转换,tochar(),todate()
Oracle日期格式转换 本文主要介绍Oracle中的日期转换. 1. 日期转化为字符串 (以2016年10月20日为例) select to_char(sysdate,'yyyy-mm-dd hh2 ...
- SpringMVC与MyBatis整合之日期格式转换
在上一篇博客<SpringMVC与MyBatis整合(一)——查询人员列表>中遗留了日期格式转换的问题,在这篇记录解决过程. 对于controller形参中pojo对象,如果属性中有日期类 ...
- SpringMVC对日期类型的转换
在做web开发的时候,页面传入的都是String类型,SpringMVC可以对一些基本的类型进行转换,但是对于日期类的转换可能就需要我们配置. 1.如果查询类使我们自己写,那么在属性前面加上@Date ...
- Python 日期格式转换
经常需要爬取网站上的时间信息,不同的网站又有不同的日期显示方式.而我需要将日期格式转化为一种特定的格式,所以为了简便和学习,记录下各种不同的日期格式转换. 日期格式化符号: %y :两位数的年份表示( ...
随机推荐
- iOS:图片上传时两种图片压缩方式的比较
上传图片不全面的想法:把图片保存到本地,然后把图片的路径上传到服务器,最后又由服务器把路径返回,这种方式不具有扩展性,如果用户换了手机,那么新手机的沙盒中就没有服务器返回的图片路径了,此时就无法获取之 ...
- Android Killer
首先,我们先看一Android界有名的大神写关于Android反编译的博客: 郭 大 侠:http://blog.csdn.net/guolin_blog/article/details/497380 ...
- centos7 将服务添加到systemctl
centos7中提供了systemd服务,可以方便的管理各种服务 但是有些通过编译安装的服务systemd里面没有,我们只需要添加一下服务文件即可 以下用nginx作为例子,展示如何添加服务到syst ...
- django 模板实现换行
django中的模板并不能实现自动换行,遇到/n的话不会自动换行,毕竟/n是python里面的换行符,html怎么可能认识呢? 那如何实现在模板中换行呢 <p> {{ article.co ...
- ArcGIS Server的切图原理深入【转】
http://blog.newnaw.com/?p=69 GoogleMap,Virtual Earth,YahooMap等,目前所有的WebGIS都使用了缓存机制以提高地图访问速度.原理都是将地图设 ...
- jquery触发/失去焦点事件
触发焦点: $("Element").focus() 触发每一个匹配元素获得焦点事件. $("Element").focus(function) 事件会在获得焦 ...
- android APK反编译及代码混淆
反编译.查看源代码,需要用到两个工具:dex2jar 和 jdgui dex2jar(google code) jdgui(google code),最新版本请见 官方 操作很简单,步骤如下: 1.将 ...
- Android模糊演示样例-RenderScript-附效果图与代码
本文链接 http://blog.csdn.net/xiaodongrush/article/details/31031411 參考链接 Android高级模糊技术 http://s ...
- LoadRunner访问 Mysql数据库
这是很久以前编写的一个测试案例,那时是为了检查大量往Mysql数据库里插入数据,看一下数据库的性能如何?服务器是否会很快就被写满了. 前期的准备工作:Mysql 数据库搭建,LoadRunner,li ...
- MVC总结--数据传递
View->Controller 1.传统方式,Get方式提交.在Controller中获得client请求: string Name = Request["name"]; ...