时间处理

本文介绍了一个Java通用转换帮助类,提供了多种数据类型的转换方法,包括字符串到各种数值类型及日期类型的转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

首先写了一个公用的转换帮助类

package com.dzf.test;

import java.math.BigDecimal;
import java.sql.Date;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class ConvertHelper ... {
public static final StringDecimalFormat = " ###,###,###,##0.00 " ;

public static final StringDateFormat = " yyyy-MM-dd " ;

/***/ /**
*把字符串转换为整数。
*
*
@param str
*待转换的字符串
*
@return 整数
*
@throws Exception
*如果字符串格式不对
*/

public static int StringToInt(Stringstr) ... {
if (str == null || str.equals( "" )) ... {
return 0 ;
}
else ... {
return Integer.parseInt(str);
}

}


/***/ /**
*把字符串转换为长整形。
*
*
@param str
*待转换的字符串
*
@return 整数
*
@throws Exception
*如果字符串格式不对
*/

public static long StringToLong(Stringstr) ... {
if (str == null || str.equals( "" ))
return 0 ;
return StringToNumber(str).longValue();

}


/***/ /**
*把字符串转换为DOUBLE。
*
*
@param str
*待转换的字符串
*
@return 整数
*
@throws Exception
*如果字符串格式不对
*/

public static double StringToDouble(Stringstr) ... {
if (str == null || str.equals( "" ))
return 0 ;
return StringToNumber(str).doubleValue();

}


/***/ /**
*把字符串转换为Decimal。
*
*
@param str
*待转换的字符串
*
@return 整数
*
@throws Exception
*如果字符串格式不对
*/

public static BigDecimalStringToDecimal(Stringstr) ... {
return new BigDecimal(StringToNumber(str).doubleValue());

}


/***/ /**
*把字符串转换为NUMBER。
*
*
@param str
*待转换的字符串
*
@return 整数
*
@throws Exception
*如果字符串格式不对
*/

public static NumberStringToNumber(Stringstr) ... {
if (str == null )
return null ;
if (str.equals( "" ))
str
= " 0 " ;

Numbernumber;

DecimalFormatdf
= new DecimalFormat(DecimalFormat);
try ... {
number
= df.parse(str);
return new BigDecimal(number.toString());
}
catch (ParseExceptione) ... {
e.printStackTrace();
}

return null ;
}


/***/ /**
*把BigDecimal类型转换为字符串
*
*
@param number
*
@return
*/

public static StringDecimalToString(BigDecimalnumber) ... {
if (number == null ) ... {
return " 0.00 " ;
}

DecimalFormatdf
= new DecimalFormat(DecimalFormat);

return df.format(number);
}


/***/ /**
*把Number类型转换为字符串
*
*
@param number
*
@return
*/

public static StringDecimalToString(Numbernumber) ... {
if (number == null ) ... {
return " 0.00 " ;
}

DecimalFormatdf
= new DecimalFormat(DecimalFormat);

return df.format(number);
}


/***/ /**
*把double类型转换为字符串
*
*
@param number
*
@return
*/

public static StringDecimalToString( double number) ... {
DecimalFormatdf
= new DecimalFormat(DecimalFormat);
return df.format(number);
}


/***/ /**
*把long类型转换为字符串
*
*
@param number
*
@return
*/

public static StringDecimalToString( long number) ... {
DecimalFormatdf
= new DecimalFormat(DecimalFormat);
return df.format(number);
}


/***/ /**
*把java.util.Date类型转换为字符串
*
*
@param date
*
@return
*/

public static StringDateToString(java.util.Datedate) ... {
if (date == null )
return "" ;
SimpleDateFormatsdf
= new SimpleDateFormat(DateFormat);
return sdf.format(date);
}


/***/ /**
*把字符串转换为Date类型
*
*
@param str
*时间类型如"2007-10-28"
*
@return
*
@throws Exception
*/

public static DateStringToDate(Stringstr) ... {
if (str == null || str.equals( "" ))
return null ;
SimpleDateFormatsdf
= new SimpleDateFormat(DateFormat);
Datedate;
try ... {
date
= new Date(sdf.parse(str).getTime());
return date;
}
catch (ParseExceptione) ... {
e.printStackTrace();
}

return null ;
}


/***/ /**
*把字符串转换为Date类型
*
*
@param str
*时间类型如"2007-10-28"
*
@param format
*时间格式如"yyyy-MM-dd"
*
@return
*
@throws Exception
*/

public static DateStringToDate(Stringstr,Stringformat) ... {
if (str == null || str.equals( "" ))
return null ;
SimpleDateFormatsdf
= new SimpleDateFormat(format);
Datedate;
try ... {
date
= new Date(sdf.parse(str).getTime());
return date;
}
catch (ParseExceptione) ... {
e.printStackTrace();
}

return null ;
}


/***/ /**
*将OBJECT转换为STRING
*
*
@param obj
*
@return 如果OBJ为NULL,则返回null
*/

public static StringObjToString(Objectobj) ... {
return ObjToString(obj, null );
}


/***/ /**
*将OBJECT转换为STRING
*
*
@param obj
*
@param retStr
*为空时的返回值
*
@return
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值