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:LKDBHelper实体对象映射数据库-第三方框架(在FMDB的基础上进行二次封装)
一 插件简介: 其github地址:https://github.com/li6185377/LKDBHelper-SQLite-ORM 全面支持 NSArray,NSDictionary, Mode ...
- cmake处理多源文件目录的方法
cmake处理源代码分布在不同目录中的情况也很简单,现在假设我们的源代码分布情况如下: 源代码的分布情况 其中src目录下的文件要编译成一个链接库 第一步,项目主目录中的CMakelist.txt 在 ...
- Geeks - Check whether a given graph is Bipartite or not 二分图检查
检查一个图是否是二分图的算法 使用的是宽度搜索: 1 初始化一个颜色记录数组 2 利用queue宽度遍历图 3 从随意源点出发.染色0. 或1 4 遍历这点的邻接点.假设没有染色就染色与这个源点相反的 ...
- @NotEmpty、@NotBlank、@NotNull区别
@NotEmpty 用在集合类上面 @NotBlank 用在String上面 @NotNull 用在基本类型上
- JVM类加载机制详解(一)JVM类加载过程
http://blog.csdn.net/zhangliangzi/article/details/51319033 http://chenzhou123520.iteye.com/blog/1597 ...
- 【百度地图JavaScript API】手机端浏览器定位的实现
[百度地图JavaScript API]手机端浏览器定位的实现 https://blog.csdn.net/xiao190128/article/details/72579476
- 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)如何在TwinCAT Scope中做变量监控
为了更好的监控变量,可以打开ScopeView即变量监控器 添加一个Scope View,然后右击添加一个Channel 我们在之前登录的时候可以选择Run-Time的端口(默认是801) ...
- vue2.0快速构建项目
准备工作:已经安装了nodejs,已经安装了vue-cli $ mkdir gankbook $ cd gankbook $ vue init webpack-simple 按照需要写好信息,这将会写 ...
- 算法笔记_159:算法提高 第二大整数(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 编写一个程序,读入一组整数(不超过20个),当用户输入0时,表示输入结束.然后程序将从这组整数中,把第二大的那个整数找出来,并把它打印出来 ...
- canvas锯齿
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...