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 :两位数的年份表示( ...
随机推荐
- 如何在Visual Studio中加载web load test的后缀为.ltrar的结果文件
1. From a Web performance and load test project, open a load test. 2. On the embedded toolbar, cho ...
- poj3592 Instantaneous Transference tarjan缩点+建图
//给一个n*m的地图.坦克从(0 , 0)開始走 //#表示墙不能走,*表示传送门能够传送到指定地方,能够选择也能够选择不传送 //数字表示该格的矿石数, //坦克从(0,0)開始走.仅仅能往右和往 ...
- SVN-项目 XXX 受源代码管理。向源代码管理注册此项目时出错。建议不要对此项目进行任何修改
错误描述: 项目 XXX 受源代码管理.向源代码管理注册此项目时出错.建议不要对此项目进行任何修改 解决办法: 使用记事本打开,项目csproj文件删除图中几行,重新打开解决方案就可以了 原因分析: ...
- 安装Tomcat指定JDK ——转
转自:http://www.cnblogs.com/lioillioil/archive/2011/10/08/2202169.html 一.应用实例 一般情况下一台服务器只跑一个业务,那么就直接配置 ...
- 运行时权限请求框架easypermissions
前言 之前使用过AndPermission权限申请库,当开发者执行有权限的代码发生异常时,AndPermission会抓到异常并回调到失败中,这里要注意的是会抓到任何异常,不仅仅是没有权限时的异常. ...
- Android开发调试常用命令列表
Android开发调试常用命令列表 adb命令 am am start -n com.iflytek.autofly.account/.ui.MainActivity am start -n com. ...
- Unity3d 显示IOS基本的游戏中心脚本
using UnityEngine; using UnityEngine.SocialPlatforms; public class Startup : MonoBehaviour { // we'l ...
- iOS APP 上传
原地址:http://www.cnblogs.com/uvsjoh/archive/2012/11/14/2769739.html 流程:1 开发好要发布的程序 -- 需要在程序中包含符合要求规格的i ...
- react 执行 yarn build 后 去除 .js.map 文件
map文件是帮助我们查看报错的位置的. map文件由devtool属性控制,如果不想要map,注释掉就可以,大约webpack.config.prod.js第57行: // devtool: shou ...
- 001-使用idea开发环境安装部署,npm工具栏,脚本运行
一.概述 参看官方文档:https://ant.design/docs/spec/introduce-cn 其中包含了设计价值观.设计原则.视觉.模式.可视化.动态等. 其中Ant Design 的 ...