📄 typecast.java
字号:
package com.frame.util;
import java.sql.Date;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import com.frame.exception.FrameException;
/**
* <p>Title: 类型转换类</p>
* <p>Description: 类型转换类,用于把String类型的数据和int、double、Date等类型的互相转换。</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author <a href=mailto:wolf_chy@sina.com>Steven Cheng</a>
* @version 1.0
*/
public class TypeCast {
/**
* 把一个String类型的数据,转换成int型的。如果转换失败,抛出AppException
* @param str
* @param paraName
* @param isCanNull
* @return
* @throws AppException
*/
public static int stringToInt(String str,String paraName,boolean isCanNull)
throws FrameException
{
if(str==null || str.equals(""))
{
if(isCanNull)
return NullFlag.INTNULL;
else
throw new FrameException(GlobalErrorCode.INPUTPARAMTYPEERRORCODE,"| 传入的参数:"+paraName+"为空,无法转换成 int 型 |");
}
try{
return Integer.parseInt(str);
}
catch(NumberFormatException nfe)
{
throw new FrameException(GlobalErrorCode.INPUTPARAMTYPEERRORCODE,"| 传入的参数:"+paraName+"错误,无法转换成 int 型 |");
}
}
/**
* 把一个String类型的数据转换成double型的。如果转换失败,抛出AppException
* @param str
* @param paraName
* @param isCanNull
* @return
* @throws AppException
*/
public static double stringToDouble(String str,String paraName,boolean isCanNull)
throws FrameException
{
if(str==null || str.equals(""))
{
if(isCanNull)
return NullFlag.DOUBLENULL;
else
throw new FrameException(GlobalErrorCode.INPUTPARAMTYPEERRORCODE,"| 传入的参数:"+paraName+"为空,无法转换成 double 型 |");
}
try{
return Double.parseDouble(str);
}
catch(NumberFormatException nfe)
{
throw new FrameException(GlobalErrorCode.INPUTPARAMTYPEERRORCODE,"| 传入的参数:"+paraName+"错误,无法转换成 double 型 |");
}
}
/**
* This method format a string to a java.sql.Date's object.The string must like "yyyy-MM-dd".
* For Example:
* TypeCast.stringToDate("1995-1-5","parameter name",false);
* @param strDate A String whose should be parsed.
* @param paraName
* @param isCanNull if this parameter is false,strDate must be not null.
* @return
* @throws AppException
*/
public static Date stringToDate(String strDate,String paraName,boolean isCanNull)
throws FrameException
{
Date targetDate = null;
if(strDate==null || strDate.equals(""))
{
if(isCanNull)
return null;
else
throw new FrameException(GlobalErrorCode.NULLINPUTPARAMCODE,"| 传入的参数:"+paraName+"为空,无法转换成 Date 型 |");
}
if(strDate.indexOf("-")==-1)
throw new FrameException(GlobalErrorCode.INPUTPARAMTYPEERRORCODE,"| 传入的参数:"+paraName+"格式不对,无法转换成 Date 型 |");
int yyyy,MM,dd;
try{
yyyy = Integer.parseInt(strDate.substring(0,strDate.indexOf("-")));
MM = Integer.parseInt(strDate.substring(strDate.indexOf("-")+1,strDate.lastIndexOf("-")));
dd = Integer.parseInt(strDate.substring(strDate.lastIndexOf("-")+1,strDate.length()));
targetDate = DateUtil.getDate(yyyy,MM,dd);
}
catch(NumberFormatException nfe)
{
throw new FrameException(GlobalErrorCode.INPUTPARAMTYPEERRORCODE,"| 传入的参数:"+paraName+"错误,无法转换成 Date 型 |");
}
catch(IllegalArgumentException e)
{
throw new FrameException(GlobalErrorCode.INPUTPARAMTYPEERRORCODE,"| 传入的参数:"+paraName+"错误,无法转换成 Date 型 |");
}
return targetDate;
}
/**
* int类型转换成String类型。
* @param para
* @return
*/
public static String intToString(int para)
{
return ""+para;
}
/**
* 把一个double型的数据转换成一个字符串,用于显示。
* 按照货币表示:0为0.00; 19为19.00;24.1为24.10;
* 25.225为25.23。
* @param para
* @return
*/
private static String doubleToString(double para)
{
NumberFormat nf = new DecimalFormat("###.##");
String strValue = nf.format(para);
if(strValue.indexOf(".")==-1)
strValue = strValue + ".00";
else if( strValue.substring(strValue.indexOf(".")+1).length()==1)
strValue = strValue + "0";
return strValue;
}
/**
* format a Date to String. the String like "yyyy-MM-dd".
* if the Date is null,return a empty String
* @param date
* @return
*/
public static String simpleDateToString(Date date)
{
if(date==null)
return "";
java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
return format.format(date);
}
/**
* format a Date to String. the String like "yyyy-MM-dd HH:mm:ss"
* if the Date is null,return a empty String
* @param date
* @return
*/
public static String dateTimeToString(Date date)
{
if(date==null)
return "";
java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.format(date);
}
/**
* 字符串转换,如果是null,允许为空,返回"";
* 不允许为空,throws AppException,字符串不为空,返回strSource.trim()
* @param strSource
* @param paraName
* @param isCanNull
* @return
* @throws AppException
*/
public static String stringToString(String strSource,String paraName,boolean isCanNull)
throws FrameException
{
if(strSource==null)
{
if(isCanNull)
return "";
else
throw new FrameException(GlobalErrorCode.NULLINPUTPARAMCODE,"| 传入的参数:"+paraName+"为空,无法转换成 String 型 |");
}
return strSource;
}
/**
* 字符串转换,如果是null,返回empty String
* @param strSource
* @return
*/
public static String stringToString(String strSource)
{
if(strSource==null)
return "";
else
return strSource;
}
/**
* 把一个String类型的数据转换成long型。如果转换失败,抛出AppException
* @param str
* @param paraName
* @param isCanNull
* @return
* @throws AppException
*/
public static long stringToLong(String str,String paraName,boolean isCanNull)
throws FrameException
{
if(str==null || str.equals(""))
{
if(isCanNull)
return NullFlag.LONGNULL;
else
throw new FrameException(GlobalErrorCode.INPUTPARAMTYPEERRORCODE,"| 传入的参数:"+paraName+"为空,无法转换成 long 型 |");
}
try{
return Long.parseLong(str);
}
catch(NumberFormatException nfe)
{
throw new FrameException(GlobalErrorCode.INPUTPARAMTYPEERRORCODE,"| 传入的参数:"+paraName+"错误,无法转换成 long 型 |");
}
}
/**
* long型转换成String类型。
* @param para
* @return
*/
public static String longToString(long para)
{
return ""+para;
}
/**
* 非金额的double类型数据转换成String类型,直接转换,不定义格式
* @param para
* @return
*/
public static String notMoneyDoubleToString(double para)
{
return ""+para;
}
/**
* 把一个double型的数据转换成一个字符串,用于显示。
* 按照货币表示:0为0.00; 19为19.00;24.1为24.10;
* 25.225为25.23。
* @param para
* @return
*/
public static String moneyDoubleToString(double para)
{
return doubleToString(para);
}
public static void main(String[] args)
{
//String strDate = "2003-1-8";
//java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
//NumberFormat nf = new DecimalFormat("###.##");
double para = 42;
double para1 = 22.2d;
double para2 = 123456.1157;
double para3 =0;
//String strValue = nf.format(para);
System.out.println(doubleToString(para));
System.out.println(doubleToString(para1));
System.out.println(doubleToString(para2));
System.out.println(doubleToString(para3));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -